Finding Java Home path via command line

Discussion in 'Java' started by Nigel, May 1, 2007.

  1. Nigel

    Nigel Guest

    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
     
    Nigel, May 1, 2007
    #1
    1. Advertising

  2. "Nigel" <> wrote in message
    news:...
    > 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%
     
    Richard Reynolds, May 1, 2007
    #2
    1. Advertising

  3. Nigel

    Real Gagnon Guest

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


    You can use the regedit utilty`to dump the JRE location installed on your
    workstation and then parse the output.

    Example at :
    http://www.rgagnon.com/javadetails/java-0525.html

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Java, Javascript, VBScript and PowerBuilder code snippets
    * http://www.rgagnon.com/howto.html
    * http://www.rgagnon.com/bigindex.html
     
    Real Gagnon, May 1, 2007
    #3
  4. Nigel

    Lew Guest

    Richard Reynolds wrote:
    > "Nigel" <> wrote in message
    > news:...
    >> 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%


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

    --
    Lew
     
    Lew, May 1, 2007
    #4
  5. "Lew" <> wrote in message
    news:...
    > Richard Reynolds wrote:
    >> "Nigel" <> wrote in message
    >> news:...
    >>> 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%

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


    '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?
     
    Richard Reynolds, May 2, 2007
    #5
  6. Nigel

    Guest

    On May 1, 2:51 pm, Nigel <> wrote:
    > 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.
     
    , May 2, 2007
    #6
  7. Nigel wrote:
    > 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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, May 2, 2007
    #7
  8. Nigel

    Lew Guest

    Richard Reynolds wrote:
    > '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?


    <http://www.cygwin.com/>

    --
    Lew
     
    Lew, May 2, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    16,252
    Thomas Weidenfeller
    Mar 10, 2006
  2. Cameron McCormack
    Replies:
    0
    Views:
    369
    Cameron McCormack
    Aug 27, 2007
  3. nancyflorida
    Replies:
    0
    Views:
    336
    nancyflorida
    Nov 12, 2007
  4. nancyflorida
    Replies:
    0
    Views:
    320
    nancyflorida
    Nov 12, 2007
  5. * Its my Pleasure *
    Replies:
    0
    Views:
    381
    * Its my Pleasure *
    Feb 20, 2008
Loading...

Share This Page