5

When I type mvn --version in ubuntu from the terminal(ubuntu),I get the below output.

Warning: JAVA_HOME environment variable is not set.
Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-32-generic", arch: "amd64", family:     "unix"

When I did not set any JAVA_HOME environment variable , how is maven getting the java home installed path.Is it trying to find this path from the /usr/bin/java command which is installed in my system and if so why is it taking the path till jre.

P.S : Also I could not find any java path in any maven config.

Thanks.

crackerplace
  • 4,787
  • 7
  • 27
  • 39

3 Answers3

3

As showed in the CLIReportingUtils.java (the maven class that retrieves the Java Home), the value comes from the following call :

System.getProperty( "java.home", "<unknown java home>" )

The java.home is for the JRE unlike the JAVA_HOME which is for the JDK. So Maven is displaying the JRE home.

Zakaria
  • 14,316
  • 22
  • 82
  • 123
  • My question was how come maven is picking some java path as JAVA_HOME when I did not set any.This is for ubuntu. – crackerplace Jul 03 '15 at 11:16
  • System properties are for Java only. You don't set it on the OS. You can set it while running a command (-DpropertyName) : http://stackoverflow.com/a/7054981/644669 – Zakaria Jul 03 '15 at 11:22
  • ok.Got that.But which java process is setting this system property and how is it able to get this path ? – crackerplace Jul 03 '15 at 11:40
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/System.java#System.initProperties%28java.util.Properties%29 Then, go to the java native implementation (in c). The java.home is communicated by the OS to Java when installed. – Zakaria Jul 03 '15 at 12:17
1

I would say there is a difference between JAVA_HOME environment variable and the output Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre. The latter is just the output of the installation directory see here.

So maven will run Java in background and java knows where its installed.

hinneLinks
  • 3,293
  • 20
  • 36
  • yes.I meant maven was treating this java location as JAVA_HOME for itself and hence its printed in mvn --version. – crackerplace Jul 03 '15 at 11:34
  • 1
    as @Zakaria posted, Maven just reads the `java.home` Property, which is by default the install-directory. `System.out.println(System.getProperty("java.home"))` is always the installation directory, `System.out.println(System.getenv("JAVA_HOME"))` is the value of the environment variable – hinneLinks Jul 03 '15 at 11:45
  • Thanks for that.Was confused in choosing your answer and zakaria's.Just went with zakaria's for the source code link. – crackerplace Jul 03 '15 at 11:52
0

I think,I understand it now.In the maven script i.e (usr/share/maven/bin/mvn) they are trying to find the java installed using a variety of options.

So at one place they are doing the below

JAVACMD="`which java`"

And now in my system "which java" points to the below

java -> /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

Hope this is how its getting the java path.

crackerplace
  • 4,787
  • 7
  • 27
  • 39
  • Sorry to insist but as I pointed it in my answer you can see in the maven source code that the maven command is calling the following instruction : `System.getProperty( "java.home", "" )` – Zakaria Jul 03 '15 at 11:24