Don't set JAVA_HOME with full path

When I tried to use mvn after a long time, I was addicted to specifying JAVA_HOME, so I made a memorandum.

mvn clean!

After updating to High Sierra, the dependencies of the Java project I'm using at work were reset, so I thought mvn clean for the time being.

$ mvn clean
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

Whaaaaaaaat

Apparently the environment variable JAVA_HOME is bad. How did you set it?

It's a full pass

$ cat ~/.bash_profile

...

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home

$ java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

Since it is specified with the full path, it quickly changes to a meaningless environment variable as soon as the JDK version is upgraded. Rather, it was often safe until now.

There should be a smarter setting method

Apparently, this notation will automatically follow you even if you upgrade the JDK. Save it with this path ...

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

result

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home

$ mvn -v
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T16:58:13+09:00)
Maven home: /usr/local/Cellar/maven/3.5.2/libexec
Java version: 1.8.0_172, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.4", arch: "x86_64", family: "mac"

The setting is completed successfully. I used to copy and paste, but I'm addicted to it if I don't pay attention to environment variables.

Recommended Posts

Don't set JAVA_HOME with full path
How to set JAVA_HOME with Maven appassembler-maven-plugin
JAVA_HOME and Path
Set up GitLab with docker
Validate path parameters with @RequestMapping