Runtime.exec() with env and working directory parameters is not working.

P

Priyanka AGARWAL

I have a problem with Runtime.exec command when I want to execute it
in the working directory.
My Runtime.exec command calls an external Application(Samcef) which
uses certain environment variables
and licence for calling the external application.

I have used this command:-
.............
.............

String[] sz_env = {""};
String sz_working_directory = D:\\users
String sz_execute_cmd = sz_Samcef_exe + " ba,as iteration n 1 <
samcef_input.dat";

Process wait = Runtime.getRuntime().exec(sz_exec_command, sz_env, new
File(sz_working_directory));
.............
.............

It does'nt take the existing environment settings of the operating
system.

But if I use this command from command prompt it works fine.
If I use cmd /c ,go to the working directory using cd then it works
fine.
But I dont want to use cmd/c , I want it to work both on windows and
unix.
 
R

Rajesh Tihari

Why dont you try and invoke the JVM with the environment settings in
the classpath, i.e., -DCLASSPATH=add your environment settings here
after the classpath.

I do not guarantee that it will work but it is worth a try.

Cheers
Rajesh
 
G

Gordon Beaton

String[] sz_env = {""};
String sz_working_directory = D:\\users
String sz_execute_cmd = sz_Samcef_exe + " ba,as iteration n 1 <
samcef_input.dat";

Process wait = Runtime.getRuntime().exec(sz_exec_command, sz_env,
new File(sz_working_directory));
[...]

It does'nt take the existing environment settings of the operating
system.

If you want the child process to inherit environment variable settings
from the parent, pass null as the environment when you call
Runtime.exec(). In your example you have passed an *empty*
environment, which isn't the same thing.

If you want to redirect the stdin of the child process, you need to
start a shell and pass the complete command line to the shell,
something like this:

String cmd = { "/bin/sh", "-c", "myprog < input.dat" };

BTW why do you use such horrible names for your variables?

/gordon
 
P

Priyanka AGARWAL

Roedy Green said:
see http://mindprod.com/jgloss/exec.html


sz_Samcef_exe has no idea what to do with the < pipe command when
passed to it as a parameter. A command interpreter of some kind is
needed to act on it.

what kind of file name is sz_Samcef_exe??
........................
........................
sz_Samcef_exe is the exe path for samcef application.
We invoke Samcef by specifying the exe path and the parameters samcef
takes.

For example:-
To invoke notepad:-
You specify the notepad.exe and the <filename> so that it opens that
file.

But the problem with samcef is that it uses envirionment variables
internally.
For Example:-
SAM_EXE,SAM_LANG etc...........

I have tried to use these environment variables:-
String sz_env = {"SAM_EXE=D:\tools\samcef\exec",
"SAM_LANG=vfr"};

Process wait = Runtime.getRuntime().exec(sz_exec_command, sz_env, new
File(sz_working_directory));

But still it has the licence problem.
 
R

Roedy Green

sz_Samcef_exe is the exe path for samcef application.
We invoke Samcef by specifying the exe path and the parameters samcef
takes.

If you were coding this in windows, you would have to say
%sz_Samcef_exe%

And you would could not exec it directly. You would have to exec a
command processor which understands % replacement. Something similar
likely applies to your OS.

See http://mindprod.com/jgloss/exec.html
 
P

Priyanka AGARWAL

Hey Thanks a lot. Its working using null as the environment.
Can you please tell me what kind of variable names do you use?

Priyanka.

Gordon Beaton said:
String[] sz_env = {""};
String sz_working_directory = D:\\users
String sz_execute_cmd = sz_Samcef_exe + " ba,as iteration n 1 <
samcef_input.dat";

Process wait = Runtime.getRuntime().exec(sz_exec_command, sz_env,
new File(sz_working_directory));
[...]

It does'nt take the existing environment settings of the operating
system.

If you want the child process to inherit environment variable settings
from the parent, pass null as the environment when you call
Runtime.exec(). In your example you have passed an *empty*
environment, which isn't the same thing.

If you want to redirect the stdin of the child process, you need to
start a shell and pass the complete command line to the shell,
something like this:

String cmd = { "/bin/sh", "-c", "myprog < input.dat" };

BTW why do you use such horrible names for your variables?

/gordon
 
G

Gordon Beaton

Hey Thanks a lot. Its working using null as the environment.
Can you please tell me what kind of variable names do you use?

For one thing, I avoid pseudo-Hungarian like the plague. Note too that
"sz" isn't even correct for Java Strings, which aren't zero terminated
as far as the application programmer is concerned. And in Java, the
convention is to use capitals to separate "words" in variable names,
without underscores.

/gordon
 
P

Priyanka AGARWAL

Thanks.Ya you are true,we were using C style naming convention.

So my sz_working_directoy becomes workingDirectory .
Is it correct. But how do we differentiate between different data types.
What do we do for integers.??
 
G

Gordon Beaton

Thanks.Ya you are true,we were using C style naming convention.

So my sz_working_directoy becomes workingDirectory . Is it correct.
But how do we differentiate between different data types. What do we
do for integers.??

I have never been a fan of naming systems that attempt to encode
information about the data type in the name of each variable, and in
fact I don't believe that it is necessary to do so.

Do you not already know that something like "wordCount" is an integer,
or "fileName" is a String? These are admittedly simple examples, but
simply knowing that a variable contains an integer doesn't prevent you
from doing silly things like adding "wheelDiameter" to "daysInMonth",
and expecting to get a meaningful result.

I suggest that you search this group for things like "hungarian
notation", "naming conventions" or similar terms, to see what others
have written. The topic comes up occasionally and usually leads to
much discussion (as I am sure it will here too).

Also have a look at some of the Java style guides that are available.
Here are a few links, in no particular order:

http://java.sun.com/docs/codeconv/
http://gee.cs.oswego.edu/dl/html/javaCodingStd.html
http://www.chimu.com/publications/javaStandards/index.html
http://developer.netscape.com/docs/technote/java/codestyle.html
http://membres.lycos.fr/beust/naming/
http://directory.google.com/Top/Computers/Programming/Languages/Java/Coding_Standards/

/gordon
 

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