Default JAVA, $JAVA_HOME vs sudo update-alternatives --config java - java

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.

Related

Command update-alternatives --config java change nothing

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`

How can I found where I defined JAVA_HOME

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

Hadoop Installation 2.6.0 on Ubuntu 14 - Java Error

EDIT
I am trying to install Hadoop 2.6.0 on my Ubuntu 14 machine. I am coming across an error though.
When I am trying to set the HOME variable for Java it does not seem to be doing as expected.
I am on my machine as hduser setup specifically for running and using Hadoop. This user is a sudoer.
Some information:
java -version' gives the following
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)
This is the only version installed on my machine, which can be seen by running the following command:
update-alternatives --display java
Which gives the following message:
java - auto mode
link currently points to /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java - priority 1071
slave java.1.gz: /usr/lib/jvm/java-7-openjdk-amd64/jre/man/man1/java.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java'.
I then go to the following path:
cd /usr/lib/jvm
and the I list out the contents ls
default-java java-1.7.0-openjdk-amd64 java-7-openjdk-amd64
I then type cd java* and pwd which brings up the following path:
/usr/lib/jvm/java-1.7.0-openjdk-amd64
Ok, so with that information, I then copy that directory into the .bashrc file as follows:
# The java implementation to use.
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
The hadoop-env.sh file I fill out as follows:
#Hadoop variables
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
I then at the terminal type source ~/.bashrc and then restart the terminal in order for it to set to the new Java path. When typing Hadoop -version I get the following output:
/usr/bin/hadoop: line 350: /usr/lib/jvm/java-6-sun/bin/java: No such file or directory
/usr/bin/hadoop: line 434: /usr/lib/jvm/java-6-sun/bin/java: No such file or directory
I do not know where to go from here.
Thank you,
Add JAVA_HOME to point to your openjdk in hadoop-env.sh. Add this line in hadoop-env.sh:
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
NOTE: Change JAVA_HOME path in .bashrc too
UPDATE I:
Run these commands in terminal. (This will set java & javac in /bin to use your jdk)
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java 1
sudo update-alternatives --config java
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/javac" 1
sudo update-alternatives --config javac
NOTE: If you dont have java and javac in the specified path, it will be inside /jre folder. Change it respectively.
It seems that you have installed java (I mean JRE/JDK here) 1.6, 1.7 and 1.8 at the same time. Which is comlpetelly ok since java installations are usually managed via alternatives subsystem.
The problem is that (contrary to your expectations) setting JAVA_HOME doesn't select which java is used. This is done via mentioned alternatives subsystem instead. The JAVA_HOME variable itself is only addtional configuration and doesn't have power to override what is being executed when one asks to start java process.
Moreover running cd java* isn't really a good idea unless you would like to go into first directory which starts with given string. Try to run ls java* to see my point. And again, it's ok to have multiple different versions of java here thanks to alternatives subsystem.
Another problem is that you are mixing different flavours of java which you probably don't have installed (java-8-oracle vs java-6-sun vs java-1.6.0-openjdk.
To check which java is installed, run:
alternatives --display java
And then based on the results, swich to one version of java and set JAVA_HOME accordingly.

How to remove old version of Java and install new version

I have a Linux box on which Java 1.7 is installed:
#java -version
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment (rhel-2.3.4.1.el6_3-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)
But for some new tool I need to install the jdk-8u45-linux-x64.tar.gz
So how can I remove old one and install the new one?
To remove OpenJDK (the one you've already installed)
sudo apt-get purge openjdk-\*
Make a new directory for your new JDK
sudo mkdir -p /usr/local/java
Copy the file to the directory (you should be in that file path)
sudo cp -r jdk-8u45-linux-x64.tar.gz /usr/local/java/
Extract the file
sudo tar xvzf jdk-8u45-linux-x64.tar.gz
You should add this to your PATH now. To do that:
a. Open /etc/profile : sudo gedit /etc/profile
b. Scroll down (the end) and add the path where your jdk was installed
JAVA_HOME=/usr/local/java/jdk1.8.0_45
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH
Save and exit
Inform your Linux system where your Oracle Java JDK/JRE is located.
a. Notify the system that Oracle Java JRE is available for use
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.8.0_45/bin/java" 1
b. Notify the system that Oracle Java JDK is available for use
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.8.0_45/bin/javac" 1
c. Notify the system that Oracle Java Web start is available for use
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.8.0_20/bin/javaws" 1
Inform your Linux system that Oracle Java JDK/JRE must be the default Java.
a. Set the java runtime environment for the system
sudo update-alternatives --set java /usr/local/java/jdk1.8.0_45/bin/java
b. Set the javac compiler for the system
sudo update-alternatives --set javac /usr/local/java/jdk1.8.0_45/bin/javac
c. Set Java Web start for the system
sudo update-alternatives --set javaws /usr/local/java/jdk1.8.0_20/bin/javaws
Reload your system wide PATH
source /etc/profile
Check the new version and you're done!
java -version
Just unpack the new Java version, for example into /opt. Then do
export JAVA_HOME=/opt/jdk1.8.0_45
export PATH=$JAVA_HOME/bin:$PATH
Put these exports into the startup files for your shell and you should be set. It is not necessary to uninstall the Java 7 installation.
On Centos/Redhat
First do : rpm -qa|grep jdk
And then : rpm -e <the version which you dont want>
I had java 7 installed on my ubuntu machine and wanted java 8. For that i followed the following steps
purge the older with sudo apt-get purge openjdk-\*
Add repository sudo add-apt-repository ppa:openjdk-r/ppa
Update the packages sudo apt-get update
Install java 8 with sudo apt-get install openjdk-8-jdk
To check list of java versions installed dosudo update-java-alternatives --list
Select default java for java sudo update-alternatives --config java
Select default java for javac sudo update-alternatives --config javac
check with java -version and javac -version to check if update version is installed
I referred following link Install OpenJDK8 on Unbuntu Trusty
Hope this helps!
I went to this article, the final option did it for me:
A final option for you to consider is yum remove libpcap, see how many packages it wants to remove (yum won't actually remove anything until you hit the Y button and press enter), since you may be able to remove these packages and use yum, if it turns out you couldn't after all, you can roll back with yum history undo command (I don't remember in what version of yum this was implemented in yum, so check first if this sub command is available to you). Also, make sure that you got your repositories pointing to the repos which has these files available.
After that I could remove my old jdk with:
sudo rpm -e java-1.8.0-openjdk-1.8.0.352.b08-2.el7_9

hadoop and java installing

i am trying to install java and hadoop
in Cantos
BUT
when I logging to
'vi .bashrc`
and i had enter these commands
export JAVA_HOME=/usr/lib/jdk1.7.0_51
export PATH=$PATH:$JAVA_HOME/bin
export HADOOP_HOME=/usr/lib/hadoop-0.20.2-cdh3u
export PATH=$PATH:$HADOOP_HOME/bin
after that i want to check the java version by writing this command
$ java -version
this message show to me
bash: java: java command not found
what i do ???????????
help please
^_^
To reflect changes made in .bashrc you have to logout or restart your system.
First of all, you don't need to export your environment variable on .bashrc, on second place
you have to use: /usr/java/latest/bin if you want to address you latest Java installation executables
third you can give a chance to your .bashrc after editing it or loging-off/login-in or
. .bashrc
and try to
echo $JAVA_HOME
echo $PATH
take a look at what whereis javac say you
I did install jdk 1.6 on centos. You can try same steps for jdk 1.7 :
1) Download JDK version 1.6.0_45 file (jdk-6u45-linux-x64.bin) for 64 bit machine from below link :
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html#jdk-6u45-oth-JPR
2) From terminal, go to the path where you have download these files :
cd /path
3) Change the mode of jdk bin file to executable using below cmd :
chmod a+x jdk-6u45-linux-x64.bin
4) Also change the mode of jre bin file to executable using below cmd :
chmod a+x jre-6u45-linux-x64.bin
5) Execute the bin files using
./jdk-6u45-linux-x64.bin
./jre-6u45-linux-x64.bin
This will create folders by jdk and jre names
6) Check default java configured on your machine using command :
alternatives --config java
Select the current selected version by '+' sign.
7) Run below command to remove the java.
alternatives --remove java /path given in the config command
8) Repeat above command until there is no output for config command. Repeat steps 7 to 10 for javac, javaws and libjavaplugin.
Now move local folders jdk1.6.0_45 and jre1.6.0_45 to /usr/java directory.
9) Install Sun/Oracle JDK java, javaws, libjavaplugin.so (for Firefox/Mozilla) and javac with alternatives –install command :
java
alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_45/jre/bin/java 20000
javaws
alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.6.0_45/jre/bin/javaws 20000
Java Browser (Mozilla) Plugin 32-bit
alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so
/usr/java/jdk1.6.0_45/jre/lib/i386/libnpjp2.so 20000
Java Browser (Mozilla) Plugin 64-bit
alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/jdk1.6.0_45/jre/lib/amd64/libnpjp2.so 20000
Install javac only if you installed JDK (Java Development Kit) package
alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_45/bin/javac 20000
alternatives --install /usr/bin/jar jar /usr/java/jdk1.6.0_45/bin/jar 20000
10) Install Sun/Oracle JRE java, javaws and libjavaplugin.so (for Firefox/Mozilla) with alternatives –install command
java
alternatives --install /usr/bin/java java /usr/java/jre1.6.0_45/bin/java 20000
javaws
alternatives --install /usr/bin/javaws javaws /usr/java/jre1.6.0_45/bin/javaws 20000
Java Browser (Mozilla) Plugin 32-bit
alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/jre1.6.0_45/lib/i386/libnpjp2.so 20000
Java Browser (Mozilla) Plugin 64-bit
alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/jre1.6.0_45/lib/amd64/libnpjp2.so 20000
11) Check current java, javac, javaws and libjavaplugin.so versions
java -version
javac -version
javaws
12) Now config following :
alternatives --config java # or javac or javaws or libjavaplugin.so
There are 4 programs which provide 'java'.
Selection Command
-----------------------------------------------
1 /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
2 /usr/lib/jvm/jre-1.5.0-gcj/bin/java
* 3 /usr/java/jdk1.6.0_18/jre/bin/java
+ 4 /usr/java/jdk1.6.0_45/jre/bin/java
Enter to keep the current selection[+], or type selection number:
Note: java with [+] is currently on use.
13) Add JAVA_HOME environment variable to /etc/profile file or $HOME/.bash_profile file
## export JAVA_HOME JDK ##
export JAVA_HOME="/usr/java/jdk1.6.0_45"
## export JAVA_HOME JRE ##
export JAVA_HOME="/usr/java/jre1.6.0_45"

Categories