0

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....

FThompson
  • 28,352
  • 13
  • 60
  • 93
  • 2
    You 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
  • 1
    When 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 Answers3

4

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.

Community
  • 1
  • 1
mike
  • 4,929
  • 4
  • 40
  • 80
3

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.
Harbinger
  • 762
  • 2
  • 14
  • 36
Alberto Miola
  • 4,643
  • 8
  • 35
  • 49
3

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.

Mario Rossi
  • 7,651
  • 27
  • 37