Problem changing Java version using alternatives
I'm not quite sure how I got into this mess, but for some reason I'm not able to change the current version of Java using alternatives. I can run alternatives --config java and type my selection but when I echo the version number for either java or javac, it spits back out 1.5 every time (despite alternatives showing the current version is 1.6). The server I'm working with is running RHEL5, by the way.
I have verified that the paths used in alternatives are pointing to the correct directories. Here's some output f开发者_JAVA百科rom my session:
[brilewis@myserver]$ sudo /usr/sbin/update-alternatives --config java
There are 3 programs which provide 'java'.
Selection Command
** 1 /usr/lib/jvm/jre-1.4.2-gcj/bin/java
+ 2 /usr/java/jdk1.5.0_10/bin/java 3 /usr/java/jdk1.6.0_16/bin/javaEnter to keep the current selection[+], or type selection number: 3
[brilewis@myserver]$ java -version
java version "1.5.0_10" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_10-b03) Java HotSpot(TM) Server VM (build 1.5.0_10-b03, mixed mode)
[brilewis@myserver]$ sudo /usr/sbin/update-alternatives --config java
There are 3 programs which provide 'java'.
Selection Command
** 1 /usr/lib/jvm/jre-1.4.2-gcj/bin/java
2 /usr/java/jdk1.5.0_10/bin/java + 3 /usr/java/jdk1.6.0_16/bin/javaEnter to keep the current selection[+], or type selection number:
UPDATE: The following is the output of echo $PATH
:
/usr/java/jdk1.5.0_10/bin:/usr/local/apache-ant-1.7.1/bin:/usr/local/apache-tomcat-6.0.24:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/NX/bin:/home/brilewis/bin
UPDATE (4/26/10): I followed Bert's suggestion and removed JAVA_HOME from the PATH environment var in /etc/profile. After doing this, I was able to use alternatives to change the version of Java. The only problem is that when I try to run javac
, I get "-bash: javac: command not found". This does not happen when the version is set to 1.5.
For Oracle Java 6u30, once you've installed their RPMs you can configure alternatives:
/usr/sbin/alternatives --install "/usr/bin/java" "java" "/usr/java/default/bin/java" 2 \
--slave /usr/bin/javac javac /usr/java/default/bin/javac \
--slave /usr/bin/javadoc javadoc /usr/java/default/bin/javadoc \
--slave /usr/bin/jar jar /usr/java/default/bin/jar \
--slave /usr/bin/keytool keytool /usr/java/default/bin/keytool \
--slave /usr/bin/orbd orbd /usr/java/default/bin/orbd \
--slave /usr/bin/pack200 pack200 /usr/java/default/bin/pack200 \
--slave /usr/bin/rmid rmid /usr/java/default/bin/rmid \
--slave /usr/bin/rmiregistry rmiregistry /usr/java/default/bin/rmiregistry \
--slave /usr/bin/servertool servertool /usr/java/default/bin/servertool \
--slave /usr/bin/tnameserv tnameserv /usr/java/default/bin/tnameserv \
--slave /usr/bin/unpack200 unpack200 /usr/java/default/bin/unpack200 \
--slave /usr/share/man/man1/java.1.gz java.1.gz /usr/java/default/man/man1/java.1.gz \
--slave /usr/share/man/man1/keytool.1.gz keytool.1.gz /usr/java/default/man/man1/keytool.1.gz \
--slave /usr/share/man/man1/orbd.1.gz orbd.1.gz /usr/java/default/man/man1/orbd.1.gz \
--slave /usr/share/man/man1/pack200.1.gz pack200.1.gz /usr/java/default/man/man1/pack200.1.gz \
--slave /usr/share/man/man1/rmid.1.gz rmid.1.gz /usr/java/default/man/man1/rmid.1.gz \
--slave /usr/share/man/man1/rmiregistry.1.gz rmiregistry.1.gz /usr/java/default/man/man1/rmiregistry.1.gz \
--slave /usr/share/man/man1/servertool.1.gz servertool.1.gz /usr/java/default/man/man1/servertool.1.gz \
--slave /usr/share/man/man1/tnameserv.1.gz tnameserv.1.gz /usr/java/default/man/man1/tnameserv.1.gz \
--slave /usr/share/man/man1/unpack200.1.gz unpack200.1.gz /usr/java/default/man/man1/unpack200.1.gz
Then activate the configuration:
/usr/sbin/alternatives --config java
And select /usr/java/default/bin/java from the menu.
Plus you must gzip the man pages
gzip /usr/java/default/man/man1/*.1
Also, the Oracle java RPMs might have clobbered your alternatives symlink so force it to be normal.
ln -sf /etc/alternatives/java /usr/bin/java
I can change the default Java on CentOS using these steps:
Add the IBM JDK:
alternatives --install /usr/bin/java java /opt/WebSphere/AppServer/java/bin/java 3
Set the new IBM JDK as default:
alternatives --config java (then select #3 in the list)
Type
java -version
at the prompt to see the result.
The only way I was able to solve the problem was to start over again by removing /var/lib/alternatives/java and installing each JDK again. I did the same for javac and jar. After doing this, I was able to switch between versions without any issues.
alternatives
works by changing a symlink in the /usr/bin
directory. However, if your path contains a valid executable earlier in the path, that will be used instead.
In this case, judging from your previous comments, it sounds like /usr/java/jdk1.5.0_10/bin
is somewhere in the path and should be removed.
For a BASH shell, the path is usually set in ~/.bashrc
or (less likely?) ~/.bash_profile
Updated with more explanations
Check which
java
executable is really running, e.g.$ type java
If this shows something other than
/usr/bin/java
, then you've likely got a specific JRE/JDK hardcoded in your path. This is fine, but you won't be able to use change Java versions using RH alternatives for any account that hardcodes a specific JRE/JDK in itsPATH
in this way. However, other packages/accounts (e.g. system processes) that don't hardcode a specific JDK version into its path will use thealternatives
-specified JRE.Check your
JAVA_HOME
environment variable, e.g.$ echo $JAVA_HOME
If this is set, this will sometimes point the java executable at a different JRE/JDK, regardless of where the
java
executable itself lives. Again, its not unusual to set this, but you won't be able to use change Java versions using RH alternatives for any account that hardcodes a different JAVA_HOME.
All that said, for development in my account, I normally set a specific JDK in my path and set JAVA_HOME to point to a specific JDK, rather than rely on the system settings. RH alternatives is fine to control what Java version other packages use, but for my own development, I like to explicitly target the Java I want to use.
When using the alternatives method, if you want to use an alternative, it must first be installed. I believe RPMs would include this as a part of the install, but in the case where manual installs are performed, you can still manually install. For example, with java,
alternatives --install "/usr/bin/java" "java" "/usr/java/example/bin/java" \
--slave /usr/bin/javac javac /usr/java/example/bin/javac \
...
What this does is to install an alternative for the java symlink. For each slave, it also builds/updates the symlink to some other value specified in another alternative, when you switch it. So if you were to switch to a different version of java using alternatives, and the alternative specifies a slave for javac, javac will repoint to the new version as well.
My full list is as follows:
alternatives --install /usr/bin/java java /usr/java/<version>/bin/java 1500 \
--slave /usr/bin/ControlPanel ControlPanel /usr/java/<version>/jre/bin/ControlPanel \
--slave /usr/bin/jar jar /usr/java/<version>/bin/jar \
--slave /usr/bin/javac javac /usr/java/<version>/bin/javac \
--slave /usr/bin/javaws javaws /usr/java/<version>/bin/javaws \
--slave /usr/bin/jcontrol jcontrol /usr/java/<version>/bin/jcontrol \
--slave /usr/bin/keytool keytool /usr/java/<version>/bin/keytool \
--slave /usr/bin/orbd orbd /usr/java/<version>/bin/orbd \
--slave /usr/bin/pack200 pack200 /usr/java/<version>/bin/pack200 \
--slave /usr/bin/policytool policytool /usr/java/<version>/bin/policytool \
--slave /usr/bin/rmid rmid /usr/java/<version>/bin/rmid \
--slave /usr/bin/rmiregistry rmiregistry /usr/java/<version>/bin/rmiregistry \
--slave /usr/bin/servertool servertool /usr/java/<version>/bin/servertool \
--slave /usr/bin/tnameserv tnameserv /usr/java/<version>/bin/tnameserv \
--slave /usr/bin/unpack200 unpack200 /usr/java/<version>/bin/unpack200 \
--slave /usr/share/man/man1/java.1 java.1 /usr/java/<version>/man/man1/java.1 \
--slave /usr/share/man/man1/javac.1 javac.1 /usr/java/<version>/man/man1/javac.1 \
--slave /usr/share/man/man1/javaws.1 javaws.1 /usr/java/<version>/man/man1/javaws.1 \
--slave /usr/share/man/man1/keytool.1 keytool.1 /usr/java/<version>/man/man1/keytool.1 \
--slave /usr/share/man/man1/orbd.1 orbd.1 /usr/java/<version>/man/man1/orbd.1 \
--slave /usr/share/man/man1/pack200.1 pack200.1 /usr/java/<version>/man/man1/pack200.1 \
--slave /usr/share/man/man1/policytool.1 policytool.1 /usr/java/<version>/man/man1/policytool.1 \
--slave /usr/share/man/man1/rmid.1 rmid.1 /usr/java/<version>/man/man1/rmid.1 \
--slave /usr/share/man/man1/rmiregistry.1 rmiregistry.1 /usr/java/<version>/man/man1/rmiregistry.1 \
--slave /usr/share/man/man1/servertool.1 servertool.1 /usr/java/<version>/man/man1/servertool.1 \
--slave /usr/share/man/man1/tnameserv.1 tnameserv.1 /usr/java/<version>/man/man1/tnameserv.1 \
--slave /usr/share/man/man1/unpack200.1 unpack200.1 /usr/java/<version>/man/man1/unpack200.1
Hope this helps.
精彩评论