Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven not picking JAVA_HOME correctly

I am on windows environment and using maven to compile my project. Although I just created the project and added the dependencies for various libararies.

As I added them maven started complaining for the missing tools.jar, so i added below to my pom.xml:

<dependency>   <groupId>com.sun</groupId>   <artifactId>tools</artifactId>   <version>1.6</version>   <scope>system</scope>   <systemPath>${java.home}/../lib/tools.jar</systemPath> </dependency> 

When i ran the maven install, i got an error for the missing jar as below :

[ERROR] Failed to execute goal on project GApp: Could not resolve dependencies for project GApp:GApp:war:0.0.1-SNAPSHOT: Could not find artifact com.sun:tools:jar:1.6 at specified path C:\Program Files\Java\jre6\lib\tools.jar -> [Help 1] 

The issue is that the tools.jar is in "C:\Program Files\Java\jdk1.6.0_26\lib" and is correctly set in the JAVA_HOME environment variable but the maven is still looking in jre folder as in error message "C:\Program Files\Java\jre6\lib\tools.jar".

C:\>echo %JAVA_HOME% C:\Program Files\Java\jdk1.6.0_26 

Interestingly: when i set the full path in dependency, it worked just fine. But i don't want to hard code it.

<dependency>   <groupId>com.sun</groupId>   <artifactId>tools</artifactId>   <version>1.6</version>   <scope>system</scope>   <systemPath>C:\Program Files\Java\jdk1.6.0_26\lib\tools.jar</systemPath> </dependency> 

Can someone suggest any dynamic solution for this?

like image 291
Garry Avatar asked Nov 08 '12 12:11

Garry


People also ask

Does Maven need JAVA_HOME?

Maven always uses the JDK specified by JAVA_HOME , no matter how many JDK installations are available on the system. This allows the user the flexibility to change JDKs as required or based on the project. Hence, it is important to ensure JAVA_HOME is defined.

Why JAVA_HOME is not working?

Verify JAVA_HOME Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder. If it doesn't, your JAVA_HOME variable was not set correctly. Please make sure you're using the correct Java installation folder, or repeat the steps above.

Should I set JAVA_HOME to JDK or JRE?

You can use either the JDK or the JRE but: JAVA_HOME is used by the launcher for finding the JDK/JRE to use. (JDK is recommended as some tasks require the java tools.)


2 Answers

It's a bug in the Eclipse Maven support. Eclipse doesn't support all of the global Maven properties as per the Maven specs.

According to the specs:

${java.home} specifies the path to the current JRE_HOME environment use with relative paths to get for example

At least in Eclipse 4.3.1 that is not the case, here java.home always points to the JRE that was used to launch Eclipse, not the build JRE.

To fix the issue you need to start Eclipse using the JRE from the JDK by adding something like this to eclipse.ini (before -vmargs!):

-vm C:/<your_path_to_jdk170>/jre/bin/server/jvm.dll 
like image 152
rustyx Avatar answered Oct 06 '22 06:10

rustyx


It seems your JAVA_HOME is set to point to the JRE in eclipse.

like image 35
Maddy Avatar answered Oct 06 '22 08:10

Maddy