Java_home in Maven

96,396

Solution 1

No, it's not wrong. It is pointing to the JRE used by your JDK, which is what it's supposed to. If you print out JAVA_HOME outside maven, it should print correctly:

C:\>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.7.0_07

C:\>mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 10:44:56+0200)
Maven home: C:\APPS\apache-maven-3.0.4\bin\..
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_07\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
C:\>

So basically JAVA_HOME needs to point to a JDK installation (maven needs the tools.jar) but maven actually uses the jre within the JDK to run itself.

When using mvn -version, maven uses java internal java.home property, as can be seen from source code:

version.append( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) ).append( LS );

This property is not the same thing as JAVA_HOME environment setting, so it might fool you. It is actually dynamic property showing you which JRE is running your code. If you compile and execute a Test.java test class printing the same, you can see that if your JAVA_HOME points to a JDK, the value of java.home is not equal to your JAVA_HOME. This is expected.

Quoting this:

What's the difference between JAVA_HOME and java.home?

JAVA_HOME is the JDK install directory, e.g., C:\jdk5. It's meant to be set as an environment variable and referenced in Windows batch files or Unix scripts. I always have it in my Windows Control Panel and .tcsh files,along with other common environment variables. Some Java applications use the name jdk.home for this purpose, which I think is a better name. But JAVA_HOME has been used since the beginning and is now a convention.

java.home is the JRE install directory, e.g., C:\jdk5\jre, or C:\Program Files\Java\jre1.5.0_06. Unlike JAVA_HOME, I never seen java.home as an environment variable. java.home is a build-in Java system property, whose value is the JRE install directory. Since all Java system properties are also exposed as Ant build properties, you can also use ${java.home} in build files.

Would jre.home be a better name? Maybe, but I don't think Sun will change it.

You can see that maven uses JAVA_HOME on mvn.bat:

:endInit
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
..
%MAVEN_JAVA_EXE% %MAVEN_OPTS% -classpath %CLASSWORLDS_JAR% ..

And if you want to make sure, you can comment out "@echo off" statement in mvn.bat, so you can see that it is being used.


TL;DR: Based on the information you've given, your configuration is correct, no need to change anything.


Edit: thanks to this thread, there was also an issue about this being confusing, which resulted in change of output for Maven version 3.5.4.

Solution 2

What helped me debugging mvn/jdk issues is to set this before running mvn: set MAVEN_BATCH_ECHO=on

You get verbose output (on windows), including info how Maven picks java.

Share:
96,396

Related videos on Youtube

One Two Three
Author by

One Two Three

Updated on April 14, 2020

Comments

  • One Two Three
    One Two Three about 4 years

    When I ran mvn -version, I noticed the java_home points to ...jdk\jre (as shown below). Is that wrong? Isn't it supposed to point to ...\jdk.x.y.z (without the \jre)? If so, how do I reset it? (In global %java_home% points to the jdk directory)

    C:\Users\Owner>mvn -version
    Apache Maven 2.2.1 (r801777; 2009-08-06 15:16:01-0400)
    Java version: 1.7.0_17
    Java home: C:\Program Files\Java\jdk1.7.0_17\jre
    Default locale: en_US, platform encoding: Cp1252
    OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows"
    
  • One Two Three
    One Two Three about 11 years
    Thanks! +1 for the TL;DR ;))
  • eis
    eis about 11 years
    No prob. Maybe accept if you feel this answered the question :)
  • Kraken
    Kraken over 9 years
    @eis Hi, I've got a problem stackoverflow.com/questions/26313902/… here, can you please help me figure it out. Thanks.
  • eis
    eis over 9 years
    @Kraken apparently you got it sorted yourself. Indeed, tools.jar is needed by Maven.
  • int lawl is over 9000
    int lawl is over 9000 over 6 years
    This was more complicated than it needed to be. Could you add instructions to "copy your JDK lib/tools.jar to /jre/lib"?
  • eis
    eis over 6 years
    @intlawlisover9000 why? you definitely shouldn't do that.
  • Adam Howell
    Adam Howell about 3 years
    When I added JAVA_HOME to my Windows 10 install, I entered two directories, separated by a semicolon. When I removed the second entry, Windows kept the semicolon, which showed up because of this setting.