Java 8 went missing - java

This is my java versions:
Matching Java Virtual Machines (4):
16.0.1 (x86_64) "Oracle Corporation" - "Java SE 16.0.1" /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
11.0.12 (x86_64) "Oracle Corporation" - "Java SE 11.0.12" /Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home
1.8.291.10 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_291 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home
I used to be able to switch between versions like this:
export JAVA8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_HOME=$JAVA8_HOME
alias usejava8="export JAVA_HOME=$JAVA8_HOME"
alias usejava11="export JAVA_HOME=$JAVA11_HOME"
But now, I don't know what happened, whenever I try to use java8, it still points to Java 16. I even run export path manually but it doesn't work.
/usr/libexec/java_home -v 16
/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
/usr/libexec/java_home -v 8
/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
/usr/libexec/java_home -v 11
/Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home
So my Java 16 and 11 are pointed to the correct home, but my machine points 8 to 16. How do I fix this?

You have to use /usr/libexec/java_home -v 1.8 ("1.8" instead of "8") to specify the 1.8 (8) JAVA_HOME.

Related

Switching between Java versions for the same version

I want to make some experiments with GradleVM, so I installed the the next java versions on MacOS
% /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
17.0.2 (x86_64) "Oracle Corporation" - "OpenJDK 17.0.2" /Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home
11.0.17 (x86_64) "Homebrew" - "OpenJDK 11.0.17" /usr/local/Cellar/openjdk#11/11.0.17/libexec/openjdk.jdk/Contents/Home
11.0.17 (x86_64) "GraalVM Community" - "GraalVM CE 22.3.0" /Library/Java/JavaVirtualMachines/graalvm-ce-java11-22.3.0/Contents/Home
I can easily switch between v11 and v17 with the approach
alias java-17="export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version"
alias java-11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
But how I can switch between GraalVM11 and normal jdk11?
alias graalvm-11="export JAVA_HOME=/Library/Java/JavaVirtualMachines/graalvm-ce-java11-22.3.0/Contents/Home; java -version"
I've taken the path to GraalVM installation from the output of java_home -V

Using Multiple versions of Java on my mac

I had installed Java 16 before and was using it. Now I have a need to use Java 8. After installing Java 8 I realized that the JAVA_HOME variable has to be set. I use macOS Big Sur version 11.5.2 and my terminal uses zsh shell. I followed a video on youtube that told me to do the following steps:
touch .zshrc
open -e .zshrc
inside .zshrc file write down the following commands
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home'
export PATH = $JAVA_HOME/bin:$PATH
and save the file
source ~/.zshrc
but when I run the step 4 on my terminal I seem to get the following message:
/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home not found
But I can guarantee that the directory structure is present.
Here is the output when I run
ls /Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home
output
Use the /usr/libexec/java_home command. On my machine I have:
% /usr/libexec/java_home -V
Matching Java Virtual Machines (10):
16.0.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 16" /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home
16.0.1 (x86_64) "GraalVM Community" - "GraalVM CE 21.1.0" /Library/Java/JavaVirtualMachines/graalvm-ce-java16-21.1.0/Contents/Home
15.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
13.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 13" /Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home
12.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 12" /Library/Java/JavaVirtualMachines/adoptopenjdk-12.jdk/Contents/Home
11.0.12 (x86_64) "GraalVM Community" - "GraalVM CE 21.2.0" /Library/Java/JavaVirtualMachines/graalvm-ce-java11-21.2.0/Contents/Home
11.0.11 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
1.8.0_282+07 (x86_64) "GraalVM Community" - "GraalVM CE 21.0.0.2" /Library/Java/JavaVirtualMachines/graalvm-ce-java8-21.0.0.2/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home
Use the following to get Java 1.8 home:
% /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
If all the above works for your set-up, add the following to your
~/.zshrc:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

How to get /usr/libexec/java_home to filter out the JRE installations?

I have the following in my .zshrc file so I can quickly switch between JDK 8 and JDK 11. However I also have the JRE 8 and JRE 11 installed.
When I run the java8 alias (which uses the /usr/libexec/java_home script), it will default to the JRE instead of the JDK because they're the same version number.
Is it possible to filter out the JRE or to specify the JDK?
# java_home
java_version() {
/usr/libexec/java_home -v $1
export JAVA_HOME=$(/usr/libexec/java_home -v $1)
print "Switching JAVA_HOME to $JAVA_HOME"
}
alias java8="java_version 1.8"
alias java11="java_version 11"
export DEFAULT_JAVA_VERSION=1.8
export JAVA_HOME=$(/usr/libexec/java_home -v $DEFAULT_JAVA_VERSION) #default to 8
List of JVMs installed:
/usr/libexec/java_home -V
Matching Java Virtual Machines (4):
11.0.11 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
11.0.11 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK (JRE) 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jre/Contents/Home
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK (JRE) 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jre/Contents/Home
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
Edit: the "-v" flag only outputs the path to the version passed on. And the "-V" flag will output all versions detected in the system.

Wrong JAVA_HOME after upgrade to macOS Big Sur v11.0.1

In my setup on macOS I'm working with several JDKs, switching between them via /usr/libexec/java_home tool, similar to a method described in this SO answer
After upgrading to macOS Big Sur v11.0.1, my JAVA_HOME setting stopped working, always reporting the same java version:
% /usr/libexec/java_home -V
Matching Java Virtual Machines (5):
11.0.8 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
1.8.162.12 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_162 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
1.8.0_45 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
1.7.0_45 (x86_64) "Oracle Corporation" - "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
% /usr/libexec/java_home -v 1.8.162.12
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
% /usr/libexec/java_home -v 1.7.0_45
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
Seems in macOS Big Sur v11.0.1 the behavior of the /usr/libexec/java_home -v ... command has changed: it is sensitive to the previously set value of JAVA_HOME environment variable.
Exact behavior is not clear, I couldn't find any documentation on this, but in my experiments it reported the version already set in JAVA_HOME, regardless of the -v switch:
% JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home /usr/libexec/java_home -v 1.8.0_162
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Additionally, I noticed that it reports nothing, if JAVA_HOME is set, but doesn't point to a valid java home (also for -V):
% JAVA_HOME=dummy /usr/libexec/java_home -v 1.7.0_45
% JAVA_HOME=dummy /usr/libexec/java_home -V
%
Solution is to ensure JAVA_HOME is not set before executing /usr/libexec/java_home:
% unset JAVA_HOME ; /usr/libexec/java_home -v 1.8.0_162
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
I have Big Sur 11.2.1 from 18.02.2021. I had the same issue with JAVA_HOME path configuration. After reading a lot of information I solved my problem with next:
Install JDK 8: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
Java SE Development Kit 8u281
Install it as usual on your MacOS Big Sur.
Check JDK version:
java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
Check what versions of JDK are already installed on your machine:
/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
1.8.281.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_281 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Get only one that you need:
/usr/libexec/java_home -v 1.8.0_281
/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
On Catalina and Big Sur - there is Z-shell , to add Environment variables like JAVA_HOME there are two files for that: ~/.zshenv and ~/.zshrc, I couldn't make it work with first one. It worked for me like this:
nano ~/.zshrc
Add to that file this line:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_281)
Press Ctrl+X and save changes.
Then run command to apply that changes to current terminal:
source ~/.zshrc
After that you can check if everything is working with command:
echo $JAVA_HOME
it will print you: /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
Restart your terminal and programs which will be using JAVA_HOME variable. For me it was Android Studio. Now everything works fine.
I am new to Mac OS and it took me with 4hours to solve this. With Windows it's just done in a minute:)
As I tryed. I just delete the /Library/Internet Plug-Ins/JavaAppletPlugin.plugin and relogin. Then everything works fine.
For me https://developer.apple.com/forums/thread/666681 worked after spending so much time on the solutions.
I simply call following commands
sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefpane
Had this problem after installing Big Sur.
First I started uninstalling all JDK from the system with:
sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -rf /Library/PreferencePanes/JavaControlPanel.prefPane
sudo rm -rf /Library/Application\ Support/Oracle/Java/
sudo rm -rf /Library/Java/JavaVirtualMachines
Then, I installed the JDK that I am going to use:
brew tap AdoptOpenJDK/openjdk
brew install --cask adoptopenjdk<version>
For me, it is totally broken.
Unsetting JAVA_HOME, /usr/libexec/java_home gives:
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
$> /usr/libexec/java_home -v "1.8.0"
appears to access the exact jdk if present. Ignores the 'plugin' if installed.
The quotes were recommended in random places on internet. They are also shown in the man page.
The /usr/libexec/java_home tool in macOS will give you the location of the latest Java version installed. Invoke it from the terminal to get a response similar to the following:
/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
Navigate into the root and open the .zprofile file.
cd ~
open .zprofile
If the file is not found, create one using the echo > .zprofile command.
Open the file using open .zprofile. This should open it up in a text editor.
Add the response of the /usr/libexec/java_home tool into the file like so:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home
My solution is creating a link to your JDK file. Apparently, Teradata studio was executing a wrong path
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/server/libjvm.dylib /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/libserver.dylib
Agree with #Konstantin. I faced the same issue , after the Big Sur upgrade.
The steps you need to follow to fix this are :
unset JAVA_HOME
check what java_home in library is pointing to: /usr/libexec/java_home -v
Matching Java Virtual Machines (2):
1.8.221.11 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_221 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
Because both the versions start with 1.8.221, so by default 1st one is picked, check the version
/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home'
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

/usr/libexec/java_home Filter By JDK

In my MacOS i am using something like the below to set java on my
setJava() {
version=$#
echo "Setting JAVA to " $version
export JAVA_HOME=$(/usr/libexec/java_home -v $version)
echo "JAVA_HOME:" $JAVA_HOME
java -version
}
That works fine, but what i would like to achieve is choose a JDK not by the version, but by the JVM release itself, like Zulu, OpenJDK, Graal etc.
If i run
/usr/libexec/java_home -V
i am getting
Matching Java Virtual Machines (4):
12.0.1, x86_64: "AdoptOpenJDK 12" /Library/Java/JavaVirtualMachines/adoptopenjdk-12.jdk/Contents/Home
1.8.0_212, x86_64: "GraalVM CE 19.0.0" /Library/Java/JavaVirtualMachines/graalvm-ce-19.0.0/Contents/Home
1.8.0_163-zulu-8.28.0.1, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
1.8.0_161, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
Is there any way to parameterize the above script so that /usr/libexec/java_home returns me the JDK of the JDK that i would like to use?

Categories