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

Discussion in 'Java' started by Priyanka AGARWAL, May 24, 2004.

  1. 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.
    Priyanka AGARWAL, May 24, 2004
    #1
    1. Advertising

  2. Priyanka AGARWAL

    Roedy Green Guest

    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.
    Roedy Green, May 24, 2004
    #2
    1. Advertising

  3. 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:<>...
    > 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.
    Rajesh Tihari, May 24, 2004
    #3
  4. 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
    Gordon Beaton, May 24, 2004
    #4
  5. Roedy Green <> 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.
    Priyanka AGARWAL, May 24, 2004
    #5
  6. Priyanka AGARWAL

    Roedy Green Guest

    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.
    Roedy Green, May 25, 2004
    #6
  7. 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
    Priyanka AGARWAL, May 25, 2004
    #7
  8. 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
    Gordon Beaton, May 25, 2004
    #8
  9. 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
    Priyanka AGARWAL, May 25, 2004
    #9
  10. 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/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

    --
    [ 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
    Gordon Beaton, May 25, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    668
    Eric Sosman
    Apr 24, 2006
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,073
    Gordon Beaton
    May 22, 2006
  3. Ankit Mehta
    Replies:
    1
    Views:
    1,376
    Simon Brooke
    Sep 29, 2006
  4. Replies:
    3
    Views:
    4,932
  5. TDR
    Replies:
    3
    Views:
    158
    Daniel Berger
    Aug 31, 2007
Loading...

Share This Page