简体   繁体   English

Maven 没有正确选择 JAVA_HOME

[英]Maven not picking JAVA_HOME correctly

I am on windows environment and using maven to compile my project.我在 windows 环境下使用 maven 编译我的项目。 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 :当我添加它们时,maven 开始抱怨缺少tools.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 ".问题是tools.jar位于“ C:\\Program Files\\Java\\jdk1.6.0_26\\lib ”并且在JAVA_HOME环境变量中正确设置,但 maven 仍在 jre 文件夹中查找错误消息“ 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?有人可以为此提出任何动态解决方案吗?

It's a bug in the Eclipse Maven support.这是 Eclipse Maven 支持中的一个错误。 Eclipse doesn't support all of the global Maven properties as per theMaven specs .根据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总是指向用于启动 EclipseJRE,而不是构建 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

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

You should NEVER use system scope dependencies.您永远不应该使用系统范围依赖项。 All the code in tools.jar will be available just via the running JVM already. tools.jar 中的所有代码都可以通过正在运行的 JVM 获得。 You should remove this dependency altogether..您应该完全删除此依赖项..

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).如果您仍然存在对工具 jar 的依赖作为问题,那么您添加的依赖项之一具有该依赖项(而且质量确实很差)。 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.一种方法是对拉入工具的依赖项使用排除项。

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窗口>首选项>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 主路径指向 JDK 或 JDK 下的 JRE

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

You might be using the wrong Maven installation.您可能使用了错误的 Maven 安装。 Switch it in Window > Preferences > Maven > Installations.在“窗口”>“首选项”>“Maven”>“安装”中切换它。 I had mine set to the Fedora Maven install;我将我的设置为 Fedora Maven 安装; changing it back to the default (the version embedded in m2e) fixed the problem for me.将其改回默认值(嵌入在 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,这可能将 tools.jar 放在一个奇怪的位置(有关 Fedora 的 OpenJDK 的所有内容都很奇怪且不标准),因此 Maven 找不到它。

Also look in the .mavenrc file in your home dir.还要查看主目录中的 .mavenrc 文件。 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.我按照建议更改了所有内容,结果发现我的 .mavenrc 文件将 JAVA_HOME 设置为旧版本。

Sounds like you are running Maven in Eclipse.听起来您正在 Eclipse 中运行 Maven。 Eclipse does not consult JAVA_HOME. Eclipse 不咨询 JAVA_HOME。

Make sure you have set your JRE in Eclipse preferences to your desired JDK.确保已将 Eclipse 首选项中的 JRE 设置为所需的 JDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM