Tag: runtime.exec

如何使用Java运行时使用“cd”命令?

我创build了一个独立的Java应用程序,我试图在Ubuntu 10.04terminal中使用“cd”命令来更改目录。 我已经使用了下面的代码。 String[] command = new String[]{"cd",path}; Process child = Runtime.getRuntime().exec(command, null); 但是上面的代码给出了以下错误 Exception in thread "main" java.io.IOException: Cannot run program "cd": java.io.IOException: error=2, No such file or directory 任何人都可以请告诉我如何实现它?

process.waitFor()永远不会返回

Process process = Runtime.getRuntime().exec("tasklist"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); process.waitFor();

如何解决“java.io.IOException:错误= 12,无法分配内存”调用运行时#exec()?

在我的系统上,我无法运行启动进程的简单Java应用程序。 我不知道如何解决。 你能给我一些提示如何解决? 该计划是: [root@newton sisma-acquirer]# cat prova.java import java.io.IOException; public class prova { public static void main(String[] args) throws IOException { Runtime.getRuntime().exec("ls"); } } 结果是: [root@newton sisma-acquirer]# javac prova.java && java -cp . prova Exception in thread "main" java.io.IOException: Cannot run program "ls": java.io.IOException: error=12, Cannot allocate memory at java.lang.ProcessBuilder.start(ProcessBuilder.java:474) at java.lang.Runtime.exec(Runtime.java:610) at java.lang.Runtime.exec(Runtime.java:448) […]

如何从我的Java应用程序运行batch file?

在我的Java应用程序中,我想运行一个batch file,它调用“ scons -Q implicit-deps-changed build\file_load_type export\file_load_type ” 看来,我甚至不能让我的batch file执行。 我没有想法。 这是我在Java中所拥有的: Runtime. getRuntime(). exec("build.bat", null, new File(".")); 以前,我有一个Python Sconscript文件,我想运行,但由于没有工作,我决定我会通过batch file调用脚本,但该方法尚未成功。

Runtime.getRuntime()。exec()不起作用

我需要从一个程序执行一个命令。 命令行是好的,我在terminal上试了一下,但是在程序中不起作用。 我从我的代码中添加一个副本: File dir = new File("videos"); String[] children = dir.list(); if (children == null) { // Either dir does not exist or is not a directory System.out.print("No existe el directorio\n"); } else { for (int i=0; i<children.length; i++) { // Get filename of file or directory String filename = children[i]; //Recojo el momento […]

为什么Runtime.exec(String)适用于一些而不是所有的命令?

当我尝试运行Runtime.exec(String) ,某些命令工作,而其他命令执行,但失败或做不同于我的terminal。 这里是一个自包含的testing用例,演示了这个效果: public class ExecTest { static void exec(String cmd) throws Exception { Process p = Runtime.getRuntime().exec(cmd); int i; while( (i=p.getInputStream().read()) != -1) { System.out.write(i); } while( (i=p.getErrorStream().read()) != -1) { System.err.write(i); } } public static void main(String[] args) throws Exception { System.out.print("Runtime.exec: "); String cmd = new java.util.Scanner(System.in).nextLine(); exec(cmd); } } 如果我用echo hello […]

运行时的exec()方法不redirect输出

Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt"); 我正在使用Java运行此命令。 脚本正在运行,但不会将其streamredirect到文件。 而且, out.txt文件没有被创build。 这个脚本运行良好,如果我在shell上运行它。 有任何想法吗?

如何在另一个java程序中编译和运行java程序?

我有一个Main.java和Test.java类,我想在Test.java代码中编译和运行Main.java。 这是我的代码 Process pro1 = Runtime.getRuntime().exec("javac Main.java"); pro1.waitFor(); Process pro2 = Runtime.getRuntime().exec("java Main"); BufferedReader in = new BufferedReader(new InputStreamReader(pro2.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } 我只是在Main.java中打印“ok”,但是这个代码不打印任何东西。 问题是什么 ?

在java中执行外部程序

我试图做一个应用程序,调用一个外部程序,我必须传递两个参数。 它不会给出任何错误。用c ++编写的program.exe会拍摄一张照片并修改txt文件的内容。 Java程序运行,但它什么都不做 这是我的示例代码 String[] params = new String [3]; params[0] = "C:\\Users\\user\\Desktop\\program.exe"; params[1] = "C:\\Users\\user\\Desktop\\images.jpg"; params[2] = "C:\\Users\\user\\Desktop\\images2.txt"; Runtime.getRuntime().exec(params);

如何用参数执行命令?

如何使用参数在Java中执行命令? Process p = Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php -m 2"}); 没有工作。 String[] options = new String[]{"option1", "option2"}; Runtime.getRuntime().exec("command", options); 也没有工作,因为它没有指定“m”参数。