Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA_HOME on OSX with Eclipse and Maven

I've recently set up an OSX machine and i have trouble getting Maven to pickup JAVA_HOME when running inside Eclipse.

I've done the following so far:

  1. Set JAVA_HOME in ~/.bash_profile with export JAVA_HOME=$(/usr/libexec/java_home)
  2. Specify the VM for Eclipse inside eclipse.ini to -vm /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java
  3. Set JAVA_HOME in /etc/mavenrc with echo JAVA_HOME=\/usr/libexec/java_home -v 1.7` | sudo tee -a /etc/mavenrc`

Working from the CLI everything seems as expected. java -version prints:

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:

/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

mvn -v prints:

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:

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.

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?

like image 371
user3054594 Avatar asked Dec 01 '13 14:12

user3054594


People also ask

How to check JAVA HOME environment variable on Mac OS X?

Type “ echo $JAVA_HOME ” command to check Java Home environment variable on Mac OS X. Success result should look like this:

How to add Maven_Home=which path in Eclipse?

Just to use in eclipse you can add this in Window -> Preferences -> Java -> Build Path -> Classpath Variables. Just add new variable with name MAVEN_HOME. But for executions you need to set in in Run/Debug configuration - probably that's what you need. is about executable file. MAVEN_HOME= which path ? I am using apache maven 3.0.4

How to change Java_home and Maven_home values on the macOS terminal?

To make the change take effect, run source .bash_profile command in the terminal, now you can echo JAVA_HOME and MAVEN_HOME ‘s value on the macOS terminal.

How do I set Java_home on Mac?

Set JAVA_HOME ( Mac ). First, all environment variable settings are saved in the current user’s .bash_profile file, this file is saved in the current user’s home directory. Run cd ~ in terminal to go to the current user home directory. Then execute ls -l .bash_profile to make sure the file exists.


2 Answers

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.

The solution is to create (if it does not exist) or edit a file /etc/launchd.conf and add the following line to it:

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)

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.

Edit: for a refinement of my answer, see also this answer: https://stackoverflow.com/a/23078977/451941

like image 79
Jeen Broekstra Avatar answered Oct 16 '22 20:10

Jeen Broekstra


I cannot comment Jeen Broekstra answer because I am a novice :-(

His solution is good, it needs only to be improved a little bit.

In /etc/launchd.conf it is sufficient to put the instruction

getenv JAVA_HOME

Then, in /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.

like image 32
Roberto Pose Avatar answered Oct 16 '22 20:10

Roberto Pose