Runtime.exec() for running external applications doesnt work with unix

P

Priyanka AGARWAL

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.

Thanks
Priyanka
 
J

JScoobyCed

Priyanka said:
I have a problem with Runtime.exec() on Unix.
In my program I am calling an external application using java's
runtime.exec().
execute_cmd = "/snecma/Samv91/samcef ba,as iteration n 1 <
samcef_input.dat";

I might be wrong, but the command you have uses a redirection '<'
I am not sure that unix/linux likes this if you are not in a shell. Try
to use '/usr/sbin/ksh /snecma/Samv91/samcef ba,as iteration n 1 <
samcef_input.dat' and see if you have more success.
 
P

Priyanka AGARWAL

JScoobyCed said:
I might be wrong, but the command you have uses a redirection '<'
I am not sure that unix/linux likes this if you are not in a shell. Try
to use '/usr/sbin/ksh /snecma/Samv91/samcef ba,as iteration n 1 <
samcef_input.dat' and see if you have more success.

Hi,
I have already mentioned that it works fine if I run a shell script
, but I dont want to create a shell script and execute the command.Is
there a solution?

Priyanka
 
T

Thomas Schodt

Priyanka said:
I have already mentioned that it works fine if I run a shell script,
but I dont want to create a shell script and execute the command.Is
there a solution?

Where does the post by JScoobyCed suggest a shell script? ... it just
says run a shell and give it those arguments.
 
P

Priyanka AGARWAL

Thomas Schodt said:
Where does the post by JScoobyCed suggest a shell script? ... it just
says run a shell and give it those arguments.

Hi,
I have tried this too, it doensnt invoke a shell from runtime.exec().
What could be the problem??
Priyanka.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top