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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running non-java programs from a java app

Status
Not open for further replies.

JNich

Programmer
Mar 4, 1999
3
US
How can I execute a C program (or any executable, for that matter) from within a java application. I want to run non-dll stuff.
 
Try this (or see in API):<br>
String[] cm=new String[2];<br>
cm[0]="C.EXE";<br>
cm[1]="Parms to exe, if needed";<br>
Process p=Runtime.getRuntime().exec(cm);<br>
<br>
if you want to wait the end of the process<br>
try {<br>
p.waitFor();<br>
} catch ...<br>

 
Hmmm, I still get a java.io.IOException: CreateProcess: dir *.jav error=0<br>
<br>
Any ideas?
 
If you want to execute OS inner commands (like dir, copy, etc.) you must execute the command.com (in Win9x) or cmd.exe (in WinNT).<br>
For example: "command /c dir *.java" or "cmd /c dir *.java".<br>
The /c means that no permanent OS loaded, only one command and exit.<br>

 
Runtime rt = Runtime.getRuntime();
try
{
Process extProcess = rt.exec(&quot;Executable.exe /p&quot; + filename);
extProcess.waitFor();
// receive the output of external program
InputStream extInput = extProcess.getInputStream();
int bytesAvailable = extInput.available();
if (bytesAvailable > 0)
{
byte[] extInputData = new byte[bytesAvailable];
extInput.read(extInputData);
this.setOutcome(new String(extInputData));
}
}
catch (Exception e)
{

}
Seth Juarez
Web Consultant
Holman's of Nevada
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top