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!

Runtime.exec(a.exe) ... never this easy...

Status
Not open for further replies.

burnettben

Programmer
Apr 24, 2005
7
0
0
AU
Hi,

I am trying to run a C program from within a java program. However it always seems to not run. I have tried redirecting the output via streams to a file and various other suggestions made on this forum eg absolute path, using array to pass in executable but it appears to be finding the executable but then not running it. I can run it fine from the command line (i even wrote a helloworld to see if that ran ... it didn't) If anyone says anything wrong or has any suggestions pls reply asap as i am running out of ideas!!

Cheers Ben

File outputFile = new File(controlData.getFileName());

FileOutputStream fos = new FileOutputStream(outputFile);
String output;

//Start external program ....
String[] s = new String[]{"C:\\Documents and Settings\\Ben\\My Documents\\UNI\\ITN613\\viztool\\model\\a.exe"};

//String[] s = new String[]{"cmd /c start a.exe "};

modelProgram = Runtime.getRuntime().exec(s);
BufferedReader a_in = new BufferedReader(new InputStreamReader(modelProgram.getInputStream()));
while((output = a_in.readLine()) != null){
System.out.println("aaa");
fos.write(output.getBytes());
}
a_in.close();
fos.close();

//Block until process returns with status ( 0 = normal termination)

exitStatus = modelProgram.waitFor();

//Now if termination status normal should be a file in directory ..
if(exitStatus == 0 ){
//Do something with file
} else{
//Irregular termination ... always seems to end in here ... and no file has been produced.
System.out.println("Termination not normal do not import into database...");
}
 
Whats you error stack trace ?

Normally, you start a Win32 process through :

Code:
Process p = Runtime.getRuntime().exec("cmd /c C:/path/to/myprog.exe");
int iRetCode = p.waitFor();   // noticed you were not doing this - maybe you need to 

if (iRetCode != 0) {
  // handle error if prog should return 0 in success
}

InputStream is = p.getInputStream(); 
... etc ...

--------------------------------------------------
Free Database Connection Pooling Software
 
i do a Process.waitFor(); after the streams are set up to read the program response. I tried using cmd/ c a.exe but it fails also.
 
If i use cmd /c start a.exe a windows cmd window opens up but i can't execute anything in there. Could this be a permissions thing? Does my executing java program required more permissions?
 
what happens when you run the prog normally in a dos console ?

No, Java does need any more permissions that *normal* for running a programme.

--------------------------------------------------
Free Database Connection Pooling Software
 
I discovered that it dodn't run in a normal dos window. was missing a dll (ran in cygwin window).

Ended up calling it by using exec(a.exe);
Simple....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top