tags:

views:

346

answers:

3

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 from 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/java

Enter 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/java

Enter 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.

A: 

Updated with more explanations

  1. 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 its PATH in this way. However, other packages/accounts (e.g. system processes) that don't hardcode a specific JDK version into its path will use the alternatives-specified JRE.

  2. 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.

Bert F
1. java is hashed (/usr/java/jdk1.5.0_10/bin/java)2. JAVA_HOME is set to /usr/java/jdk1.5.0_10
Brian Lewis
@Brian - okay, sounds like `/usr/java/jdk1.5.0_10` is hardcoded into your path before /usr/bin, so setting RH alternatives won't do anything while the `PATH` is set that way. For consistency, I would ask that you add update your question to show your PATH. Then take`/usr/java/jdk1.5.0_10` out of your `PATH`. The see what `java -version` gives. If it still gives J1.5 despite the `alternatives` setting, clear your JAVA_HOME env variable and test it again.
Bert F
@Bert - I updated my question. I also tried out your suggestion and removed $JAVA_HOME from my $PATH env var. When I reconnected to the server and executed `java -version`, I got v1.6 this time. However, now the problem is that `javac` gives me "-bash: javac: command not found".
Brian Lewis
@Brian `/usr/sbin/update-alternatives --config javac` - based on looking at http://www.redhat.com/docs/en-US/JBoss_SOA_Platform/5.0.0/html/Getting_Started_Guide/appe-install_jdk_rhel.html - Run alternatives for `jar` too
Bert F
@Bert - when I execute `/usr/sbin/alternatives --config javac` I don't get a prompt like when I do the same for java. I get nothing for jar as well.
Brian Lewis
@Brian - kind of weird - is `jar` broken like `javac` or does it work? Unfortunately, I don't have access to an RHEL5 system to investigate further. I'd update your question again with the info from our last few comments to garner some other responses.
Bert F
A: 

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

R. Bemrose
I do have JAVA_HOME in my PATH environment variable (not sure if that's the "path" you're referring to), but this didn't cause me any problems before when I used alternatives. I checked those two files (bashrc and bash_profile, but there was nothing in them referring to the JDK.
Brian Lewis
A: 

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.

Brian Lewis