How To Switch Java in Centos

* If you need to install a new version of java, download the version from java.sun
* run /usr/sbin/alternatives to change the default java. If you will run the next command you will see which java versions are available:

# /usr/sbin/alternatives --config java
There is 1 programs which provide 'java'.
Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
Enter to keep the current selection[+], or type selection number:

Add the new java to the list of alternatives:

# /usr/sbin/alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_15/bin/java 2

Choose the new java as the default:

# /usr/sbin/alternatives --config java
There are 2 programs which provide 'java'.
Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
   2           /usr/java/jdk1.6.0_15/bin/java
Enter to keep the current selection[+], or type selection number: 2

The same thing you should do with for javac:

# /usr/sbin/alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_15/bin/javac 2
# /usr/sbin/alternatives --config javac
There are 2 programs which provide 'javac'.
Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/java-1.4.2-gcj/bin/javac
   2           /usr/java/jdk1.6.0_15/bin/javac
Enter to keep the current selection[+], or type selection number: 2
2 Comments
  1. free says:

    I followed this link for java on CentOs
    http://wiki.centos.org/HowTos/JavaOnCentOS
    After completing all steps
    when I execute command java. I get correct output
    But when I do javac … command not found.
    I tried above mentioned options also.
    which javac… give not found

  2. This is an excellent post. Thank you. Using the alternatives config, I have three different versions of java installed on my Centos 6 server that I can toggle between very easily for running Tomcat properly.

    I was previously encountering an error in Tomcat — java.lang.ClassNotFoundException: [Ljava.lang.String;

    The problem was that even though I had tied the JAVA_HOME environment variable to the desired version of Java, the environmental vars for the user starting tomcat were still somehow getting involved. By following your steps on setting up ‘alternatives’ for java and javac, I was able to fix this problem in Tomcat.

    Thanks again!

    RS

Leave a Reply

*