Questions tagged [runtime.exec]

The Runtime.exec() method allows Java apps. to create a new OS Process.

The Runtime.exec(...) method (which has 6 overloaded variants) allows Java apps. to create a new system (represented in Java as a Process).

There are common mistakes made when creating new processes - as detailed in When Runtime.exec() won't. The article is the first thing to check if a process fails. Implement all the tips, and even if doing so does not make the process work, it will provide much more detailed information on why it failed.

The ProcessBuilder class introduced in Java 1.5 makes it easier to create and use a process correctly. For example, the redirectErrorStream(boolean) convenience method will merge the System.out & System.err streams, which allows both streams to be consumed in one Thread.

881 questions
40
votes
4 answers

Runtime.exec().waitFor() doesn't wait until process is done

I have this code: File file = new File(path + "\\RunFromCode.bat"); file.createNewFile(); PrintWriter writer = new PrintWriter(file, "UTF-8"); for (int i = 0; i <= MAX; i++) { writer.println("@cd " + i); writer.println(NATIVE SYSTEM…
Maroun
  • 94,125
  • 30
  • 188
  • 241
36
votes
6 answers

Java Runtime.getRuntime().exec() alternatives

I have a collection of webapps that are running under tomcat. Tomcat is configured to have as much as 2 GB of memory using the -Xmx argument. Many of the webapps need to perform a task that ends up making use of the following code: Runtime runtime…
twilbrand
  • 1,330
  • 5
  • 19
  • 27
33
votes
8 answers

Running Bash commands in Java

I have the following class. It allows me to execute commands through java. public class ExecuteShellCommand { public String executeCommand(String command) { StringBuffer output = new StringBuffer(); Process p; try { p =…
mickzer
  • 5,958
  • 5
  • 34
  • 57
31
votes
5 answers

How to execute command with parameters?

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",…
Alex
  • 325
  • 1
  • 4
  • 5
26
votes
3 answers

how to compile & run java program in another java program?

I have a Main.java and Test.java classes that I want to compile and run Main.java in Test.java code. Here is my code Process pro1 = Runtime.getRuntime().exec("javac Main.java"); pro1.waitFor(); Process pro2 =…
user467871
26
votes
6 answers

Using Quotes within getRuntime().exec

I'd like to invoke bash using a string as input. Something like: sh -l -c "./foo" I'd like to do this from Java. Unfortunately, when I try to invoke the command using getRuntime().exec, I get the following error: foo": -c: line 0:…
Daniel
  • 23,365
  • 10
  • 36
  • 34
23
votes
4 answers

Redirect Runtime.getRuntime().exec() output with System.setOut();

I have a program Test.java: import java.io.*; public class Test { public static void main(String[] args) throws Exception { System.setOut(new PrintStream(new FileOutputStream("test.txt"))); System.out.println("HelloWorld1"); …
Leo Izen
  • 4,165
  • 7
  • 37
  • 56
23
votes
2 answers

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

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…
user2110167
  • 241
  • 1
  • 3
  • 7
23
votes
4 answers

Taking thread dumps in production

I am analyzing the differences between approaches for taking thread dumps. Below are the couple of them I am researching on Defining a jmx bean which triggers jstack through Runtime.exec() on clicking a declared bean operation. Daemon thread…
Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113
20
votes
5 answers

The right way to kill a process in Java

What's the best way to kill a process in Java ? Get the PID and then killing it with Runtime.exec() ? Use destroyForcibly() ? What's the difference between these two methods, and is there any others solutions ?
Kariamoss
  • 542
  • 1
  • 9
  • 29
20
votes
4 answers

Mock Runtime.getRuntime()?

Can anyone make any suggestions about how best to use EasyMock to expect a call to Runtime.getRuntime().exec(xxx)? I could move the call into a method in another class that implements an interface, but would rather not in an ideal world. interface…
Rich
  • 15,602
  • 15
  • 79
  • 126
19
votes
5 answers

How to get java getRuntime().exec() to run a command-line program with arguments?

I've been trying to write a java program that uses the Runtime.getRuntime().exec() method to use the command-line to run an instance of the program "tesseract". Some background, Tesseract is a free open source program that is used to perform OCR…
Samuel
  • 574
  • 3
  • 7
  • 22
18
votes
1 answer

Unable using Runtime.exec() to execute shell command "echo" in Android Java code

I can use Runtime.exec() to execute shell commands like "getprop" and "ls system" and they work fine. However, when I use "echo $BOOTCLASSPATH", "echo \\$BOOTCLASSPATH" or "echo HelloWorld", it won't show it in stdout. The logcat…
QY Lin
  • 467
  • 1
  • 5
  • 10
17
votes
4 answers

Java Runtime exec() fails to escape characters properly

This might already been answered before but that was regarding unicode and I don't think this is unicode (it's in ASCII so...). When I execute this in my terminal there is no problem what so ever vboxmanage setextradata "Test Machine"…
Tim Strijdhorst
  • 1,539
  • 3
  • 14
  • 29
17
votes
2 answers

Runtime.getRuntime().exec(cmd) hanging

I am executing a command which returns me the Revision number of a file; 'fileName'. But if there is some problem executing the command, then the application hangs up. What can I do to avoid that condition? Please find below my code. String cmd=…
user1688404
  • 729
  • 3
  • 9
  • 19
1
2
3
58 59