burnettben
Programmer
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...");
}
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...");
}