Maven没有正确地选择JAVA_HOME

时间:2021-05-23 23:16:27

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.

我使用windows环境并使用maven编译我的项目。尽管我刚刚创建了这个项目并添加了各种libararies的依赖项。

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

当我添加它们时,maven开始抱怨缺少的工具。jar,所以我在我的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 :

当我运行maven安装时,丢失的jar有一个错误,如下所示:

[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".

问题是这些工具。jar在“C:\程序文件\Java\jdk1.6.0_26\lib”中,并且在JAVA_HOME环境变量中正确设置,但是maven仍然在jre文件夹中查找错误消息“C:\程序文件\Java\jre6\lib工具.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?

有人能对此提出动态解决方案吗?

7 个解决方案

#1


32  

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

这是Eclipse Maven支持中的一个错误。根据Maven规范,Eclipse不支持所有的全局Maven属性。

According to the specs:

根据规格:

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

$ { java。home}指定当前JRE_HOME环境的路径,例如,使用相对路径获取

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.

至少在Eclipse 4.3.1中不是这样,这里是java。home总是指向用于启动Eclipse的JRE,而不是构建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!):

要解决这个问题,您需要使用JDK中的JRE启动Eclipse,方法是向Eclipse添加类似的东西。ini(之前—vmargs !):

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

#2


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

永远不要使用系统范围依赖项。工具中的所有代码。jar将通过运行的JVM获得。您应该完全消除这种依赖关系。

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

另外,为了检查运行时Maven使用的是什么,只需调用

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

如果您仍然对tools jar有依赖关系,那么您添加的一个依赖关系就具有这种依赖关系(而且质量真的很差)。找出它运行的是哪一个。

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


1  

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

看起来您的JAVA_HOME被设置为指向eclipse中的JRE。

#4


1  

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

Maven $ { java。home}属性根据您的Eclipse执行从不同的地方设置和获取:

  • from the default JRE selected for the workspace

    从为工作区选择的默认JRE中

    Window>Preferences>Java>Installed JREs

    Window > Preferences > Java >安装jre

  • from the specific Eclipse project,

    在特定的Eclipse项目中,

    Java Build Path>Libraries>JRE System Library

    Java构建路径>库>JRE系统库

  • from the Run configuration.

    从运行配置。

    Run Configurations> Specific Run Configuration > JRE

    运行配置>特定的运行配置> JRE

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

记住您的JRE home路径指向JDK或JRE下的JDK。

#5


0  

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

听起来您正在Eclipse中运行Maven。Eclipse不咨询JAVA_HOME。

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

确保您已经将JRE设置为您想要的JDK。

#6


0  

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

在添加JAVA_HOME环境变量时,如果路径中有空格,则必须用引号括起来。

#7


0  

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.

您可能使用了错误的Maven安装。在窗口>首选项> Maven >安装中切换。我将我的设置设置为Fedora Maven安装;将它更改为默认版本(m2e中嵌入的版本)为我解决了这个问题。

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.

我怀疑导致这个问题的原因是Fedora Maven安装使用的是Fedora的OpenJDK,它可能会放置工具。jar位于一个奇怪的位置(关于Fedora的OpenJDK的一切都是奇怪和不标准的),因此Maven找不到它。

#1


32  

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

这是Eclipse Maven支持中的一个错误。根据Maven规范,Eclipse不支持所有的全局Maven属性。

According to the specs:

根据规格:

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

$ { java。home}指定当前JRE_HOME环境的路径,例如,使用相对路径获取

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.

至少在Eclipse 4.3.1中不是这样,这里是java。home总是指向用于启动Eclipse的JRE,而不是构建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!):

要解决这个问题,您需要使用JDK中的JRE启动Eclipse,方法是向Eclipse添加类似的东西。ini(之前—vmargs !):

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

#2


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

永远不要使用系统范围依赖项。工具中的所有代码。jar将通过运行的JVM获得。您应该完全消除这种依赖关系。

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

另外,为了检查运行时Maven使用的是什么,只需调用

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

如果您仍然对tools jar有依赖关系,那么您添加的一个依赖关系就具有这种依赖关系(而且质量真的很差)。找出它运行的是哪一个。

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


1  

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

看起来您的JAVA_HOME被设置为指向eclipse中的JRE。

#4


1  

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

Maven $ { java。home}属性根据您的Eclipse执行从不同的地方设置和获取:

  • from the default JRE selected for the workspace

    从为工作区选择的默认JRE中

    Window>Preferences>Java>Installed JREs

    Window > Preferences > Java >安装jre

  • from the specific Eclipse project,

    在特定的Eclipse项目中,

    Java Build Path>Libraries>JRE System Library

    Java构建路径>库>JRE系统库

  • from the Run configuration.

    从运行配置。

    Run Configurations> Specific Run Configuration > JRE

    运行配置>特定的运行配置> JRE

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

记住您的JRE home路径指向JDK或JRE下的JDK。

#5


0  

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

听起来您正在Eclipse中运行Maven。Eclipse不咨询JAVA_HOME。

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

确保您已经将JRE设置为您想要的JDK。

#6


0  

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

在添加JAVA_HOME环境变量时,如果路径中有空格,则必须用引号括起来。

#7


0  

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.

您可能使用了错误的Maven安装。在窗口>首选项> Maven >安装中切换。我将我的设置设置为Fedora Maven安装;将它更改为默认版本(m2e中嵌入的版本)为我解决了这个问题。

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.

我怀疑导致这个问题的原因是Fedora Maven安装使用的是Fedora的OpenJDK,它可能会放置工具。jar位于一个奇怪的位置(关于Fedora的OpenJDK的一切都是奇怪和不标准的),因此Maven找不到它。