Maven not picking JAVA_HOME correctly

匿名 (未验证) 提交于 2019-12-03 01:01:02

问题:

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:

com.suntools1.6system${java.home}/../lib/tools.jar

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.

com.suntools1.6systemC:\Program Files\Java\jdk1.6.0_26\lib\tools.jar

Can someone suggest any dynamic solution for this?

回答1:

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://jre/bin/server/jvm.dll 


回答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.



回答3:

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



回答4:

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



回答5:

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.



回答6:

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



回答7:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!