runtime.exec

Printing Runtime exec() OutputStream to console

一世执手 提交于 2019-11-26 02:57:38
问题 I am trying to get the OutputStream of the Process initiated by exec() to the console. How can this be done? Here is some incomplete code: import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; public class RuntimeTests { public static void main(String[] args) { File path = new File(\"C:\\\\Dir\\\\Dir2\"); String command = \"cmd /c dir\"; Reader rdr = null; PrintStream prtStrm = System.out

read the output from java exec

核能气质少年 提交于 2019-11-26 02:38:06
问题 Hello i have some question about java. here is my code: public static void main(String[] args) throws Exception { Process pr = Runtime.getRuntime().exec(\"java -version\"); BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } pr.waitFor(); System.out.println(\"ok!\"); in.close(); System.exit(0); } in that code i\'am trying to get a java version command execute is ok, but i can\'t

Runtime's exec() method is not redirecting the output

我的梦境 提交于 2019-11-26 02:37:38
问题 Process p = Runtime.getRuntime().exec(\"sh somescript.sh &> out.txt\"); I am running this command using Java. The script is running but it\'s not redirecting its stream to the file. Moreover, the file out.txt is not getting created. This script runs fine if I run it on shell. Any ideas? 回答1: You need to use ProcessBuilder to redirect. ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh"); builder.redirectOutput(new File("out.txt")); builder.redirectError(new File("out.txt"));

Execute external program in java

こ雲淡風輕ζ 提交于 2019-11-26 00:44:30
问题 I tried to make an application that calls an external program that I have to pass two parameters. It doesn\'t give any errors.The program.exe,written in c++, takes a picture and modifies the content of txt file. The java program runs but it does nothing Here is my sample code 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\"

process.waitFor() never returns

一世执手 提交于 2019-11-26 00:33:19
问题 Process process = Runtime.getRuntime().exec(\"tasklist\"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); process.waitFor(); 回答1: There are many reasons that waitFor() doesn't return. But it usually boils down to the fact that the executed command doesn't quit. This, again, can have many reasons. One common reason is that the process produces some output and you don't read from the appropriate streams. This means that the process is blocked as

Difference between ProcessBuilder and Runtime.exec()

孤街浪徒 提交于 2019-11-26 00:28:08
问题 I\'m trying to execute an external command from java code, but there\'s a difference I\'ve noticed between Runtime.getRuntime().exec(...) and new ProcessBuilder(...).start() . When using Runtime : Process p = Runtime.getRuntime().exec(installation_path + uninstall_path + uninstall_command + uninstall_arguments); p.waitFor(); the exitValue is 0 and the command is terminated ok. However, with ProcessBuilder : Process p = (new ProcessBuilder(installation_path + uninstall_path + uninstall_command

Running Command Line in Java [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 00:16:45
问题 This question already has an answer here: Executing a Java application in a separate process 9 answers Is there a way to run this command line within a Java application? java -jar map.jar time.rel test.txt debug I can run it with command but I couldn\'t do it within Java. 回答1: Runtime rt = Runtime.getRuntime(); Process pr = rt.exec("java -jar map.jar time.rel test.txt debug"); http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html 回答2: You can also watch the output like this: final

How to execute command with parameters?

好久不见. 提交于 2019-11-25 23:47:27
问题 How am I to execute a command in Java with parameters? I\'ve tried Process p = Runtime.getRuntime().exec(new String[]{\"php\",\"/var/www/script.php -m 2\"}); which doesn\'t work. String[] options = new String[]{\"option1\", \"option2\"}; Runtime.getRuntime().exec(\"command\", options); This doesn\'t work as well, because the m parameter is not specified. 回答1: See if this works (sorry can't test it right now) Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"}); 回答2:

Why does Runtime.exec(String) work for some but not all commands?

孤街浪徒 提交于 2019-11-25 23:41:11
问题 When I try to run Runtime.exec(String) , certain commands work, while other commands are executed but fail or do different things than in my terminal. Here is a self-contained test case that demonstrates the effect: 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

How to make pipes work with Runtime.exec()?

半腔热情 提交于 2019-11-25 22:59:42
问题 Consider the following code: String commandf = \"ls /etc | grep release\"; try { // Execute the command and wait for it to complete Process child = Runtime.getRuntime().exec(commandf); child.waitFor(); // Print the first 16 bytes of its output InputStream i = child.getInputStream(); byte[] b = new byte[16]; i.read(b, 0, b.length); System.out.println(new String(b)); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } The program\'s output is: /etc: adduser.co When I run from the