Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Runtime.exec() with env and working directory parameters is not working.

Reply
Thread Tools

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

 
 
Priyanka AGARWAL
Guest
Posts: n/a
 
      05-24-2004
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.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      05-24-2004
On 23 May 2004 23:49:45 -0700, (Priyanka AGARWAL)
wrote or quoted :

>String sz_execute_cmd = sz_Samcef_exe + " ba,as iteration n 1 <
>samcef_input.dat";


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??

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Rajesh Tihari
Guest
Posts: n/a
 
      05-24-2004
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


(Priyanka AGARWAL) wrote in message news:< om>...
> 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.

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-24-2004
On 23 May 2004 23:49:45 -0700, Priyanka AGARWAL wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Priyanka AGARWAL
Guest
Posts: n/a
 
      05-24-2004
Roedy Green <look-> wrote in message news:<>. ..
> On 23 May 2004 23:49:45 -0700, (Priyanka AGARWAL)
> wrote or quoted :
>
> >String sz_execute_cmd = sz_Samcef_exe + " ba,as iteration n 1 <
> >samcef_input.dat";

>
> 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-25-2004
On 24 May 2004 07:28:15 -0700, (Priyanka AGARWAL)
wrote or quoted :

>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


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Priyanka AGARWAL
Guest
Posts: n/a
 
      05-25-2004
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 <> wrote in message news:<40b1dec3$>...
> On 23 May 2004 23:49:45 -0700, Priyanka AGARWAL wrote:
> > 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

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-25-2004
On 24 May 2004 21:47:37 -0700, Priyanka AGARWAL wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Priyanka AGARWAL
Guest
Posts: n/a
 
      05-25-2004
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.??

Gordon Beaton <> wrote in message news:<40b2e966$>...
> On 24 May 2004 21:47:37 -0700, Priyanka AGARWAL wrote:
> > 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

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-25-2004
On 25 May 2004 06:51:31 -0700, Priyanka AGARWAL wrote:
> 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/ja...rds/index.html
http://developer.netscape.com/docs/t...codestyle.html
http://membres.lycos.fr/beust/naming/
http://directory.google.com/Top/Comp...ing_Standards/

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with accessing directory defined in ENV variables Thomas Luedeke Ruby 7 08-12-2008 05:01 PM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
ENV['LD_LIBRARY_PATH'] not changing env TDR Ruby 3 08-31-2007 06:01 PM
SAXException: Attribute "xmlns:env" was already specified for element "env:Envelope' Ankit Mehta Java 1 09-29-2006 09:24 PM
Virtual directory error: "directory does not exist or is not accessible because of security settings" Dave ASP .Net 1 10-24-2005 02:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57