Maven not picking JAVA_HOME correctly
Asked Answered
C

10

35

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?

Cards answered 8/11, 2012 at 12:3 Comment(5)
What's the value given for ${java.home}?Jesus
Are you running Maven from the command line or in an IDE like Eclipse or IntelliJ?Nynorsk
@metalmadz JAVA_HOME is environment variableCards
@cuberoot, i am running it from within the EclipseCards
I have always avoided placing the JDK/JREs in directories containing spaces (cf. "Program Files") as that has been known to mess with Maven. Perhaps give that a try?Rubbish
A
39

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
Alphard answered 17/4, 2014 at 9:18 Comment(6)
Is there a link for the Eclipse Maven bug related to this?Vernalize
What's the analog of javaw.exe in a Linux environment? jre/bin/ has a java and a javaws, but no javaw.Unpremeditated
On a Mac/Linux the path would be smth like /<your_path_to_jdk170>/jre/bin/javaAlphard
Works perfectly thanks. Garry, you could accept this ;)Marlette
Seems this issues still exists? The same error I got today, and your answer really helped. So, is this issues in existence, even after 4+ years? What exactly is this error?Freudian
Five years after this answer this bug is still there (now is 2019-04-05). I also want to note that be very careful with the path. 1) do not use double quotes around the string. 2) Use forward slash even in windows. 3) Use the jvm.dll 4) make sure you point to jdk.../jre/.... 5) as @Daniel Fernández has pointed out, need a line break after -vm (and not --vm!) For me the winning path is this C:/Program Files/Java/jdk1.8.0_201/jre/bin/server/jvm.dllLynea
J
2

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

Jesus answered 8/11, 2012 at 12:45 Comment(0)
C
2

You should NEVER use system scope dependencies. All the code in tools.jar will be available just via the running JVM already. You should remove this dependency altogether..

Also in order to check what runtime Maven is using just call

mvn -v

If you are still having a dependency to the tools jar as a problem, one of the dependencies you added has that dependency (and it is really bad quality). To find out which one it is run

mvn dependency:tree

or if that fails just remove one dependency after another until the problems is gone for the command above.

Then, when you know where it comes from you can decide what to do next. One path would be to use an exclusion on the dependency that pull tools in.

Cowitch answered 8/11, 2012 at 18:4 Comment(2)
i know i should never use system scope dependencies but as i communicated in my question, when i don't mention the tools.jar in dependency maven starts complaining for missing artifact "com.sun:tools:jar:1.4.2".Cards
Below is the output of mvn -v C:\>mvn -v Apache Maven 3.0.3 (r1075438; 2011-02-28 23:01:09+0530) Maven home: C:\Custom Programs\apache-maven-3.0.3\bin\.. Java version: 1.6.0_26, vendor: Sun Microsystems Inc. Java home: C:\Program Files\Java\jdk1.6.0_26\jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows xp", version: "5.1", arch: "x86", family: "windows"Cards
R
2

Maven ${java.home} property is set and taken from different places depending your Eclipse execution:

  • from the default JRE selected for the workspace

    Window>Preferences>Java>Installed JREs

  • from the specific Eclipse project,

    Java Build Path>Libraries>JRE System Library

  • from the Run configuration.

    Run Configurations> Specific Run Configuration > JRE

Remember your JRE home paths points to a JDK or JRE under JDK

Rafaelof answered 21/10, 2014 at 9:30 Comment(1)
This answer is still accurate 6 years later (for Eclipse 2020-09)! In my case, the run configuration setting was the faulty one.Protection
B
1

When you add the JAVA_HOME environment variable, if there are spaces in the path you must wrap the whole thing in quotes.

Bridewell answered 5/2, 2013 at 0:8 Comment(0)
S
1

You might be using the wrong Maven installation. Switch it in Window > Preferences > Maven > Installations. I had mine set to the Fedora Maven install; changing it back to the default (the version embedded in m2e) fixed the problem for me.

I suspect what was causing this issue is that the Fedora Maven install was using Fedora's OpenJDK, which probably puts tools.jar in a weird location (everything about Fedora's OpenJDK is weird and non-standard), so Maven can't find it.

Sabinasabine answered 19/1, 2017 at 23:46 Comment(0)
F
1

Also look in the .mavenrc file in your home dir. This caught me off guard.

I changed everything as suggested only to find that my .mavenrc file set the JAVA_HOME to an older version.

Festival answered 30/1, 2020 at 17:18 Comment(0)
N
0

Sounds like you are running Maven in Eclipse. Eclipse does not consult JAVA_HOME.

Make sure you have set your JRE in Eclipse preferences to your desired JDK.

Nynorsk answered 8/11, 2012 at 12:34 Comment(0)
C
0

Check to see if you have a .mavenrc file, it may be forcing JAVA_HOME to be something other that what is set in the environment variable.

Champlain answered 16/9, 2021 at 18:53 Comment(0)
M
0

it's just a bug in maven support for eclipse. You can ignore this error for now.

The support for local dependencies will be discontinued in near future, I think the maven developers understand this approach is not the best way to resolve this, but we know that exists some proprietary jars (like db2's JDBC driver license) that never will resided in a public repository ...

Melchior answered 29/3, 2023 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.