Java Reference
In-Depth Information
$ java -DHOME=/home/mydir -DALT=other -DETC="so forth" AllEnv
Instead of typing those values, you'd probably want to let the Linux shell
put in the values from its environment. So you'd use shell variables, for
example:
$ java -DHOME="${HOME}" -DALT="${ALT}" -DETC="${ETC}" AllEnv
assuming that HOME , ALT , and ETC have already been defined in the shell's
environment. 2
If there are only a few variables that you need to pass to Java, put them on
the command line as shown above. Put that command line into a shell script
and use the script to invoke the program so that the parameters are supplied
every time.
But if you want to access many or all of the environment variables then
you may want to do something a little more complex. Notice the syntax of the
output of the env command. It is in the same format ( name=value ) as are
properties. So if we use a shell script to invoke our program, we can have it
place all these values into a file by redirecting output, then open this file as a
Java properties file and thus make all the name/value pairs accessible.
The following commands in a shell script attempt to do just that:
env > /tmp/$$.env
java -DENVFILE=/tmp/$$.env MyClass
rm /tmp/$$.env
where MyClass is the Java program that you wish to run.
TIP
The shell variable $$ is the numeric process ID of the running process. This
provides a unique ID during each invocation of the program. Each run of the
script will have its own process and thus its own process ID. Thus a single user
could execute this script multiple times concurrently without fear of collision
with himself or others.
2. The quotations around the shell variables keep any embedded spaces as part of the variable's
value. The curly braces are not strictly necessary in this use.
Search WWH ::




Custom Search