13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

108Chapter 4Where Am I? Executi<strong>on</strong> C<strong>on</strong>text$ java -DHOME=/home/mydir -DALT=other -DETC="so forth" AllEnvInstead of typing those values, you’d probably want to let the <strong>Linux</strong> shellput in the values from its envir<strong>on</strong>ment. So you’d use shell variables, forexample:$ java -DHOME="${HOME}" -DALT="${ALT}" -DETC="${ETC}" AllEnvassuming that HOME, ALT, and ETC have already been defined in the shell’senvir<strong>on</strong>ment. 2If there are <strong>on</strong>ly a few variables that you need to pass to Java, put them <strong>on</strong>the command line as shown above. Put that command line into a shell scriptand use the script to invoke the program so that the parameters are suppliedevery time.But if you want to access many or all of the envir<strong>on</strong>ment variables thenyou may want to do something a little more complex. Notice the syntax of theoutput of the env command. It is in the same format (name=value) as areproperties. So if we use a shell script to invoke our program, we can have itplace all these values into a file by redirecting output, then open this file as aJava 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/$$.envjava -DENVFILE=/tmp/$$.env MyClassrm /tmp/$$.envwhere MyClass is the Java program that you wish to run.TIPThe shell variable $$ is the numeric process ID of the running process. Thisprovides a unique ID during each invocati<strong>on</strong> of the program. Each run of thescript will have its own process and thus its own process ID. Thus a single usercould execute this script multiple times c<strong>on</strong>currently without fear of collisi<strong>on</strong>with himself or others.2. The quotati<strong>on</strong>s around the shell variables keep any embedded spaces as part of the variable’svalue. The curly braces are not strictly necessary in this use.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!