[JAVA] How to set up and operate jEnv (Mac)

What is "jEnv"?

Java environment management tool. By introducing jEnv, you can easily switch between Java versions. It is pyenv in Python. The operation method is almost the same.

environment

setup

Install jEnv

Install from Homebrew.

$ brew update
$ brew install jenv

Add the following to .bash_profile and pass it through the path.

.bash_profile


# jEnv
export JENV_ROOT="$HOME/.jenv"
if [ -d "${JENV_ROOT}" ]; then
  export PATH="$JENV_ROOT/bin:$PATH"
  eval "$(jenv init -)"
fi

JDK installation

There are many articles installed with brew cask, but this time I will drop the installer from Oracle and install it manually. Install the required version of the JDK by referring to the following article.

-How to install past version of Java (JDK 10 or earlier) (Mac) --Qiita

-How to uninstall Java 8 (Mac) --Qiita

Doing the above will enrich your Java development environment.

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (5):
    10, x86_64:	"Java SE 10"	/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
    9.0.4, x86_64:	"Java SE 9.0.4"	/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
    1.8.0_162, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
    1.8.0_77, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home
    1.7.0_80, x86_64:	"Java SE 7"	/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home

Probably not all, but somehow it feels good so I installed all of JDK 7-10.

Create .jenv folder

Manually create the ~ / .jenv / versions folder.

$ mkdir ~/.jenv
$ mkdir ~/.jenv/versions

If not created, jenv add will cause ln: failed to create symbolic link'/ Users / {username} /. Jenv / versions / oracle64-1.7.0.80': No such file or directory error. https://github.com/gcuisinier/jenv/issues/175

Once created, reload .bash_profile.

$ source ~/.bash_profile

Add jEnv environment

Initially there is no environment in jEnv.

$ jenv versions
* system (set by /Users/{username}/.jenv/version)

Add the environment with the jenv add command.

# jenv add {Java Virtual Machine(JVM)Home path}
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home

The JVM Home path is the path output by the / usr / libexec / java_home -V command.

If an environment with the same major version has already been added, overwriting will be confirmed, so enter y and it is OK.

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
oracle64-1.8.0.162 added
1.8.0.162 added
There is already a 1.8 JDK managed by jenv
Do you want to override (type y to confirm)? y
1.8 added

With this, the environment of jEnv has also been enriched.

$ jenv versions
* system (set by /Users/{username}/.jenv/version)
  1.7
  1.7.0.80
  1.8
  1.8.0.162
  1.8.0.77
  10
  9.0
  9.0.4
  oracle64-1.7.0.80
  oracle64-1.8.0.162
  oracle64-1.8.0.77
  oracle64-10
  oracle64-9.0.4

Method of operation

Java environment switching (global)

Set up the global Java environment with the jenv global command. Java 10 has just been released and it seems that Kotlin is not yet supported, so we will set Java 9 here.

#Set up a global Java environment
# jenv global {Environment name}
$ jenv global oracle64-9.0.4

Even in Java 9, there are three environments, "9.0", "9.0.4", and "oracle64-9.0.4", but since the tutorial on the official website sets "oracle64 ...", that is also the case here. I haven't investigated what is different.

Check if the environment has changed. If "*" is added at the beginning of "oracle64-9.0.4", the environment has been switched.

$ jenv versions
  system
  1.7
  1.7.0.80
  1.8
  1.8.0.162
  1.8.0.77
  10
  9.0
  9.0.4
  oracle64-1.7.0.80
  oracle64-1.8.0.162
  oracle64-1.8.0.77
  oracle64-10
* oracle64-9.0.4 (set by /Users/{username}/.jenv/version)

Also check the Java version.

$ java -version
java 9.0.4
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

You can see that the environment has changed.

(2018/04/04 23:30 postscript)

Gradle didn't work well with Java 9, so I decided to use Java 8. https://qiita.com/uhooi/items/c9caa9a9ed6c934a789b#gradleのインストール

Java environment switching (local)

If you want to set up a local Java environment (only under a specific folder), you can set it with the jenv local command after moving to the target folder.

$ mkdir java7
$ cd java7
$ jenv local oracle64-1.7.0.80
$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

##Global settings are applied to folders that are not set locally
$ cd ..
$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

in conclusion

The Java development environment is ready. Now you can play around with Android Studio and Kotlin!

Reference link

Recommended Posts

How to set up and operate jEnv (Mac)
How to set up and use kapt
How to set up JavaED Full Edition (pleiades)
How to set up computer vision for tracking images and videos with TrackingJs
Steps to set up Postfix and Dovecot on CentOS 8.3
How to install Gradle and Kotlin with SDKMAN (Mac)
How to set up Android OR mapper "Orma" (Kotlin)
How to uninstall Java 8 (Mac)
How to set Docker nginx
How to set Java constants
How to install JDK8-10 (Mac)
Steps to set up Jenkins on your local Mac, create one job and succeed
How to set character code and line feed code in Eclipse
How to set and describe environment variables using Rails zsh
How to set up a proxy with authentication in Feign
How to use StringBurrer and Arrays.toString.
How to install JMeter for Mac
How to set Spring Boot + PostgreSQL
How to call classes and methods
[Rails] How to speed up docker-compose
How to use equality and equality (how to use equals)
How to connect Heroku and Sequel
How to convert LocalDate and Timestamp
How to set Lombok in Eclipse
How to use \ t Escape sequences different for mac and Windows-java
How to set and use profile in annotation-based Configuration in Spring framework
[Note] How to restart the Windows container set up with docker-compose
How to check Java installed on Mac
How to use OrientJS and OrientDB together
How to install Play Framework 2.6 for Mac
How to install Eclipse (Photon) on Mac
How to switch Java versions on Mac
How to set different source / target versions for production code and test code
[Java] How to set the Date time to 00:00:00
How to build SquashTM and how to support Japanese
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to find the tens and ones
[Easy] How to upgrade Ruby and bundler
How to use substring and substr methods
Needed for iOS 14? How to set NSUserTrackingUsageDescription
Note: [Docker] How to start and stop
How to write and explain Dockerfile, docker-compose
How to use @Builder and @NoArgsConstructor together
How to use ToolBar with super margin Part1 Set characters and change colors
JDBC promises and examples of how to write
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to output Excel and PDF using Excella
How to execute and mock methods using JUnit
[Java] How to get and output standard input
How to play audio and music using javascript
How to use Segmented Control and points to note
Git and GitHub ~ How to fix common errors ~
[Java] (for MacOS) How to set the classpath
How to use scope and pass processing (Jakarta)
How to get and study java SE8 Gold
How to build API with GraphQL and Rails
How to execute processing before and after docker-entrypoint.sh
How to find the total score and average score
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to get success and error messages
How to build parquet-tools and merge Parquet files