This post originated from an RSS feed registered with Java Buzz
by Weiqi Gao.
Original Post: Where Is My java.home?
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
From time to time, I need to find out what my JVM thinks a certain system property is set. For example, the installation instructions of some software packages would say something like "put this file in the lib subdirectory of what your JVM thinks is java.home."
What I would usually do is to write a throw away Java program that prints out the said property. And of course you get tired of doing this tedious chore after a while. That's when you write a little script to do the work.
Here's my little script. It's call jenv, and it is written in the BeanShell scripting language:
[weiqi@gao]$ cat jenv
#!/usr/bin/env bsh
// Get all the system properties
props = System.getProperties();
if (bsh.args.length == 0) {
// print them all
for (name : props.propertyNames()) {
print(name + "=" + props.getProperty(name));
}
} else {
outer_for:
for (name : props.propertyNames()) {
for (regex : bsh.args) {
if (name.matches(".*" + regex + ".*")) {
print(name + "=" + props.getProperty(name));
break outer_for;
}
}
}
}
It can be run anywhere a bash shell is available (Cygwin on Windows or Linux.) When this script is run bash delegates to another shell script called bsh passing the path name of the script file and any arguments. On Linux, my bsh script contains only the line:
java bsh.Interpreter "$@"
In Cygwin, since the file name passed to bsh is in Cygwin format, which Java doesn't understand, my bsh script has to do a little bit more:
#!/bin/bash
#
# Run the BeanShell command line interpreter
#
declare -a args
declare -i index
until [ -z "$1" ]
do
if echo $1 | egrep -q '^/|^\.'
then
args[index]=`cygpath -a -w $1`
else
args[index]=$1
fi
((index++))
shift
done
java bsh.Interpreter ${args[@]}
The weird looking loop is there to straighten out any Cygwin path names to the Windows format so that Java can understand them. It's stolen from another script I've written.
Now that I have the scripts, finding out the values of system properties is as easy as finding out the values of environment variables. The command jenv by itself print out all system properties. If any command line arguments are supplied, they are used as regular expressions to filter the system properties. Only system properties whose name contains a substring that matches one of the regular expressions are printed.
[weiqi@gao]$ jenv user.home
user.home=C:\Documents and Settings\gao_w
[weiqi@gao]$ jenv user home
user.country=US
user.dir=u:\bin
user.variant=
user.home=C:\Documents and Settings\gao_w
user.timezone=
user.name=gaow
java.home=C:\Program Files\Java\jre1.5.0_07
user.language=en