This discussion is archived
1 2 Previous Next 17 Replies Latest reply: May 15, 2009 9:39 AM by 807588 RSS

Automatically Set JAVA_HOME in a .bat File

807588 Newbie
Currently Being Moderated
Is there a way to automatically determine the current location of the JDK so that it can be used to set the JAVA_HOME environment variable?

Our project uses a very simple .BAT file to launch Ant on Micorosft Windows platforms.

Build.bat:
@echo off
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_07
set ANT_HOME=C:\Apps\Ant\apache-ant-1.7.1
call %ANT_HOME%\bin\ant build
pause
It works. However, the paths are hardcoded and the .bat file breaks every time a new verion of Java is released. I want to be able to set JAVA_HOME automatically.

If I type "*java -version*" at the command (DOS) prompt, I get information about the current instlation of Java. Presumably, that is using "*C:\WINDOWS\system32\java.exe*" and it knows the current version of Java. There has to be a way to do something similar to the Unix/Linux "*which java*" command.

Note that if Java is not yet installed on Windows, you get this:
C:\Documents and Settings\Dem>java -version
'java' is not recognized as an internal or external command,
operable program or batch file.
After installing the JDK, you get this:
C:\Documents and Settings\Dem>java -version
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)
  • 1. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    I am unaware of any MS Windows™ parallel to which.
    However, the paths are hardcoded and the .bat file breaks every time a new verion of Java is released. I want to be able to set JAVA_HOME automatically.
    It breaks when a new version of java is installed (nit picky I know but not the same thing).
    Anyway assuming you have control over the installation process, could you install the new java in the same location so that it would not change all the time?

    Or take John's simple lazy-man's approach.
    Let the command path handle it.
    I make sure that the jdk is on my command path an as long as I have only one JDK installed I never have to think too mucha about where. (until the next installation.)

    Are you sure that you need to set JAVA_HOME? As I remember ANT only needs it if you don't want it to use whatever JDK installation it finds (presumably via the PATH variable).

    Please also note that is possible that you have a JDK and a separate JRE installed so it is possible that these two produce different results or are at least referring to different installations.
    java - version
    javac -version
    So while I'm sure that there is something that you could put together do auto set it it might be easier to just depend on the comand path. That is unless you have some special requirements like having multiple JDKs installed or something.

    Edited by: johndjr on May 15, 2009 8:55 AM
    no need to quote the entire post
  • 2. Re: Automatically Set JAVA_HOME in a .bat File
    abillconsl Explorer
    Currently Being Moderated
    System.out.println(System.getProperty("java.home"));
    Should give you what you want. So, write a Java program that gets this value into a String (note: it will probably give you something like: "C:\Program Files\Java\jdk1.5.0_12\jre", so you can get the substring up till the last '\'). Then read your batch file and replace your JAVA_HOME with the new String value.

    ~Bill
  • 3. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    nevermind

    ===== BIG CORRECTION ==========
    My "ANT" doesn't need JAVA_HOME theory appears to be invalid.
    ================================

    Bill,
    Is there any way to have the bat file set the variable based on the output?
    some equivlent of shell script's
    JAVA_HOME=$(java -cp blah BillsClass)
    I don't know of any.

    But my track record today ain't so great.

    Edited by: johndjr on May 15, 2009 10:20 AM
    eating humble pie
  • 4. Re: Automatically Set JAVA_HOME in a .bat File
    abillconsl Explorer
    Currently Being Moderated
    John,

    I think what the OP wants is some way that the batch file can pick up a variable that exists in the registry, or as a System setting of some kind, that gets set when the new version of Java gets installed. That being the case, doing something like I suggested won't do it because the java pgm itself would need to be kicked off automatically on installation of the JDK. But there is another way. Having the Java pgm I suggested already, the batch file could be written so that it executes the class each time it itself is used - rather, another batch file calls the Java pgm (which writes a new batch file with the new JAVA_HOME hard coded in it), then calls the new batch file (the original batch file the OP already has.
  • 5. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    why not create a shortcut, point your .bat to the shortcut, and just update the shortcut to point to the new JDK when you make an install?
  • 6. Re: Automatically Set JAVA_HOME in a .bat File
    abillconsl Explorer
    Currently Being Moderated
    It's not automatic if it has to be done automatically; rather, it's about the same as just manually updating the batch file - unless I misunderstand you.

    But, alas ... it seems as though we're more interested in the solution than the OP ;-)
  • 7. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    Based on my trackrecord regarding windows programs today I should just keep silent, but where the fun in that?
    why not create a shortcut, point your .bat to the shortcut, and just update the shortcut to point to the new JDK when you make an install?
    I would not suggest that mainly because shortcuts don't work.
    Well maybe that is a bit strong but let mey say that as far as I ciould ever tell they only work for Windows Explorer.

    I could never get the command interpreter to "dereference" them.

    Try this.
    1. create a text file
    2 create shortcurt to it (you might want to rename it to something without spaces)
    3. try to type the file

    I just get some non text nonsense.

    Am I doing something wrong.
    I would love for shortcuts to have some value.
  • 8. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    abillconsl wrote:
    It's not automatic if it has to be done automatically; rather, it's about the same as just manually updating the batch file - unless I misunderstand you.

    But, alas ... it seems as though we're more interested in the solution than the OP ;-)
    Must be the Friday syndrome.
  • 9. Re: Automatically Set JAVA_HOME in a .bat File
    abillconsl Explorer
    Currently Being Moderated
    johndjr wrote:
    abillconsl wrote:
    It's not automatic if it has to be done automatically; rather, it's about the same as just manually updating the batch file - unless I misunderstand you.

    But, alas ... it seems as though we're more interested in the solution than the OP ;-)
    Must be the Friday syndrome.
    That said, the solution I offered in post #4 should certainly work, given of course, that I understand the OP ... who has perhaps disappeared?
  • 10. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    abillconsl wrote:
    That said, the solution I offered in post #4 should certainly work, given of course, that I understand the OP ... who has perhaps disappeared?
    I agree.
  • 11. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    A little bit of background may help here...

    The project is an open source project where I have no direct control over anyone's machine except my own. The project's wiki has simple but detailed step-by-step instructions for setting up SVN, building the project, and checking changes back into the repository.

    I'm fanatical about keeping things clean and straightforward.

    My open source rules:
    1) Don't depend on environmental settings (like the developer having JAVA_HOME properly set)
    2) Don't require developers to edit a file before the doing the first build
    3) Do work with any reasonably current versions of the platform components (OS, SVN, Ant, Java, etc.)
    4) Rely on the source/target attributes in build.xml for consistency (do not rely on environmental settings or specific versions of platform components)

    I like the System.getProperty("java.home") idea. It definitely has potential, but it is kind of a chicken and egg problem. If a more direct solution is not possible, I'll try to use it.
  • 12. Re: Automatically Set JAVA_HOME in a .bat File
    807588 Newbie
    Currently Being Moderated
    I read an article that said that C:\WINDOWS\system32\java.exe is just a dummy program that looks up the JRE location in the Windows Registry. With that in mind, I dug around for how to read the Windows Registry from a *.bat* file.

    Here's a start:
    C:\>reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit" /v CurrentVersion
    
    ! REG.EXE VERSION 3.0
    
    HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
        CurrentVersion      REG_SZ  1.6
    C:\>reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.6" /v JavaHome
    
    ! REG.EXE VERSION 3.0
    
    HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.6
        JavaHome    REG_SZ  C:\Program Files\Java\jdk1.6.0_13
    I'm not sure how to convert this into setting JAVA_HOME in a bat file, but it looks like it has potential.
  • 13. Re: Automatically Set JAVA_HOME in a .bat File
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    pilaftank wrote:
    A little bit of background may help here...

    The project is an open source project where I have no direct control over anyone's machine except my own. The project's wiki has simple but detailed step-by-step instructions for setting up SVN, building the project, and checking changes back into the repository.

    I'm fanatical about keeping things clean and straightforward.

    My open source rules:
    1) Don't depend on environmental settings (like the developer having JAVA_HOME properly set)
    2) Don't require developers to edit a file before the doing the first build
    3) Do work with any reasonably current versions of the platform components (OS, SVN, Ant, Java, etc.)
    4) Rely on the source/target attributes in build.xml for consistency (do not rely on environmental settings or specific versions of platform components)
    Except they are developers right? It isn't like it is a standard consumer so your rules seem a bit strict.

    And I suspect there will be situations where it won't work regardless of what you do.

    But at any rate you can construct a java app such that it writes a batch file. Your main batch file runs the java app and then it calls the batch file that was just created.
  • 14. Re: Automatically Set JAVA_HOME in a .bat File
    abillconsl Explorer
    Currently Being Moderated
    Exactly what I meant, yes. Regardless of what java version you have, such a simple java pgm should run on it. You put the class file in the same directory as the batch file and run it as
    java -cp . GetJHome batchfilename.bat
    pause
    batchfilename.bat
    PS: The batchfilename.bat is the original package batch file that is an argument to the java pgm. The execution of same is on the next line - the new version of it, that is. The Java pgm would create a new named batch file, or delete the original and write the same named one.
1 2 Previous Next