JAVA_HOME 在 OSX 上使用 Eclipse 和 Maven
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20313453/
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
JAVA_HOME on OSX with Eclipse and Maven
提问by user3054594
I've recently set up an OSX machine and i have trouble getting Maven to pickup JAVA_HOME when running inside Eclipse.
我最近设置了一台 OSX 机器,但在 Eclipse 中运行时,我无法让 Maven 获取 JAVA_HOME。
I've done the following so far:
到目前为止,我已经完成了以下工作:
- Set JAVA_HOME in ~/.bash_profile with
export JAVA_HOME=$(/usr/libexec/java_home)
- Specify the VM for Eclipse inside eclipse.ini to
-vm /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java
- Set JAVA_HOME in
/etc/mavenrc
withecho JAVA_HOME=\
/usr/libexec/java_home -v 1.7` | sudo tee -a /etc/mavenrc`
- 在 ~/.bash_profile 中设置 JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home)
- 在 eclipse.ini 中为 Eclipse 指定 VM 以
-vm /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java
/etc/mavenrc
使用echo JAVA_HOME=\
/usr/libexec/java_home -v 1.7`设置 JAVA_HOME | 须藤 tee -a /etc/mavenrc`
Working from the CLI everything seems as expected. java -version
prints:
从 CLI 工作一切似乎都如预期。java -version
印刷:
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
echo $JAVA_HOME
prints:
echo $JAVA_HOME
印刷:
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
mvn -v
prints:
mvn -v
印刷:
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 17:22:22+0200)
Maven home: /usr/local/Cellar/maven/3.1.1/libexec
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"
Running mvn install
inside a project from the CLI works as well. However doing the same from within Eclipse fails with the following:
mvn install
从 CLI 在项目中运行也可以。但是,在 Eclipse 中执行相同操作失败,并显示以下内容:
Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.9.1:jar (attach-javadocs) on project suppress-warnings: MavenReportException: Error while creating archive: Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set.
In Eclipse > Preferences > Java > Installed JREs
i'm seeing that JDK 7 with the same location as specified in JAVA_HOME
is set as default and also used for my project. Switching between the embedded Maven runtime or the external installation yields the same results.
在Eclipse > Preferences > Java > Installed JREs
我看到与指定位置相同的 JDK 7JAVA_HOME
被设置为默认值,也用于我的项目。在嵌入式 Maven 运行时或外部安装之间切换会产生相同的结果。
Where or how do i have to set JAVA_HOME
so that a Maven process started from within Eclipse gets the right JAVA_HOME
location?
我必须在哪里或如何设置JAVA_HOME
以便从 Eclipse 中启动的 Maven 进程获得正确的JAVA_HOME
位置?
回答by openmike
In your home directory: (/Users/{username})
nano .bash_profile and add the line below :
export
JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home`Make sure the path "/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home" exist.
在您的主目录中:(/Users/{username})
nano .bash_profile 并添加以下行:
导出
JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home`确保路径“/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home”存在。
回答by Rob
Eclipse gives you the ability to specify launch environment variables. Do it there.
Eclipse 使您能够指定启动环境变量。在那里做。
回答by Jeen Broekstra
The problem is that on Mac OSX, the environment variables defined in .bashrc
or .bash_profile
are only exported to terminal environments. A bundled GUI app like Eclipse does not see them, and consequently neither does any run configuration executed from Eclipse.
问题是在 Mac OSX 上,定义的环境变量.bashrc
或.bash_profile
仅导出到终端环境。像 Eclipse 这样的捆绑 GUI 应用程序看不到它们,因此也看不到从 Eclipse 执行的任何运行配置。
The solution is to create (if it does not exist) or edit a file /etc/launchd.conf
and add the following line to it:
解决方案是创建(如果它不存在)或编辑一个文件/etc/launchd.conf
并向其添加以下行:
setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
Of course, modified to your actual JAVA_HOME
value. (note that the trick commonly used in .bash_profile
with $(/usr/libexec/java_home)
does not work in launchd.conf
- or at least I couldn't get it to work)
当然,修改为您的实际JAVA_HOME
值。(请注意,通常使用的技巧在.bash_profile
with$(/usr/libexec/java_home)
中不起作用launchd.conf
- 或者至少我无法让它起作用)
After you have done this, you will need to reboot the machine. After reboot the M2E Maven run configurations will be able to see the JAVA_HOME
variable and execute correctly.
完成此操作后,您将需要重新启动机器。重新启动后,M2E Maven 运行配置将能够看到JAVA_HOME
变量并正确执行。
回答by Roberto Pose
I cannot comment Jeen Broekstra answer because I am a novice :-(
我无法评论 Jeen Broekstra 的回答,因为我是新手 :-(
His solution is good, it needs only to be improved a little bit.
他的解决方案很好,只需要改进一点点。
In /etc/launchd.conf
it is sufficient to put the instruction
在/etc/launchd.conf
它足以把指令
getenv JAVA_HOME
Then, in /etc/bashrc
然后,在 /etc/bashrc
export JAVA_HOME=`/usr/libexec/java_home`
Then, re-login (it not necessary to reboot ;-) and Eclipse works as expected w.r.t. Maven.
然后,重新登录(不需要重新启动 ;-),Eclipse 就可以像 Maven 预期的那样工作。
回答by bgausden
You're probably looking for ~/.MacOSX/environment.plist
您可能正在寻找 ~/.MacOSX/environment.plist
Add a new row to environment.plist to set your preferred JAVA_HOME e.g.
向 environment.plist 添加新行以设置您首选的 JAVA_HOME 例如
JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home
JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home
envvars set here will be "seen" by applications including Eclipse
此处设置的 envvars 将被包括 Eclipse 在内的应用程序“看到”
回答by Sarfaraz Khan
I faced the same problem. Tried Robert's and Jeen's solution but for some reason it didn't work.So a very simple solution that worked for me was to add JAVA_HOME as environment variable in eclipse.In eclipse project's maven configuration add JAVA_HOME in the environment variable list.
我遇到了同样的问题。尝试了 Robert 和 Jeen 的解决方案,但由于某种原因它不起作用。所以对我有用的一个非常简单的解决方案是在 eclipse 中添加 JAVA_HOME 作为环境变量。在 eclipse 项目的 maven 配置中,在环境变量列表中添加 JAVA_HOME。
回答by Christopher Yang
回答by Pálossy László
That above works not any more in YOSEMITE for GRAPHICAL APPLICATIONS! Like eclipse, or anything started with Spotlight. (.bash_profile, launchd.conf works for terminal sessions only.) Before starting eclipse, just open a terminal window, and give out the following command:
以上不再适用于优胜美地的图形应用程序!像 eclipse,或任何以 Spotlight 开始的东西。(.bash_profile,launchd.conf 仅适用于终端会话。)在启动 eclipse 之前,只需打开一个终端窗口,并发出以下命令:
launchctl setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
(With your installation path! Perhaps works with $(/usr/libexec/java_home) instead of the full path too.)
(使用您的安装路径!也许也适用于 $(/usr/libexec/java_home) 而不是完整路径。)
View the whole excellent article about the permanent solution here: Setting environment variables via launchd.conf no longer works in OS X Yosemite?
在此处查看有关永久解决方案的整篇优秀文章:通过 launchd.conf 设置环境变量不再适用于 OS X Yosemite?
回答by Hanley Tang
A simple way to solve this issue ,add the following config to pom.xml
解决这个问题的一个简单方法,在 pom.xml 中添加以下配置
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
full code:
完整代码:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
<aggregate>true</aggregate>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
</plugin>
回答by feistyfawn
Locate the maven-javadoc-plugin artifactId in the pom.xml printing the error and add the java.home as a javadocExecutable configuration:
在打印错误的 pom.xml 中找到 maven-javadoc-plugin artifactId 并将 java.home 添加为 javadocExecutable 配置:
<plugin>
...
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<javadocExecutable>${java.home}/../bin/javadoc</javadocExecutable>
</configuration>
...
</plugin>
Reason: to use the applications own 'java.home' value is the best and simplest approach since it removes a dependance on operating systems local variables set which may or may not differ in another container
原因:使用应用程序自己的“java.home”值是最好和最简单的方法,因为它消除了对操作系统局部变量集的依赖,这些变量集在另一个容器中可能不同也可能不同