Reading ENV vars in Solaris - Indirectly - help!

Discussion in 'Java' started by Wells, May 16, 2004.

  1. Wells

    Wells Guest

    Hello all,

    I know there is no direct way to read Solaris env variables using JDK 1.4.2
    but I think I should be able to use something like what's below to get at
    them indirectly. The problem is that I always get a null myvar. Can anyone
    help me?
    Thanks so much.
    Wells...

    ---------------------------
    Process p = Runtime.getRuntime().exec("sh -l echo $JAVA_HOME");
    BufferedReader br = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    String myvar = br.readLine();
    System.out.println(myvar);
    ------------------------------

    PS: This works on windows if I do:
    Process p = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%");

    PPS: This is a JSP/Servlets web app deployed on Oracle 9iAS. And since I
    am running in multiple environments (i.e. dev1, dev1, test, prod), I never
    know where I'll be running so there is no way to preset the required env
    vars.
     
    Wells, May 16, 2004
    #1
    1. Advertising

  2. Wells wrote:

    ....
    > Process p = Runtime.getRuntime().exec("sh -l echo $JAVA_HOME");

    ....
    > Process p = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%");

    ....
    > PPS: This is a JSP/Servlets web app deployed on Oracle 9iAS. And since I
    > am running in multiple environments (i.e. dev1, dev1, test, prod), I never
    > know where I'll be running so there is no way to preset the required env
    > vars.


    You can obtain java.home value as well as many other useful info
    (os.name, for example) from System.getProperties()
     
    Mykola Rabchevskiy, May 16, 2004
    #2
    1. Advertising

  3. Wells

    Wells Guest

    Yes thank you, I'm aware of that.
    My bad though, I should've said that I want other env variables. I simply
    used JAVA_HOME as an example. What I really want are specific env vars we
    set according to the environment we are running. For example, RPTS_DIR is
    an env var representing a reports directory. It would have a different path
    depending on the environment we are running and it's UNIX env vars like that
    I need to get which are not available through System.getProperties().

    Thanks,
    Wells...

    "Mykola Rabchevskiy" <> wrote in message
    news:lvQpc.19785$...
    > Wells wrote:
    >
    > ...
    > > Process p = Runtime.getRuntime().exec("sh -l echo $JAVA_HOME");

    > ...
    > > Process p = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%");

    > ...
    > > PPS: This is a JSP/Servlets web app deployed on Oracle 9iAS. And since

    I
    > > am running in multiple environments (i.e. dev1, dev1, test, prod), I

    never
    > > know where I'll be running so there is no way to preset the required env
    > > vars.

    >
    > You can obtain java.home value as well as many other useful info
    > (os.name, for example) from System.getProperties()
     
    Wells, May 17, 2004
    #3
  4. Wells

    Wells Guest

    Yes thank you, I'm aware of that.
    My bad though, I should've said that I want other env variables. I simply
    used JAVA_HOME as an example. What I really want are specific env vars we
    set according to the environment we are running. For example, RPTS_DIR is
    an env var representing a reports directory. It would have a different path
    depending on the environment we are running and it's UNIX env vars like that
    I need to get which are not available through System.getProperties().

    Thanks,
    Wells...

    "Mykola Rabchevskiy" <> wrote in message
    news:lvQpc.19785$...
    > Wells wrote:
    >
    > ...
    > > Process p = Runtime.getRuntime().exec("sh -l echo $JAVA_HOME");

    > ...
    > > Process p = Runtime.getRuntime().exec("cmd.exe /c echo %JAVA_HOME%");

    > ...
    > > PPS: This is a JSP/Servlets web app deployed on Oracle 9iAS. And since

    I
    > > am running in multiple environments (i.e. dev1, dev1, test, prod), I

    never
    > > know where I'll be running so there is no way to preset the required env
    > > vars.

    >
    > You can obtain java.home value as well as many other useful info
    > (os.name, for example) from System.getProperties()
     
    Wells, May 17, 2004
    #4
  5. On Sun, 16 May 2004 20:24:23 GMT, Wells wrote:
    > I know there is no direct way to read Solaris env variables using
    > JDK 1.4.2 but I think I should be able to use something like what's
    > below to get at them indirectly. The problem is that I always get a
    > null myvar. Can anyone help me?


    [...]

    > Process p = Runtime.getRuntime().exec("sh -l echo $JAVA_HOME");


    Express the command like this instead:

    String[] cmd = {"/bin/sh", "-c", "echo $JAVA_HOME"};
    Process p = Runtime.getRuntime().exec(cmd);

    This version of exec() ensures that the arguments are grouped the way
    sh expects them.

    Of course JAVA_HOME is a poor example of reading from the shell
    environment, since you can read the System property "java.home" in
    pure Java.

    /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 17, 2004
    #5
  6. Wells wrote:
    > Yes thank you, I'm aware of that.
    > My bad though, I should've said that I want other env variables. I simply
    > used JAVA_HOME as an example. What I really want are specific env vars we
    > set according to the environment we are running. For example, RPTS_DIR is
    > an env var representing a reports directory. It would have a different path
    > depending on the environment we are running and it's UNIX env vars like that
    > I need to get which are not available through System.getProperties().


    (a) Do not top-post.

    (b) Many consider it bad style to provide application configuration via
    environment variables. Consider using a dot-rc file (the Properties
    object is nice here, too), the Java preference API and/or command line
    options.

    If youn really want to do it via environment variables:

    (c) Consider using a wrapper and the -D switch on the command line to
    pass your environment variables to the VM. Especially if you know the
    names of the variables in advance.

    (d) If you can live with some risk (newlines in environment variables
    will break the parsing), read all variables into a Properties object:

    Properties environment = new Properties();
    environment.load(Runtime.getRuntime().exec("env").getInputStream());

    /Thomas
     
    Thomas Weidenfeller, May 17, 2004
    #6
    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. __jakal__

    reading env variables in solaris

    __jakal__, May 7, 2004, in forum: Java
    Replies:
    6
    Views:
    1,248
    Roedy Green
    May 7, 2004
  2. RN

    env vars in log4j

    RN, Jul 27, 2004, in forum: Java
    Replies:
    0
    Views:
    413
  3. Ankit Mehta
    Replies:
    1
    Views:
    1,565
    Simon Brooke
    Sep 29, 2006
  4. Ross
    Replies:
    4
    Views:
    387
    Lie Ryan
    Oct 1, 2008
  5. TDR
    Replies:
    3
    Views:
    194
    Daniel Berger
    Aug 31, 2007
Loading...

Share This Page