JAVA_HOME on OSX with Eclipse and Maven
Asked Answered
N

11

14

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?

Nevile answered 1/12, 2013 at 14:54 Comment(0)
C
15

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://mcmap.net/q/803500/-java_home-on-osx-with-eclipse-and-maven

Cooe answered 31/3, 2014 at 4:26 Comment(0)
D
4

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.

Dag answered 15/4, 2014 at 8:59 Comment(0)
N
3

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:

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

View the whole excellent article about the permanent solution here: Setting environment variables via launchd.conf no longer works in OS X Yosemite?

Nab answered 11/5, 2015 at 13:56 Comment(1)
linkConstringent
R
2

Eclipse gives you the ability to specify launch environment variables. Do it there.

Redan answered 1/12, 2013 at 15:19 Comment(10)
This actually works, but requires me do define it for every run configuration. Guess have to live with that..Nevile
No. There is a master that it uses to create all other run configurations.Redan
@Redan can you elaborate on this? I am struggling with the exact same problem, and I have sofar failed to locate this master configuration you speak of.Cooe
Hi @JeenBroekstra, have not used eclipse in some time now. Have you seen this? #2079233Redan
or this one: #1998070 there is for sure a way to stipulate an environment variable that will then be used in all new run configurations.Redan
Thanks @Rob, appreciated. Unfortunately this does not solve the problem, as this preference settings does not give a way to add environment variables. I'll look at bit further - if I do find something I'll post it here.Cooe
@JeenBroekstra what about this: wiki.eclipse.org/Debug/Plan/3.6/Launch_TemplatesRedan
That sounds relevant, but it talks about an "LCD" treeviewer or toolbar (no explanation of what that stands for, though I assume it's 'Launch Configuration <something>'), and I can't for the life of me find that in my Eclipse installation (version 4.3). But I've managed to find an alternative solution - see my other answer. Thanks again!Cooe
Really sorry @JeenBroekstra! I spent a long time last night going around in circles looking for that defaults configuration page. It was a nightmare to find before, but now I think it's gone.Redan
As suggested by @Rob, adding environment variable JAVA_HOME[launch environment variables] under Run AS -> Environment Variable, added new variable 'JAVA_HOME' with correct path and this worked for me. + 1Hawserlaid
S
2

This solution worked best for me:

  • add /etc/mavenrc file.
  • add JAVA="/path/to/your/jdk" in file.
  • no need to reboot or log out, Maven picks up the change straight away.

See more details in this blog.

Supersensual answered 10/2, 2015 at 5:46 Comment(0)
B
1

A simple way to solve this issue ,add the following config to 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>
Bluefield answered 9/9, 2015 at 6:52 Comment(0)
I
0
  1. In your home directory: (/Users/{username})

  2. nano .bash_profile and add the line below :

    export
    JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home`

  3. Make sure the path "/Library/Java/JavaVirtualMachines/1.6.0_31-b04-413.jdk/Contents/Home" exist.

Ilka answered 1/12, 2013 at 15:11 Comment(1)
I already did that, except that your are suggesting to use Java 6 instead of 7. How would that help?Nevile
N
0

You're probably looking for ~/.MacOSX/environment.plist

Add a new row to environment.plist to set your preferred JAVA_HOME e.g.

JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home

envvars set here will be "seen" by applications including Eclipse

Negativism answered 27/6, 2014 at 10:9 Comment(0)
M
0

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.

Manure answered 18/12, 2014 at 11:37 Comment(0)
R
0

Locate the maven-javadoc-plugin artifactId in the pom.xml printing the error and add the java.home as a javadocExecutable configuration:

<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

Reverie answered 11/7, 2017 at 17:35 Comment(1)
If you are using Eclipse 2020-03 and your $JAVA_HOME is Java 14, you need to use the absolute path to the Jave 14 javadoc in <javadocExecutable> because the -vm path in eclipse.ini has to be set to Java 13.Islamism
Z
0

In Eclipse check that the JRE for Maven is selected correctly to a JDK.

The Maven Runner Java Runtime (JRE) needs to specify a JDK (not a JRE), as the javadoc command is not included in JRE. Therefore Use Internal JRE does not work with maven attach-javadocs.

Zeculon answered 16/2, 2018 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.