Finding Java Home path via command line

N

Nigel

Hi,

Without manually adding enviornment variables, Is there a way I can
find the home directory of Java via the command line? (i.e: C:\Program
Files\Java\jre1.6.0)

I know in using Java API I can find the home path by performing
System.getProperty("java.home"), but I would like to be able to run a
Perl script that can parse command line output and grab the Java home
path.

Not sure if I'm asking for a miracle or not... :)
Any help would be appreciated.

Thanks.
- Nigel
 
R

Richard Reynolds

Nigel said:
Hi,

Without manually adding enviornment variables, Is there a way I can
find the home directory of Java via the command line? (i.e: C:\Program
Files\Java\jre1.6.0)

I know in using Java API I can find the home path by performing
System.getProperty("java.home"), but I would like to be able to run a
Perl script that can parse command line output and grab the Java home
path.

Not sure if I'm asking for a miracle or not... :)
Any help would be appreciated.

Thanks.
- Nigel
not sure what you mean, JAVA_HOME __is__ an environment variable, if you
mean the java install dir that would be used if you just typed "java" then
that would be determined by your PATH environment variable. Either way
they're just env vars that have to be set up and can be accessed normally
e.g. $PATH or %PATH%
 
L

Lew

Richard said:
not sure what you mean, JAVA_HOME __is__ an environment variable, if you
mean the java install dir that would be used if you just typed "java" then
that would be determined by your PATH environment variable. Either way
they're just env vars that have to be set up and can be accessed normally
e.g. $PATH or %PATH%

#!/bin/bash
$ jh=$(dirname `which java`)
$ echo $jh
/usr/java/java/bin
$
 
R

Richard Reynolds

Lew said:
#!/bin/bash
$ jh=$(dirname `which java`)
$ echo $jh
/usr/java/java/bin
$

'which' will give you the java executable though, and dirname the bin dir,
you'd still have to work out the home dir, also, there may be symlinks
involved. Is there a "which" for windows?
 
P

printdude1968

Hi,

Without manually adding enviornment variables, Is there a way I can
find the home directory of Java via the command line? (i.e: C:\Program
Files\Java\jre1.6.0)

I know in using Java API I can find the home path by performing
System.getProperty("java.home"), but I would like to be able to run a
Perl script that can parse command line output and grab the Java home
path.

Not sure if I'm asking for a miracle or not... :)
Any help would be appreciated.

Thanks.
- Nigel

If you type "set" from the command line you will get a listing of all
the env vars that are set.
If I were working on a win machine using ksh or some other *nix build,
I would simply do
% set | grep "JAVA_HOME"
and use the the results of that.

$ set | grep "JAVA_HOME"
JAVA_HOME='/dev/fs/C/Program Files/Java/jdk1.6.0'

The same basic command would work for a real *nix installation.
My understanding is that you don't always need to set a JAVA_HOME
variable which
would mean that you need to parse the "echo %PATH%" results and try to
get it from there.

J:\>echo %PATH%
C:\Tcl\bin;C:\Python25\;J:\apache-ant-1.7.0\bin;C:\oraclexe\app\oracle
\product\1
0.2.0\server\bin;c:\ruby\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS
\System32\
Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program
Files\QuickT
ime\QTSystem\;C:\SFU\common\;C:\Program Files\Subversion\bin;C:
\Program Files\My
SQL\MySQL Server 4.1\bin;C:\Program Files\Common Files\GTK\2.0\bin;C:
\Program Fi
les\Java\jdk1.6.0\bin;C:\Sun\jwsdp-2.0\jwsdp-shared\bin

You should be able to traverse the output until you find a jdk or jre
on the PATH and
pull out the contents of JAVA_HOME from there.

I'm not a DOS guru so I've no idea how to do this.

It's much easier if the JAVA_HOME variable in DOS is already set.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Nigel said:
Without manually adding enviornment variables, Is there a way I can
find the home directory of Java via the command line? (i.e: C:\Program
Files\Java\jre1.6.0)

I know in using Java API I can find the home path by performing
System.getProperty("java.home"), but I would like to be able to run a
Perl script that can parse command line output and grab the Java home
path.

Not sure if I'm asking for a miracle or not... :)

What do you want on a system with more than one Java version
installed ?

JAVA_HOME is used to give the run script the capability to specify
which Java version to use.

Arne
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top