I want to switch my java version with the command update-alternatives --config java, but I don't see any changes after that.
I'm using that command and have this output:
$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number: 1
Before it I have used Java 8 and selection was set on /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java. Then I switched, run java -version and have the same version as it was - Java 8:
$ java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment Corretto-8.212.04.1 (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM Corretto-8.212.04.1 (build 25.212-b04, mixed mode)
I changed JAVA_HOME and PATH to use Java 11, but it still changed nothing:
$ echo $PATH
/home/user9679/.sdkman/candidates/java/current/bin:/home/user9679/.sdkman/candidates/gradle
/current/bin:/home/user9679/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/
bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-1.11.0-openjdk-amd64/
$ echo $JAVA_HOME
/usr/lib/jvm/java-1.11.0-openjdk-amd64/
Probably you have made a change in your system that prevent update-alternatives to work.
Expected behavior
As I know, in a typical installation, the java command is a symbolic link in /usr/bin/java. You must exclude the "real" java path from the $PATH variable. If your computer use a java command in a different directory, the update-alternatives will not work.
-- location of the java command
$ type -p java
/usr/bin/java
-- real location of the java command
$ readlink -f /usr/bin/java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
If you get a different result for the type -p java command, you must check if the location of the java command is determined by some folder in the $PATH and remove that part.
NOTE: You may define the JAVA_HOME variable processing the /usr/bin/java symbolic link.
-- real location of the JAVA_PATH variable
$ dirname /usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-11-openjdk-amd64/bin/
$ dirname /usr/lib/jvm/java-11-openjdk-amd64/bin/
/usr/lib/jvm/java-11-openjdk-amd64/
-- setting the JAVA_HOME variable
$ export JAVA_HOME=`type -p java|xargs readlink -f|xargs dirname|xargs dirname`
Related
when I try to compile with ionic my application tells me the following error:
Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=undefined (recommended setting)
ANDROID_HOME=/home/jmarroni/Android/Sdk (DEPRECATED)
Requirements check failed for JDK 8 ('1.8.*')! Detected version: 11.0.2
Check your ANDROID_SDK_ROOT / JAVA_HOME / PATH environment variables.
[ERROR] An error occurred while running subprocess cordova.
Try installing the previous version of java 1.8 but do not recognize the change.
I’ve faced the same problem. In my case, two different JDK are installed
/usr/lib/jvm$ default-java java-1.11.0-openjdk-amd64 java-11-openjdk-amd64 java-1.8.0-openjdk-amd64 java-8-openjdk-amd64 openjdk-11
sudo update-alternatives --config java
sudo update-alternatives --config javac
I will put in one place all the information:
Version 8 of Java JDK. I was unable to find quickly the openjdk-8 version, so I decided to install Oracle version
Setting JAVA_HOME is not needed
Proceed to
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
Download:
jdk-8u251-linux-x64.tar.gz or newest
Login with oracle account
Uncompress it in /opt
sudo cp ~/Desktop/jdk-8u251-linux-x64.tar.gz /opt
cd /opt
sudo tar xfz jdk-8u251-linux-x64.tar.gz
sudo ln -s jdk1.8.0_251 java
Make java binary point to desired SDK
5.1. Install alternative
sudo update-alternatives --install /usr/bin/java java /opt/java/bin/java 2
5.2. Activate it
sudo update-alternatives --config java
5.3. Select the entry added in step 4.1.
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 auto mode
1 /opt/java/bin/java 2 manual mode
2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
3 /usr/lib/jvm/java-14-openjdk-amd64/bin/java 1411 manual mode
In this case type 1 and press Enter
5.4. Test
Type:
java -version
Expected output similar to:
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
Make javac binary point to desired SDK
6.1. Install alternative
sudo update-alternatives --install /usr/bin/javac javac /opt/java/bin/javac 2
6.2. Activate it
sudo update-alternatives --config javac
6.3. Select the entry added in step 6.1.
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-14-openjdk-amd64/bin/javac 1411 auto mode
* 1 /opt/java/bin/javac 2 manual mode
2 /usr/lib/jvm/java-14-openjdk-amd64/bin/javac 1411 manual mode
In this case type 1 and press Enter
6.4. Test
Type:
javac -version
Expected output similar to:
javac 1.8.0_251
if the output is similar to the output of "java -version", make sure you've written the correct command in section 6.1 with the missing "c" of the original post.
Worked for me.
PAY ATTENTION!
6.1 : the original post had a typo (a missing "c").
the command should be: "sudo update-alternatives --install /usr/bin/javac javac /opt/java/bin/javac 2"
I need to configure my Jenkins Server and I need to add Java path to Jenkins configuration.
yum install java-1.8.0-openjdk
However I'm new to lunix command, any suggestion please?
I dont know where yum install it!
See the validated answer!
try this if java command works
readlink -f $(which java)
They usually reside in /usr/lib/jvm. You can list them via ll /usr/lib/jvm. The value you need to enter in the field JAVA_HOME in jenkins is /usr/lib/jvm/java-1.8.0-openjdk.
Update
when I look at '/usr/lib/jvm' I get ...
What you see there is a list of symbolic links pointing to a similar target located in /etc/alternatives. In the end they all point to the same target. The difference is only the name which allows you to choose how explicit your choice if the target version shall be.
(Because java-1.8.0-openjdk is missing: Maybe you also need to install the package java-1.8.0-openjdk-devel.)
Here are the solution
# cd /opt/jdk1.7.0_79/
# alternatives --install /usr/bin/java java /opt/jdk1.7.0_79/bin/java 2
# alternatives --config java
# alternatives --install /usr/bin/jar jar /opt/jdk1.7.0_79/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.7.0_79/bin/javac 2
# alternatives --set jar /opt/jdk1.7.0_79/bin/jar
# alternatives --set javac /opt/jdk1.7.0_79/bin/javac
# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
# export JAVA_HOME=/opt/jdk1.7.0_79
# export PATH=$PATH:/opt/jdk1.7.0_79/bin
Thanks to tecadmin
Try echo $JAVA_HOME or look in vi ~/.bash_profile
Here is my system info
wangbo#ubuntu:~$ uname -a
Linux ubuntu 4.4.0-72-generic #93-Ubuntu SMP Fri Mar 31 14:07:41 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
When I check 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)
but when I echo $JAVA_HOME
/usr/lib/jvm/java-9-oracle
It is unmatched , It seems that I defined $JAVA_HOME somewhere , but I can`t found it , Anyone can help me how can I found my $JAVA_HOME defined
Other useful info :
I had install and uninstall jdk9 as first jdk before
It is not in ~/.bashrc
It is not in /etc/bash.bashrc
It is not in ~/.profile
UPDATE
wangbo#ubuntu:~$ more /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
1) JAVA_HOME is most probably defined in /etc/profile.d/jdk.sh and /etc/profile.d/jdk.csh These files are unfortunately not managed by the update-alternatives system.
2) Which executable you run should depend on the update-alternatives system, but if you have forced a particular version in your PATH it won’t.
Many java applications are actually invoked by scripts which choose their preferred jvm setting up a consistent environment, and shouldn’t be affected by your problem. It is best to have a consistent environment for free java invocations from the shell, however, and in most installations I know this is achieved manually...
Some java distributions don’t need JAVA_HOME at all. In other cases you need it. My advice is to define JAVA_HOME only when it turns out you need it. If it is the case, define it in a shell script and use that script to invoke the commands that you need that particular value for.
EDIT: /etc/profile.d/jdk.sh seems to be installed by oracle-java8-set-default.
# Uninstall the package (choose one)
sudo apt purge oracle-java8-set-default
sudo apt remove --purge oracle-java8-set-default
# Configure the desired Java binary
sudo update-alternatives --config java
sudo update-alternatives --config javac
you should better check /etc/environment
Your Java command is picked up from the PATH variable. Most probably, you have added Java7 to your path. Check your .profile, .bash_profile, /etc/profile and /etc/environment files.
JAVA_HOME is used by some scripts/tools (ant comes to mind) - to select a java command.
Try
sudo find / -path /sys -prune -o -path /proc -prune -o -type f -exec grep -l JAVA_HOME {} + 2>/dev/null
Sorry the first version of my answer will take forever. But this will take only some time, but it will find the file.
On my ubuntu 14.04 system, kernel 4.4.0-71-generic I used the following:
$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle
I am on Ubuntu and I have set the following in my ~/.bashrc file:
export JAVA_HOME=/opt/jdk1.8.0_91
export PATH=$JAVA_HOME/bin:$PATH
and then:
echo $JAVA_HOME
>/opt/jdk1.8.0_91
java -version
>java version "1.8.0_91"
>Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
>Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
at the first glance, the command sudo update-alternatives --config java was not showing my manually installed Java, so I installed it to the command with sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_91 1.
Now, the command sudo update-alternatives --config java drops down the list of all Java versions being installed like that:
0 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 auto mode
1 /opt/jdk1.7.0_51/bin/java 1 manual mode
* 2 /opt/jdk1.8.0_91 1 manual mode
3 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
4 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode
But switching between these using the prompt of the sudo update-alternatives --config java does not affect $JAVA_HOME and then $java -version.
My question is, what does sudo update-alternatives --config java exactly do after switching to another alternative with respect to the settings in the $JAVA_HOME$ variable?
It only changes a symlink located (on most distro I guess) at /etc/alternatives/java.
Absolutely NO change in the environment variable you set $JAVA_HOME is made.
First look at from where the command is found, you can do :
$which java
/usr/bin/java
The which command shows /usr/bin/java in my Debian distro. This file is a symlink which points to /etc/alternatives/java.
$ls -l /usr/bin | grep java
java -> /etc/alternatives/java
Then you follow the symlink :
$ls -l /etc/alternatives/java
/etc/alternatives/java -> /path/to/my/java/installation/1.x/bin/java
This shows that /etc/alternatives/java is another symlink.
When you do an update-alternatives on java, you just change this symlink target to another one.
Then, why doesn't the executed version change when you do the update-alternatives command ?
I guess it's because of the order the executables are found in $PATH.
Since you added a directory to the PATH environment variable, there are now two possible java executables : one in /usr/bin and the other in /opt/jdk1.8.0_9, but only the first one found will be taken into account when you'll type java commands.
And because you set
PATH=$JAVA_HOME/bin:$PATH
The first one will be found in $JAVA_HOME/bin aka /opt/jdk1.8.0_91 .
Because you made /opt/jdk1.8.0_9 appear before /usr/bin which is defined by default in the the PATH variable.
You can check it by typing in a terminal
$echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/path/to/my/java/installation/1.x/bin
You can see that my java/bin dir is located after the others defined in the PATH.
To correct this, you just have to concatenate $JAVA_HOME/bin after $PATH, like this :
PATH=$PATH:$JAVA_HOME/bin
This way you will be able to choose the default java executable from alternatives and the java exe found in $JAVA_HOME/bin will be discarded.
But to be consistent, in most cases you should choose the same java exe as in $JAVA_HOME/bin.
I installed the last image "RASPBIAN Debian Wheezy" on my Pi and it contains java 8
After that I installed java 7:
and I want to modify the java path from java 8 to java 7
So I updated my environment variables to:
export JAVA_HOME=/usr/lib/jvm/jdk-7-oracle-armhf/bin/java
export PATH=$PATH:/usr/lib/jvm/jdk-7-oracle-armhf/bin
I added them to my ~/.bashrc
but still when I execute java -version I got the java 8, even after reboot:
java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)
and when I display my envirnment variables, I got the java 7!:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/jdk-7-oracle-armhf/bin
pi#raspberrypi /usr/bin $ echo $JAVA_HOME
/usr/lib/jvm/jdk-7-oracle-armhf/bin/java
How can I update the path so that the command "java -version" gave me the java 7?
You need to use update-alternatives
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-7-oracle-armhf/bin/java" 1
sudo update-alternatives --set java /usr/lib/jvm/jdk-7-oracle-armhf/bin/java
If still having problems check here http://www.element14.com/community/docs/DOC-54112/l/java-on-the-raspberry-pi
Simply, your PATH is in the wrong order. The conventional way would be:
export JAVA_HOME=/usr/lib/jvm/jdk-7-oracle-armhf
export PATH=$JAVA_HOME/bin:$PATH
Note that I prepended your new java. Since PATH is searched in order, it would still find the old java first, otherwise.