Problem changing Java version using alternatives

后端 未结 6 962
遥遥无期
遥遥无期 2021-02-01 07:03

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 a

相关标签:
6条回答
  • 2021-02-01 07:31

    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

    0 讨论(0)
  • 2021-02-01 07:32

    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.

    0 讨论(0)
  • 2021-02-01 07:34

    I can change the default Java on CentOS using these steps:

    1. Add the IBM JDK:

      alternatives --install /usr/bin/java java /opt/WebSphere/AppServer/java/bin/java 3
      
    2. Set the new IBM JDK as default:

      alternatives --config java   (then select #3 in the list)
      
    3. Type java -version at the prompt to see the result.

    0 讨论(0)
  • 2021-02-01 07:42

    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.

    0 讨论(0)
  • 2021-02-01 07:43

    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.

    0 讨论(0)
  • 2021-02-01 07:54

    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
    
    0 讨论(0)
提交回复
热议问题