java 为什么 maven 忽略我的 JAVA_HOME?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3350722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:25:53  来源:igfitidea点击:

Why maven ignores my JAVA_HOME?

javamaven-2

提问by yegor256

This is what I'm getting when mvnis executed from CruiseControl:

这是我mvn从 CruiseControl 执行时得到的:

Unable to locate the Javac Compiler in:
  /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/../lib/tools.jar
Please ensure you are using JDK 1.4 or above and
not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.

At the same time, starting it from command line gives correct result. Why mvnis going into this tools.jar? Why it is ignoring my JAVA_HOME? And how can I fix it?

同时,从命令行启动它会给出正确的结果。为什么mvn要进入这个tools.jar?为什么它忽略了我的JAVA_HOME?我该如何解决?

I don't know what are the values of JAVA_HOME/PATHwhen mvnis started from CC. I would really love to get this information but I don't know how. CC itself is started from user ccand envfor this user gives me (it's CentOS 5.4):

我不知道什么JAVA_HOME/PATH时候mvn从 CC 启动的值。我真的很想得到这些信息,但我不知道如何。CC本身是从用户开始ccenv该用户给我(这是CentOS的5.4):

JAVA_HOME=/usr/java/default
PATH=/usr/local/maven/bin:/usr/local/bin:/bin:/usr/bin:/home/cc/bin

采纳答案by Vitek

Assuming that you have a linux machine.

假设你有一台 linux 机器。

  1. Take a look on /usr/bin/java, this is a symbolic link. Look where is a target of this symbolic link (in my case the target is at this location /etc/alternatives/java)
  2. /etc/alternatives/javais symbolic link too. Create new symbolic link to "proper" JVM (e.g. Sun's JVM)
  3. Replace /etc/alternatives/javawith newly created symbolic link.
  1. 看一看/usr/bin/java,这是一个符号链接。看看这个符号链接的目标在哪里(在我的例子中,目标在这个位置/etc/alternatives/java
  2. /etc/alternatives/java也是符号链接。创建指向“正确”JVM(例如 Sun 的 JVM)的新符号链接
  3. 替换/etc/alternatives/java为新创建的符号链接。

This works for Ubuntu distribution. There is an automatic way how to do that but I forgot it actually :D

这适用于 Ubuntu 发行版。有一种自动方法可以做到这一点,但我实际上忘记了:D

回答by angularsen

I had the same problem and a simple re-install of Java JDK solved it for me:

我遇到了同样的问题,简单地重新安装 Java JDK 为我解决了这个问题:

apt-get install openjdk-6-jdk

apt-get install openjdk-6-jdk

回答by Pascal Thivent

Why mvn is going into this tools.jar?

为什么 mvn 要进入这个 tools.jar?

Because tools.jarcontains the com.sun.tools.javac.Mainclass which is used to invoke the javaccompiler programmatically.

因为tools.jar包含com.sun.tools.javac.Main用于以javac编程方式调用编译器的类。

Why it is ignoring my JAVA_HOME

为什么它忽略了我的 JAVA_HOME

That's the "interesting" part (since it's working outside CC). Are you running CC with a dedicated user? If yes, is JAVA_HOMEwell defined for this user? What is the output of

这是“有趣”的部分(因为它在 CC 之外工作)。您是否与专门的用户一起运行 CC?如果是,是否JAVA_HOME为该用户定义良好?什么是输出

echo $JAVA_HOME -or-
echo $PATH

回答by florent

type the following line to the version of Sun

在 Sun 版本中键入以下行

sudo update-java-alternatives -s java-6-sun

回答by binarygiant

I ended up having to point to the Oracle Java 7 installations on Ubuntu. Gradle (1.6) does not seem to like sym links at all. So I edited the /opt/gradle/gradle-1.6/bin/gradle file to export a JAVA_HOME of /usr/lib/jvm/java-7-oracle at the top of the script. It's happy now.

我最终不得不指向 Ubuntu 上的 Oracle Java 7 安装。Gradle (1.6) 似乎根本不喜欢符号链接。因此,我编辑了 /opt/gradle/gradle-1.6/bin/gradle 文件以在脚本顶部导出 /usr/lib/jvm/java-7-oracle 的 JAVA_HOME。现在很幸福。

回答by Павел

I know this is far outdated question, but I ended up there googling the same problem. So this solution also works fine: just put "fork='yes'" in "javac" task. After this javac task become honoring the JAVA_HOME setting.

我知道这是一个过时的问题,但我最终在谷歌上搜索了同样的问题。所以这个解决方案也很好用:只需将“fork='yes'”放在“javac”任务中。在此 javac 任务成为尊重 JAVA_HOME 设置之后。

回答by figl

mvnruns everything in a command environment that does not inherit the local environment variables of your current shell.

mvn在不继承当前 shell 的本地环境变量的命令环境中运行所有内容。

export JAVA_HOME

first then your commands maywork.

首先,您的命令可能会起作用。