Install java on Centos 7 and Command not known error - java

I downloaded JDK8U171 and copy it to /opt/jdk1.8.0_171/. I installed it by these commands :
alternatives --install /usr/bin/java java /opt/jdk1.8.0_171/bin/java 2
alternatives --config java
after this java -version worked perfectly
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
all these works were done by root. In my user account when I enter java -version I got /usr/bin/java : Permission denied but when I use
usermod a+x /usr/bin/java
I get command not known. I exported $PATH = $PATH:/opt/jdk1.8.0_171/bin but it's not useful.
which java also returns /usr/bin/which: no java in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/esadeghi/.local/bin:/home/esadeghi/bin:/opt/jdk1.8.0_171/bin:/usr/bin/java:/opt/jdk1.8.0_171/bin)

You have to grant read permissions to your user with chmod and/or assign your user to the group and grant the permissions.
Also, you can install JDK (or JRE) using yum installer it should assign proper permissions automatically.
Or you can just download binary package from Oracle website, then unzip it to any appropriate folder and grant full read permissions (chmod -R 777 /jdk1.8.0_171) to the folder for everyone.

Related

run hadoop ERROR: JAVA_HOME /usr/bin/java does not exist

Ubuntu 16.04.1 LTS
I have installed java by sudo apt-get install default-jdk
$ java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~16.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
$ which java
/usr/bin/java
and in /home/hadoop/hadoop-3.3.1/etc/hadoop/hadoop-env.sh, I have added
export JAVA_HOME=/usr/bin/java
hadoop#ubuntu:~/hadoop$ bin/hadoop
ERROR: JAVA_HOME /usr/bin/java does not exist
anyone can help?
/usr/bin/java is not a java home. A java home must be a folder (not a program) with a bin directory which contains java, jps, maybe javac and so on. You must find your jre or jdk folder and set it as JAVA_HOME.
get ll /usr/bin/java and it may be a symbolic link to your Java path. or do find /usr/lib/jvm/java-1.x.x-openjdk to find your java home. The parent directory of Java program is your JAVA_HOME and must be set with export in hadoop-env.sh.
I could not run it with Java 11 installed so i installed Java 8 and tried this : /usr/lib/jvm/java-8-openjdk-amd64/jre/bin and it worked fine.
you could run readlink -f $(which java) command which returns the actual path to the target file.
This will give you an output like this
readlink -f $(which java)
/usr/lib/jvm/java-11-openjdk-amd64/bin/java

how to delete java 10 ? [duplicate]

How to downgrade Java from 9 to 8 on a macOS Sierra 10.12.6(16G29) . I tried removing the Java plugin and installed Java 8, however the Java and javac version shows 9 in terminal, but In system preferences it is 8.
You don't need to down grade. You can run more than one version of Java on MacOS. You can set the version of your terminal with this command in MacOS.
# List Java versions installed
/usr/libexec/java_home -V
# Java 11
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
# Java 1.8
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
# Java 1.7
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
# Java 1.6
export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)
You can set the default value in the .bashrc, .profile, or .zprofile
This is how I did it. You don't need to delete Java 9 or newer version.
Step 1: Install Java 8
You can download Java 8 from here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Step 2: After installation of Java 8. Confirm installation of all versions.Type the following command in your terminal.
/usr/libexec/java_home -V
Step 3: Edit .bash_profile
sudo nano ~/.bash_profile
Step 4: Add 1.8 as default. (Add below line to bash_profile file).
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
Now Press CTRL+X to exit the bash. Press 'Y' to save changes.
Step 5: Reload bash_profile
source ~/.bash_profile
Step 6: Confirm current version of Java
java -version
If you have multiple Java versions installed on your Mac, here's a quick way to switch the default version using Terminal. In this example, I am going to switch Java 10 to Java 8.
$ java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
10.0.1, x86_64: "Java SE 10.0.1" /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
1.8.0_171, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
Then, in your .bash_profile add the following.
# Java 8
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home
Now if you try java -version again, you should see the version you want.
$ java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
As it allows to install more than one version of java, I had install many 3 versions unknowingly but it was point to latest version "11.0.2"
I could able to solve this issue with below steps to move to "1.8"
$java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
cd /Library/Java/JavaVirtualMachines
ls
jdk1.8.0_201.jdk jdk1.8.0_202.jdk openjdk-11.0.2.jdk
sudo rm -rf openjdk-11.0.2.jdk
sudo rm -rf jdk1.8.0_201.jdk
ls
jdk1.8.0_202.jdk
java -version
java version "1.8.0_202-ea"
Java(TM) SE Runtime Environment (build 1.8.0_202-ea-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b03, mixed mode)
Old question but just had that problem /dumb jira having problems with java 10/ and didn't find a simple answer here so just gonna leave it:
$ /usr/libexec/java_home -V shows the versions installed and their locations so you can simply remove /Library/Java/JavaVirtualMachines/<the_version_you_want_to_remove>. Voila
The simplest solution might be to install Java 8 in parallel to Java 9 (if not still still existant) and specify the JVM to be used explicitly in eclipse.ini. You can find a description of this setting including a description how to find eclipse.ini on a Mac at Eclipsepedia
FOR MAC USERS only
Change for both JRE and JDK version
Path 1 is the JRE - installation path: /Library/Internet Plug-Ins/
Path 2 is the JDK - installation path: /Library/Java/JavaVirtualMachines
JRE Steps:
Change directory via Terminal to the Path 1.
cd /Library/Internet Plug-Ins/
Remove JavaAppletPlugin.plugin
sudo rm -rf JavaAppletPlugin.plugin
JDK Steps:
Do the Steps under JRE Steps
Change directory via Terminal to the Path 2.
step 1
/Library/Java/JavaVirtualMachines
Take a look at all installations at this folder
step 2
ls // to list all files in folder
Remove any installations in this folder.
step 3
sudo rm -rf /Library/Java/JavaVirtualMachines/yourjdkName.jdk
After uninstalling you are ready to go for a fresh installation. Just download you're version of choice and install it.
best solution for this
As mentioned in the offical Oracle Documentation there are two relevant paths you need to delete in case you wan't to remove a Java installation.
Path 1 is the JRE - installation path: /Library/Internet Plug-Ins/
Path 2 is the JDK - installation path: /Library/Java/JavaVirtualMachines
JRE Steps:
Change directory via Terminal to the Path 1.
cd /Library/Internet Plug-Ins/
Remove JavaAppletPlugin.plugin
sudo rm -rf JavaAppletPlugin.plugin
JDK Steps:
Do the Steps under JRE Steps
Change directory via Terminal to the Path 2.
/Library/Java/JavaVirtualMachines
Take a look at all installations at this folder
ls
Remove any installations in this folder.
sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-(...).jdk
After uninstalling you are ready to go for a fresh installation. Just download you're version of choice and install it.
You can remove "JavaAppletPlugin.plugin" found in Spotlight or Finder, then re-install downloaded Java 8.
This will simply solve your problem.

How I can play with EC2 API tools?

I'm getting the error to run the script "ec2-describe-regions"
$ec2-describe-regions
/usr/local/ec2/ec2-api-tools-1.6.14.1/bin/ec2-cmd: line 62: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java/bin/java: Not a directory
Before it, I did followings:
Open a terminal on my personal computer.
Install Java.
Create security credentials (an X.509 certificate and private key).
Set up a few environment variables.
Run my first command.
Here is the information I did:
$which java
/usr/bin/java
$echo $EC2_HOME
/usr/local/ec2/ec2-api-tools-1.6.14.1
$echo $JAVA_HOME
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
$java -version
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
Try the following:
mv /usr/bin/java /usr/bin/java.orig
ln -s /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java /usr/bin/java
You may have two or more java instances on your server. Make sure you use the same java executable where your JAVA_HOME points to.
This command uses the old CLI tools, i'd recommend you use the new, unified and greatly improved CLI:
https://aws.amazon.com/cli/

How to set or change the default Java (JDK) version on macOS?

How can you change the default version of Java on a mac?
First run /usr/libexec/java_home -V which will output something like the following:
Matching Java Virtual Machines (3):
1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
or you can specify just the major version, like:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Now when you run java -version you will see:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
Add the export JAVA_HOME… line to your shell’s init file.
For Bash (as stated by antonyh):
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
For Fish (as stated by ormurin)
set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
Updating the .zshrc file should work:
nano ~/.zshrc
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
Press CTRL+X to exit the editor
Press Y to save your changes
source ~/.zshrc
echo $JAVA_HOME
java -version
This answer is an attempt to address: 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 far as I can tell, none of the current answers do that (*).
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 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.
When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).
Details of investigation in this gist.
(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.
Adding to the above answers, I put the following lines in my .bash_profile (or .zshrc for MacOS 10.15+) which makes it really convenient to switch (including #elektromin's comment for java 9):
alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
After inserting, execute $ source .bash_profile
I can switch to Java 8 by typing the following:
$ j8
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
A small fish function based on /usr/libexec/java_home
function jhome
set JAVA_HOME (/usr/libexec/java_home $argv)
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
end
If you don't use fish, you can do something similar in bash:
#!/bin/bash
jhome () {
export JAVA_HOME=`/usr/libexec/java_home $#`
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
}
Then to switch between javas do:
$> jhome #switches to latest java
$> jhome -v 1.7 #switches to java 1.7
$> jhome -v 1.6 #switches to java 1.6
ref: https://gist.github.com/kenglxn/1843d552dff4d4233271
Update...
With macOS Monterey, v12.0.1 Cask is no longer a Brew command. Just follow the steps but in step 4 just do $ brew install ... or $ brew install --cask ... instead of $ brew cask install ... Thanks to #Shivam Sahil comment.
I will share my experiences with macOS Big Sur v11.4, the best way to deal with these problems is by installing java using Homebrew:
1 - Install Homebrew.
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2 - Install Homebrew Cask.
$ brew tap homebrew/cask-versions
$ brew update
$ brew tap homebrew/cask
3 - Install the latest version of Java
$ brew cask install java
4 - Install the other needed versions of Java (Java8, Java11, Java13).
$ brew tap adoptopenjdk/openjdk
$ brew cask install adoptopenjdk8
$ brew cask install adoptopenjdk11
$ brew cask install adoptopenjdk13
$ brew cask install adoptopenjdk14
5 - Switch between different versions of Java
Switching between different versions of Java, you only need to add the following to your .bash_profile or .zshrc.
In this case, we want to be able to switch between Java8, Java11, Java13 and Java14:
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_13_HOME=$(/usr/libexec/java_home -v13)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java13='export JAVA_HOME=$JAVA_13_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
# default to Java 14
java14
6 - Reload .bash_profile or .zshrc for the aliases to take effect:
$ source ~/.bash_profile
or
$ source ~/.zshrc
7 - Finally you can use the aliases to switch between different Java versions.
$ java8
$ java -version
java version "1.8.0_261"
For more info you can see this post: How to Use Brew to Install Java on Mac
Use jenv is an easy way. (Update 2022)
Install jenv: see Getting started
Install java with brew
brew install openjdk#11
ln -s /usr/local/opt/openjdk#11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
#other java
brew install openjdk#8
brew install openjdk#17
Add java to jenv
jenv add /Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home
Use: refer to jenv
It is a little bit tricky, but try to follow the steps described in Installing Java on OS X 10.9 (Mavericks). Basically, you gonna have to update your alias to java.
Step by step:
After installing JDK 1.7, you will need to do the sudo ln -snf in order to change the link to current java. To do so, open Terminal and issue the command:
sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents \
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
Note that the directory jdk1.7.0_51.jdk may change depending on the SDK version you have installed.
Now, you need to set JAVA_HOME to point to where jdk_1.7.0_xx.jdk was installed. Open again the Terminal and type:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"
You can add the export JAVA_HOME line above in your .bashrc file to have java permanently in your Terminal
install JDK, not just JRE
/usr/libexec/java_home -v 1.8
gives
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
next
touch .bash_profile
open -a TextEdit.app .bash_profile
TextEdit will show you a blank page which you can fill in.
add to doc:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
in terminal:
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
try the command:
javac -version
should output:
javac 1.8.0_111
tl;dr
Add the line:
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home'
to the file
~/.bash_profile
(replace jdk1.8.0_144.jdk with your downloaded version)
then source ~/.bash_profile
Use jenv, it is like a Java environment manager. It is super easy to use and clean
For Mac, follow the steps:
brew install jenv
git clone https://github.com/gcuisinier/jenv.git ~/.jenv
Installation: If you are using bash follow these steps:
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l
Add desired versions of JVM to jenv:
jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
jenv add /System/Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home
Check the installed versions:
jenv versions
Set the Java version you want to use by:
jenv global oracle64-1.6.0
Four easy steps using terminal for people who uses the default process.. :)
echo $JAVA_HOME gives you current java home. For eg: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/
cd /Library/Java/JavaVirtualMachines/ will take you to the folder where u normally install jdks (It might be different for your machines)
ls shows you available folders (normally it will have the version numbers, for eg: jdk1.8.0_191.jdk openjdk-11.0.2.jdk)
export JAVA_HOME='/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home' will change the java home..
Consider the following approach only to change the JDK for each and specific tab of your terminal (i.e: iTerm).
Having in the /Library/Java/JavaVirtualMachines path the two following jdks
openjdk8u275-b01
openjdk-11.0.9.1+1
And in the .bash_profile file the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-11.0.9.1+1/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
If you open Iterm - with the first Tab A - and the following commands are executed:
javac -version
javac 11.0.9.1
java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)
The output is correct and expected
But if you open a second Tab B and you need override the default JDK then write in the terminal the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk8u275-b01/Contents/Home/
export PATH=$JAVA_HOME/bin:$PATH
Then
javac -version
javac 1.8.0_275
java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.275-b01, mixed mode)
It works fine. Of course if the Tab B is closed or you open a new Tab C all work according the .bash_profile settings (therefore the default settings)
macOS El Capitan or newer will choose the higher version of the JDK available in /Library/Java/JavaVirtualMachines, so in order to downgrade you may rename the file Info.plist to something else like Info.plist.disabled so that the OS will choose a previous version.
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
put this in your ~/.profile and use it in your terminal like so setjdk 1.8, setjdk 1.7, setjdk 9 etc etc...
If you don't have removeFromPath then it is:
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
You can add it to your .bash_profile to have the version set by default.
//Open bash profile
open ~/.bash_profile
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
// run bash profile
source ~/.bash_profile
Add following in your ~/.bash_profile and set the default java version accordingly.
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
# default to Java 8
java8
I am using macOS and have installed java using brew.
Edit:
If you are not using bash please update the correct shell file, example, if you are using zsh then it will be ~/.zshrc
add following command to the ~/.zshenv file
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Add the following line of code to your .zshrc (or bash_profile):
alias j='f(){ export JAVA_HOME=/usr/libexec/java_home -v $1 };f'
Save to session:
$ source .zshrc
Run command (e.g. j 13, j14, j1.8...)
$ j 1.8
Explanation
This is parameterised so you do not need to update the script like other solutions posted. If you do not have the JVM installed you are told. Sample cases below:
/Users/user/IDE/project $ j 1.8
/Users/user/IDE/project $ java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
/Users/user/IDE/project $ j 13
/Users/user/IDE/project $ java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
/Users/user/IDE/project $ j 1.7
Unable to find any JVMs matching version "1.7".
Check Java version: java -version
Switch between versions: https://devqa.io/brew-install-java/
open ~/.bash_profile
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
source ~/.bash_profile
When we are switching to java11 or java8, java -version command is not showing the correct version.
In that case use mvn -version to see the correct java version is used for building the solution
From the Apple's official java_home(1) man page:
**USAGE**
/usr/libexec/java_home helps users set a $JAVA_HOME in their login rc files, or provides a way for
command-line Java tools to use the most appropriate JVM which can satisfy a minimum version or archi-
tecture requirement. The --exec argument can invoke tools in the selected $JAVA_HOME/bin directory,
which is useful for starting Java command-line tools from launchd plists without hardcoding the full
path to the Java command-line tool.
Usage for bash-style shells:
$ export JAVA_HOME=`/usr/libexec/java_home`
Usage for csh-style shells:
% setenv JAVA_HOME `/usr/libexec/java_home`
JDK Switch Script
I have adapted the answer from #Alex above and wrote the following to fix the code for Java 9.
$ cat ~/.jdk
#!/bin/bash
#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() {
echo "Switching java version $1";
requestedVersion=$1
oldStyleVersion=8
# Set the version
if [ $requestedVersion -gt $oldStyleVersion ]; then
export JAVA_HOME=$(/usr/libexec/java_home -v $1);
else
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;
fi
echo "Setting JAVA_HOME=$JAVA_HOME"
which java
java -version;
}
Switch to Java 8
$ jdk 8
Switching java version 8
Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
/usr/bin/java
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
Switch to Java 9
$ jdk 9
Switching java version 9
Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
/usr/bin/java
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
If still u are not able to set it. using this command.
export JAVA_HOME=/usr/libexec/java_home -v 1.8
then you have to use this one.
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
it will surely work.
First find out where do you store the environment variables-
emacs
bash_profile
zshrc file
Steps to Set up the environment variable :-
Download the jdk from JAVA
install it by double click
Now set-up environment variables in your file
a. For emacs.profile you can use this link OR see the screenshot below
b. For ZSH profile setup -
1. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
2. source ~/.zshrc - Restart zshrc to reflect the changes.
3. echo $JAVA_HOME - make sure path is set up properly
----> /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
4. java -version
--> java version "1.8.0_112" Java(TM) SE Runtime Environment (build 1.8.0_112-b16)Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
All set Now you can easily upgrade or degrade the JAVA version..
Previously I used alias'es in .zshrc for easy switching between versions but today I use SDKMAN.
SDKMAN can also handle setting default java for the system, and downloading and installing new java versions.
Once sdkman is installed you can then do commands similar to what is possible with the nvm tool for handling node versions.
sdk list java will list the java versions available on your system as well as available online for installation including their identifier that you can use in the sdk use, sdk default and sdk install commands.
e.g. to install Amazon Corretto 11.0.8 and ask if it should be the new default do this:
sdk install java 11.0.8-amzn
A feature I also use regularly is the .sdkmanrc file.
If you place that in a directory on your machine and run the sdk env command in the directory then you can configure tool versions used only in that directory. It is also possible to make sdkman switch tool versions automatically using the sdkman_auto_env=true configuration.
sdkman also supports handling other tools for the JVM such as gradle, kotlin, maven and more.
For more information check out https://sdkman.io/usage#env
With no additional tools to install, an easy way to have a separately installed JDK recognized as a first class member by /usr/libexec/java_home -V is to symlink it as follows:
sudo ln -s <path> /Library/Java/JavaVirtualMachines/jdk-[some-identifier].jdk
<path> is expected to be a directory containing subdirectories Contents/Home/bin etc.
A common use case is to register the JDK/JRE included with Android Studio:
The default location for the Java tools in recent versions of Android Studio on macOS is /Applications/Android\ Studio.app/Contents/jre. We will use jdk-android-studio.jdk as the identifier:
sudo ln -s /Applications/Android\ Studio.app/Contents/jre /Library/Java/JavaVirtualMachines/jdk-android-studio.jdk
Now, /usr/libexec/java_home -V will list it under Matching Java Virtual Machines:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
11.0.13 (arm64) "JetBrains s.r.o." - "OpenJDK 11.0.13" /Applications/Android Studio.app/Contents/jre/Contents/Home
/Applications/Android Studio.app/Contents/jre/Contents/Home
If you are using fish and you are using mac and you want to be able to switch between JDK's, then below works for me on mac.
#kenglxn's answer didn't work for me and I figured out it bcos didn't set -g which is global !
Put below under ~/.config/fish/config.fish
alias j8="jhome -v 1.8.0_162"
alias j9="jhome -v 9.0.1"
function jhome
set -g -x JAVA_HOME (/usr/libexec/java_home $argv)
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
end
funcsave jhome
To know which version /minor version you have installed, you can do :
/usr/libexec/java_home -V 579ms  Wed 14 Feb 11:44:01 2018
Matching Java Virtual Machines (3):
9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.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_121, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
If you have multiple versions and you want to run something by using a specific version, use this example:
/usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar
Based on #markhellewell answer I created a couple of alias functions that will do it for you. Just add these to your shell startup file
#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() {
echo "Switching java version";
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;
java -version;
}
https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4
Here is how I do it on my Linux (Ubuntu / Mint mate), I guess Mac can do it similarly.
Install & config
Steps:
[Basic - part]
Download jdk (the .tgz file) by hand.
Uncompress & rename properly, at a proper location.
e.g /mnt/star/program/java/jdk-1.8
Make a soft link, which will be changed to switch java version later.
e.g ln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
Thus /mnt/star/program/java/java is the soft link.
Set JAVA_HOME in a start script.
Could use file like /etc/profile.d/eric.sh, or just use ~/.bashrc.
e.g JAVA_HOME=/mnt/star/program/java/java
Then open a new bash shell. java -version should print the java version.
[More version - part]
Download & install more Java version, as need, similar as above steps.
e.g
/mnt/star/program/java/jdk-11
[Switch - part]
In ~/.bashrc, define variable for various Java version.
e.g
_E_JAVA_HOME_11='/mnt/star/program/java/jdk-11'
_E_JAVA_HOME_8='/mnt/star/program/java/jdk-8'
# dir of default version,
_E_JAVA_HOME_D=$_E_JAVA_HOME_8
In ~/.bashrc, define command to switch Java version.
e.g
## switch java version,
alias jv11="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_11 $JAVA_HOME"
alias jv8="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_8 $JAVA_HOME"
# default java version,
alias jvd="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_D $JAVA_HOME"
alias jv="java -version"
In terminal, source ~/.bashrc to make the changes take effect.
Then could switch using the defined commands.
Commands - from above config
Commands:
jv11
Switch to Java 11
jv8
Switch to Java 8
jvd
Switch to default Java version, which is denoted by _E_JAVA_HOME_D defined above.
jv
Show java version.
Example output:
eric#eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric#eric-pc:~$ jv11
eric#eric-pc:~$ jv
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
eric#eric-pc:~$ jvd
eric#eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric#eric-pc:~$
Mechanism
It switch by changing the soft link, which is used as JAVA_HOME.
Tips
On my machine when install jdk by hand, I keep the minor version, then make a soft link with the major version but without the minor version.
e.g
// this is the actual dir,
jdk1.8.0_191
// this is a soft link to jdk1.8.0_191
jdk-8
// this is a soft link to jdk-8 or jdk-11
java
I define command alias in ~/.bashrc, but define variable in a separate file.
I am using ~/.eric_var to define the variables, and ~/.bashrc will source it (e.g source $HOME/.eric_var).
add this function to bashrc or zshrc, java-change [version] to choose the JDK
# set and change java versions
function java-change() {
echo "----- old java version -----"
java -version
if [ $# -ne 0 ]; then
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
echo "----- new java version -----"
java -version
}

Installing Oracle Java on Linux Mint (instead of using OpenJDK) -- possible problems with linking

I apologize in advance for the long query that'll follow, but I'm very lost and wanted to include all pertinent information.
Here is the system I started with:
Linux Mint 14 Nadia MATE
64-bit Kernel
OpenJDK version 1.7.0_21
I wanted to install Oracle Java 7 and have that be my system default.
Here is what I tried:
1) Downloaded tar file of JDK1.7.0_21
2) Unpacked and moved it into /usr/lib/jvm/jdk1.7.0_21
3) cd /usr/lib/jvm
4) ln -s jdk1.7.0_21 java-7-oracle
5) Created file /usr/lib/jvm/.java-7-oracle.jinfo and proceed to populate it with info such as:
alias=java-7-oracle
priority=50
section=non-free
jre ControlPanel /usr/lib/jvm/java-7-oracle/jre/bin/ControlPanel
jre java /usr/lib/jvm/java-7-oracle/jre/bin/java
jre java_vm /usr/lib/jvm/java-7-oracle/jre/bin/java_vm
jre javaws /usr/lib/jvm/java-7-oracle/jre/bin/javaws
....
6) Then, for some perhaps ill-thoughtout reasons, I abandoned this approach in favor of a shorter one that I felt I better understood. So I...
7) Deleted the symlink I had created rm java-7-oracle hoping that it would only remove the symlink.
8) Proceeded to execute the following commands:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.*/bin/java 1065
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.*/bin/javac 1065
sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.7.*/bin/javaws 1065
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.*/bin/jar 1065
9) sudo update-alternatives --config java
And I picked this option:
/usr/lib/jvm/jdk1.7.0_21/bin/java 1065 manual mode
Afterwards, when I ran java -version I got errors saying Java Virtual Machine could not be started.
Then, I went in and messed around a bit more, changed the priorities to 1, etc..
I even made the following changes to my /etc/profile:
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_17
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
export PATH
And loaded it: . /etc/profile
But I still can't get java to work.
The difference, though is that now when I run java -version I get:
bash: /usr/bin/java: No such file or directory
I've also tried specifying the absolute path: /usr/lib/jvm/jdk1.7.0_21/bin/java -version
But to no avail.
Can someone please help me? I feel quite lost at this point.
And I can't seem to revert back to OpenJDK either.
Thanks!
The problem, it turns out was that I was using a 32-bit version of Java with my 64-bit system.
So I removed /usr/lib/jvm/jdk1.7.0_21
Then I went to Oracle and downloaded Java for Linux x64.
(I'd initially been mislead by their ambiguous "Java for Linux x86" labeling because my system was x86-64. Apparently their x86 refers to 32-bit systems.)
After downloading the correct 64-bit version, I followed the instructions from this site:
http://install-climber.blogspot.com/2013/02/linux-mint-14-nadia-mate-amd64-howto-install-oracle-java-jdk7.html
1. Check to see if your Ubuntu Linux operating system architecture is 32-bit or 64-bit, open up a terminal and run the following command below.
Type/Copy/Paste: file /sbin/init
Note the bit version of your Ubuntu Linux operating system architecture it will display whether it is 32-bit or 64-bit.
2. Check if you have Java installed on your system. To do this, you will have to run the Java version command from terminal.
Open up a terminal and enter the following command:
Type/Copy/Paste: java -version
If you have OpenJDK installed on your system it may look like this:
java version "1.7.0_15"
OpenJDK Runtime Environment (IcedTea6 1.10pre) (7b15~pre1-0lucid1)
OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)
If you have OpenJDK installed on your system, you have the wrong vendor version of Java installed for this exercise.
3. Completely remove the OpenJDK/JRE from your system and create a directory to hold your Oracle Java JDK/JRE binaries. This will prevent system conflicts and confusion between different vendor versions of Java. For example, if you have the OpenJDK/JRE installed on your system, you can remove it by typing the following at the command line:
Type/Copy/Paste:
sudo apt-get purge openjdk-\*
This command will completely remove OpenJDK/JRE from your system
Type/Copy/Paste:
sudo mkdir -p /usr/local/java
This command will create a directory to hold your Oracle Java JDK and JRE binaries.
4. Download the Oracle Java JDK for Linux. Make sure you select the correctcompressed binaries for your system architecture 32-bit or 64-bit (which end in tar.gz).
For example, if you are on Ubuntu Linux 32-bit operating system download 32-bit Oracle Java binaries.
For example, if you are on Ubuntu Linux 64-bit operating system download 64-bit Oracle Java binaries.
Optional, Download the Oracle Java JDK Documentation
Select jdk-7u45-apidocs.zip
Important Information: 64-bit Oracle Java binaries do not work on 32-bit Ubuntu Linux operating systems, you will receive multiple system error messages, if you attempt to install 64-bit Oracle Java on 32-bit Ubuntu Linux.
5. Copy the Oracle Java binaries into the /usr/local/java directory. In most cases, the Oracle Java binaries are downloaded to: /home/"your_user_name"/Downloads.
32-bit Oracle Java on 32-bit Ubuntu Linux installation instructions:
Type/Copy/Paste:
cd /home/"your_user_name"/Downloads
Type/Copy/Paste:
sudo cp -r jdk-7u45-linux-i586.tar.gz /usr/local/java
Type/Copy/Paste:
cd /usr/local/java
64-bit Oracle Java on 64-bit Ubuntu Linux installation instructions:
Type/Copy/Paste:
cd /home/"your_user_name"/Downloads
Type/Copy/Paste:
sudo cp -r jdk-7u45-linux-x64.tar.gz /usr/local/java
Type/Copy/Paste:
cd /usr/local/java
6. Run the following commands on the downloaded Oracle Java tar.gz files. Make sure to do this as root in order to make them executable for all users on your system.
To open a root terminal type
sudo -s you will be prompted for your logon password.
32-bit Oracle Java on 32-bit Ubuntu Linux installation instructions:
Type/Copy/Paste:
sudo chmod a+x jdk-7u25-linux-i586.tar.gz
64-bit Oracle Java on 64-bit Ubuntu Linux installation instructions:
Type/Copy/Paste:
sudo chmod a+x jdk-7u45-linux-x64.tar.gz
7. Unpack the compressed Java binaries, in the directory /usr/local/java
32-bit Oracle Java on 32-bit Ubuntu Linux installation instructions:
Type/Copy/Paste:
sudo tar xvzf jdk-7u25-linux-i586.tar.gz
64-bit Oracle Java on 64-bit Ubuntu Linux installation instructions:
Type/Copy/Paste:
sudo tar xvzf jdk-7u45-linux-x64.tar.gz
8. Double-check your directories. At this point, you should have two uncompressed binary directories in /usr/local/java for the Java JDK/JRE listed as:
Type/Copy/Paste: ls -a
jdk1.7.0_45
9. Edit the system PATH file /etc/profile and add the following system variables to your system path. Use nano, gedit or any other text editor, as root, open up /etc/profile.
Type/Copy/Paste:
sudo gedit /etc/profile
or
Type/Copy/Paste:
sudo nano /etc/profile
10. Scroll down to the end of the file using your arrow keys and add the following lines below to the end of your /etc/profile file:
Type/Copy/Paste:
JAVA_HOME=/usr/local/java/jdk1.7.0_45
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH
11. Save the /etc/profile file and exit.
12. Inform your Ubuntu Linux system where your Oracle Java JDK/JRE is located. This will tell the system that the new Oracle Java version is available for use.
Type/Copy/Paste:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.7.0_45/bin/java" 1
this command notifies the system that Oracle Java JRE is available for use
Type/Copy/Paste:
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_45/bin/javac" 1
this command notifies the system that Oracle Java JDK is available for use
Type/Copy/Paste:
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.7.0_45/bin/javaws" 1
this command notifies the system that Oracle Java Web start is available for use
13. Inform your Ubuntu Linux system that Oracle Java JDK/JRE must be the default Java.
Type/Copy/Paste:
sudo update-alternatives --set java /usr/local/java/jdk1.7.0_45/bin/java
this command will set the java runtime environment for the system
Type/Copy/Paste:
sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_45/bin/javac
this command will set the javac compiler for the system
Type/Copy/Paste:
sudo update-alternatives --set javaws /usr/local/java/jdk1.7.0_45/bin/javaws
this command will set Java Web start for the system
14. Reload your system wide PATH /etc/profile by typing the following command:
Type/Copy/Paste:
. /etc/profile
Note your system-wide PATH /etc/profile file will reload after reboot of your Ubuntu Linux system
15. Test to see if Oracle Java was installed correctly on your system. Run the following commands and note the version of Java:
16. A successful installation of 32-bit Oracle Java will display:
Type/Copy/Paste:
java -version
This command displays the version of java running on your system
You should receive a message which displays:
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_25-b25)
Java HotSpot(TM) Server VM (build 23.1-b03, mixed mode)
Type/Copy/Paste:
javac -version
This command lets you know that you are now able to compile Java programs from the terminal.
You should receive a message which displays:
javac 1.7.0_25
**17. A successful installation of Oracle Java 64-bit will display:
Type/Copy/Paste:
java -version
This command displays the version of java running on your system
You should receive a message which displays:
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_21-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
Type/Copy/Paste:
javac -version
This command lets you know that you are now able to compile Java programs from the terminal.
You should receive a message which displays:
javac 1.7.0_25
[https://www.wikihow.com/Install-Oracle-Java-JDK-on-Ubuntu-Linux][1]

Categories