How can I run a C++ executable "exerun" placed on Desktop from my Java Code in Linux. I found alot about Runtime but I cannot grab the concept clearly..Kindly Clear this out for me....
-
2You may want to start by learning how to run *any* executable from a java vm. C++ has nothing to do with this problem. – WhozCraig Sep 04 '13 at 23:52
-
1When you know that you need a class like `Runtime`, you should look up [that class in the Javadocs](http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html) in order to read about its functionality: `Runtime.getRuntime().exec("exerun.exe")` – FThompson Sep 04 '13 at 23:53
3 Answers
One should use ProcessBuilder
instead of Runtime
Process p = new ProcessBuilder("myCommand", "myArg").start();
You can get the inputs stream, input error stream, and the output stream from p
.
Then you can redirect the output of the process and provide it with input through java.
You should also take a look at this question. It shows you how to set the working directory.
You could try the exec()
following the example below:
Runtime.getRuntime().exec("c:\\path_to_the_file\\myfile.exe", null, new File("c:\\path_of_the_folder");
Here, you find 3 parameters:
- Location of your .exe
- The second parameter can be
null
- The path of the directory with the *.exe file.

- 762
- 2
- 14
- 36

- 4,643
- 8
- 35
- 49
-
How do you get a `c:\\path_to_the_file\\myfile.exe` directory structure on Linux? – blgt Sep 05 '13 at 00:00
Basically, you need to use Runtime.getRuntime().exec(command)
, where command
is the command you want to execute. This will return a Process
object (say myProcess
).
If your program produces output, you will need to continually read the streams you can retrieve with myProcess.getInputStream()
and myProcess.getErrorStream()
. They correspond to the process's stdout
and stderr
, respectively.
If myProcess
produces output and you don't read it from the Java program, myProcess
will lockout after a while.

- 7,651
- 27
- 37