Reading ENV vars in Solaris - Indirectly - help!

W

Wells

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.
 
M

Mykola Rabchevskiy

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()
 
W

Wells

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...
 
W

Wells

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...
 
G

Gordon Beaton

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
 
T

Thomas Weidenfeller

Wells said:
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
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top