Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling an executable

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Does anyone know how I can call an executable in Java? The corresponding thing in c++ is _execl.
thanks
 
Brent,

java.lang.Runtime.getRuntime().exec(...);

There are four forms of exec()

Hope this helps
-pete
 
here's an example. it prints the name of your computer. put any .exe into "cmd" but things like dir and set don't seem to work:

try{
String cmd = "hostname";
Process pr = Runtime.getRuntime().exec(cmd);
InputStream is = pr.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while( (line = br.readLine()) != null ){
System.out.println(line);
}
br.close();

}catch(Exception e){System.out.print("\nError :"+e);}
 
Donald, please see my post above. The article explains that runtime exec is not a command line (even though it takes a parameter named cmd!). Things like 'dir' are operating system specific, therefore you would have to test whether you are on windows then run the Windows command.exe to execute Windows commands.

The article also explains that there can be problems if you don't empty the buffers of the exec, e.g. if your exec failed and produced an error message it might fill up the buffer and throw a bigger error in your Java. My Home -->
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top