- Mac OSX
There is a lot of information on the web that talks about how to install java, but sometimes after install java you are left wondering if your environment is properly configured to use it. OSX and CentOS add a few layers of complexity to java installation and this article will give you the tools to uncover the truth about what version your shell environment is sourcing.
Verify and switch version context in OSX
- Check what versions are installed
MacBook-Pro:~ danl$ /usr/libexec/java_home -V Matching Java Virtual Machines (3): 1.7.0_60, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_60.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
- Check what version your environment is currently using
MacBook-Pro:~ danl$ java -version java version "1.7.0_60" Java(TM) SE Runtime Environment (build 1.7.0_60-b19) Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)
- Switch the version context to 1.6
MacBook-Pro:~ danl$ export JAVA_HOME=$(/usr/libexec/java_home -v 1.6) MacBook-Pro:~ danl$ java -version 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)
Verify and switch version context in CentOS
Sometimes after installing a RPM there will be some flavor of java that slides in because of dependencies. This is the most common scenario where your java installation gets hijacked.
- Check what versions are installed
[root@hdw3 ~]# java -version java version "1.5.0" gij (GNU libgcj) version 4.4.7 20120313 (Red Hat 4.4.7-4) Copyright (C) 2007 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- Oops! we have gcj java set as our default despite what we set for $JAVA_HOME. The reason is alternatives makes /usr/bin/java a soft link that points to a softlink which finally points to gcj JRE binaries. So your cli commands will use /usr/bin/java but normally your application services like hadoop will force java home to be whatever is defined in $JAVA_HOME bash environmental variable.
[root@hdw3 ~]# ls -l `which java` lrwxrwxrwx 1 root root 22 Mar 22 23:01 /usr/bin/java -> /etc/alternatives/java [root@hdw3 ~]# ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 35 Mar 22 23:01 /etc/alternatives/java -> /usr/lib/jvm/jre-1.5.0-gcj/bin/java
- Here is how to fix alternatives so /usr/java/bin points to the preferred version of java
- First check what alternatives thinks is installed. In this case alternatives thinks gcj is the only version installed.
alternatives --config java There is 1 program that provides 'java'. Selection Command ----------------------------------------------- *+ 1 /usr/lib/jvm/jre-1.5.0-gcj/bin/java Enter to keep the current selection[+], or type selection number:
- Compare alternatives to what actually is installed. we find that JDK 1.7 is installed and this is the default version want to use instead of GCJ
[root@hdw3 ~]# ls -l /usr/java/ total 4 lrwxrwxrwx 1 root root 16 Mar 22 22:44 default -> /usr/java/latest drwxr-xr-x 8 root root 4096 Mar 22 22:44 jdk1.7.0_45 lrwxrwxrwx 1 root root 21 Mar 22 22:44 latest -> /usr/java/jdk1.7.0_45
- Add jdk 1.7 to alternatives and set 1.7 as default
[root@hdw3 ~]# alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_45/bin/java 3 --slave /usr/bin/javac javac /usr/java/jdk1.7.0_45/bin/java [root@hdw3 ~]# [root@hdw3 ~]# alternatives --config java There are 2 programs which provide 'java'. Selection Command ----------------------------------------------- *+ 1 /usr/lib/jvm/jre-1.5.0-gcj/bin/java 2 /usr/java/jdk1.7.0_45/bin/java Enter to keep the current selection[+], or type selection number: 2 [root@hdw3 ~]# java -version java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)