This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: Maven does not recognize JAVA_HOME correctly

I compile my projects using Maven in a Windows environment. Although I just created the project and added dependencies for the various libraries.

When I added them, Maven started reporting that tools.jar was missing, so I added the following to 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>Copy the code

When I run Maven Install, I find the following errors missing jars:

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

The tools.jar is located in C: Program Files\Java\jdk1.6.0\u 26\lib and is set correctly in the Java_ HOME environment variable. However, Maven still looks for the error message C:\Program Files\Java\jre6\lib\tools.jar in the JRE folder.

C:\>echo %JAVA_HOME%
C:\Program Files\Java\jdk16.. 0 _26
Copy the code

Interestingly: it worked fine when I set up the full path in dependency. 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\jdk16.. 0 _26\lib\tools.jar</systemPath>
</dependency>
Copy the code

Can anyone come up with a dynamic solution to this problem?

Answer a

This is a bug in Eclipse Maven. According to the Maven specification, Eclipse does not support all global Maven properties.

According to the specification:

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

At least not in Eclipse4.3.1, where java.home always points to the JRE used to launch Eclipse, rather than build the JRE.

To fix this, you need to start Eclipse from the JDK using the JRE in Eclipse.ini (before -vmargs!). Add the following content to:

-vm
C:/<your_path_to_jdk170>/jre/bin/server/jvm.dll
Copy the code

Answer two

Never use system-wide dependencies. All of the code in tools.jar is available from the running JVM. You should remove this dependency completely

To check what Maven runtime is using, simply call

mvn -v
Copy the code

If you still have dependency issues with toolsjar, find out which one it runs on:

mvn dependency:tree
Copy the code

Or if that fails, simply remove dependency after dependency until the problem with the command above is resolved.

Then, when you know where it came from, you can decide what to do next. One approach is to use exclusion of tool dependencies.

The article translated from Stack Overflow:stackoverflow.com/questions/1…