I have a problem with Runtime.exec() on Unix.
In my program I am calling an external application using java's
runtime.exec().
Following is the code::
String execute_cmd = "";
FileOutputStream samcef_input = new
FileOutputStream("samcef_input.dat");
PrintWriter pw = new PrintWriter(samcef_input);
pw.println("input.asef \"iteration.dat\"");
pw.println(".FIN 1");
pw.close();
samcef_input.close();
String working_directory = "/home/abc/xxx";
String[] env = null;
execute_cmd = "/snecma/Samv91/samcef ba,as iteration n 1 <
samcef_input.dat";
wait = Runtime.getRuntime().exec(execute_cmd , env , new
File(working_directory));
// any error message?
errorGobbler = new StreamGobbler(wait.getErrorStream
(), "ERROR");
// any output?
outputGobbler = new StreamGobbler(wait.getInputStream
(), "OUTPUT"
);
// kick them off
errorGobbler.start();
outputGobbler.start();
wait.waitFor(); // wait till the process is completed
My external application uses some environment variables.If I give env
as null , it works fine on windows.
but doesnt work on unix.
I want to use the same code for both windows and unix.
But it works fine on unix if write the cmd to call the external
application in a shell script and call that shell script instead.
But this is not a good workaround.
This is the way using a shell script :
----------------------------------------------------------------------
------------------------------
FileOutputStream out= new FileOutputStream("samcef.ksh");
PrintWriter pw1 = new PrintWriter(out);
pw1.println("/snecma/Samv91/samcef ba,as iteration n 1 <
samcef_input.dat");
pw1.close();
out.close();
String working_directory = "/home/aaa/xxx";
sz_execute_cmd = "chmod 777 " + sz_working_directory +
java.io.File.separator + "samcef.ksh";
Process wait = Runtime.getRuntime().exec(sz_execute_cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(wait.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(wait.getInputStream(), "OUTPUT" );
// kick them off
errorGobbler.start();
outputGobbler.start();
String[] env = null;
execute_cmd = "./samcef.ksh";
wait = Runtime.getRuntime().exec(execute_cmd , env , new
File(working_directory));
// any error message?
errorGobbler = new StreamGobbler(wait.getErrorStream
(), "ERROR");
// any output?
outputGobbler = new StreamGobbler(wait.getInputStream
(), "OUTPUT"
);
// kick them off
errorGobbler.start();
outputGobbler.start();
wait.waitFor(); // wait till the process is completed
Can anyone tell me what could be the reason.Why doesnt runtime.exec()
take the environment settings in unix.
Is there a workaround for this problem.