Java - zollak/pentest-notes GitHub Wiki

MacBook JAVA environment

List the available Java Virtual Machines:

MacBook-Pro:~ zollak$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
    14.0.1, x86_64:	"Java SE 14.0.1"	/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home
    11.0.7, x86_64:	"Java SE 11.0.7"	/Library/Java/JavaVirtualMachines/jdk-11.0.7.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

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

Note: The system use the highest version by default.

Change system wide java version on Mac OS

How to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave).

As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.

The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).

Solution without any 3rd party tools:

  • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
  • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.

System launcher will use the JDK with highest version among those that have an Info.plist file.

Note: more info about it, see this article on stackoverflow.

Usage:

Copy the executable to the scripts directory, then create a symlink for it:

MacBook-Pro:~ zollak$ cp pentest-notes/Tools/OSX/changeJDK.sh ~/scripts/
MacBook-Pro:~ zollak$ ln -s /Users/zollak/scripts/changeJDK.sh /usr/local/sbin/changeJDK

Before use the script the Info.plist files looks like these:

MacBook-Pro:~ zollak$ ls -la /Library/Java/JavaVirtualMachines/*/Contents/Info.plist*
-rw-r--r--  1 root  wheel  1585 Mar 12 02:21 /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Info.plist
-rw-r--r--  1 root  wheel  1581 Mar  5 11:55 /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Info.plist
-rw-r--r--  1 root  wheel  1576 Dec 20  2017 /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Info.plist
-rw-r--r--  1 root  wheel  1329 Mar 17  2018 /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Info.plist

MacBook-Pro:~ zollak$ java -version
java version "14.0.1" 2020-04-14
Java(TM) SE Runtime Environment (build 14.0.1+7)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)

Run the changeJDK script:

MacBook-Pro:~ zollak$ changeJDK
0 jdk-11.0.7.jdk disable
1 jdk-14.0.1.jdk enable
2 jdk-9.0.4.jdk disable
3 jdk1.8.0_162.jdk disable
Enter Default JDK: 3
Password:
Enable jdk1.8.0_162.jdk as default JDK

MacBook-Pro:~ zollak$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

MacBook-Pro:~ zollak$ ls -la /Library/Java/JavaVirtualMachines/*/Contents/Info.plist*
-rw-r--r--  1 root  wheel  1585 Mar 12 02:21 /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Info.plist.disable
-rw-r--r--  1 root  wheel  1581 Mar  5 11:55 /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Info.plist.disable
-rw-r--r--  1 root  wheel  1576 Dec 20  2017 /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Info.plist.disable
-rw-r--r--  1 root  wheel  1329 Mar 17  2018 /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Info.plist

MacBook-Pro:~ zollak$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
    1.8.0_162, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home

Important note: don't use JAVA_HOME as a global variable. It could overwrite the system wide settings until Java 9. Later on it has a mix state.

Note: please see changeJDK.sh file here.

Install Java

Java 8 with brew:

MacBook-Pro:~ zollak$ brew tap homebrew/cask-versions
MacBook-Pro:~ zollak$ brew cask install java8

Note: updated way to install java8: $ brew cask install homebrew/cask-versions/adoptopenjdk8

Latest Java downloads:

⚠️ **GitHub.com Fallback** ⚠️