There is a lot of information on the web that talks about how to install java, but sometimes after installing 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
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
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)
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 an 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.
[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.
[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
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:
[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
[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)