How to use the command update-alternatives --config java

后端 未结 8 1636
北恋
北恋 2020-11-28 22:05

I am installing Apache Solr on Linux Debian (Squeeze). I have been instructed to install sun-java jdk 1st. Then am told that I should use the command sudo update-alter

相关标签:
8条回答
  • 2020-11-28 22:42

    update-alternatives is problematic in this case as it forces you to update all the elements depending on the JDK.

    For this specific purpose, the package java-common contains a tool called update-java-alternatives.

    It's straightforward to use it. First list the JDK installs available on your machine:

    root@mylaptop:~# update-java-alternatives -l
    java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
    java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64
    

    And then pick one up:

    root@mylaptop:~# update-java-alternatives -s java-1.7.0-openjdk-amd64
    
    0 讨论(0)
  • 2020-11-28 22:44

    Have a look at https://wiki.debian.org/JavaPackage At the bottom of this page an other method is descibed using a command from the java-common package

    0 讨论(0)
  • 2020-11-28 22:56

    There are many other binaries that need to be linked so I think it's much better to try something like sudo update-alternatives --all and choosing the right alternatives for everything else besides java and javac.

    0 讨论(0)
  • 2020-11-28 22:59

    This is how I install jdk

    #!/bin/bash
    cd /opt/
    sudo mkdir java
    sudo tar -zxvf ~/Downloads/jdk-8u192-linux-x64.tar.gz
    sudo ln -s  jdk1.8.0_192 current
    for file in /opt/java/current/bin/*
    do
       if [ -x $file ]
       then
          filename=`basename $file`
          sudo update-alternatives --install /usr/bin/$filename $filename $file 20000
          sudo update-alternatives --set $filename $file
          #echo $file $filename
       fi
    done
    
    0 讨论(0)
  • 2020-11-28 23:04

    I'm using Ubuntu 18.04 LTS. Most of the time, when I change my java version, I also want to use the same javac version.
    I use update-alternatives this way, using a java_home alternative instead :

    Installation

    Install every java version in /opt/java/<version>, for example

    ~$ ll /opt/java/
    total 24
    drwxr-xr-x 6 root root 4096 jan. 22 21:14 ./
    drwxr-xr-x 9 root root 4096 feb.  7 13:40 ../
    drwxr-xr-x  8 stephanecodes stephanecodes 4096 jan.   8  2019 jdk-11.0.2/
    drwxr-xr-x  7 stephanecodes stephanecodes 4096 dec.  15  2018 jdk1.8.0_201/
    

    Configure alternatives

    ~$ sudo update-alternatives --install /opt/java/current java_home /opt/java/jdk-11.0.2/ 100
    ~$ sudo update-alternatives --install /opt/java/current java_home /opt/java/jdk1.8.0_201 200
    

    Declare JAVA_HOME (In this case, I use a global initialization script for this)

    ~$ sudo sh -c 'echo export JAVA_HOME=\"/opt/java/current\" >> environment.sh'
    

    Log Out or restart Ubuntu (this will reload /etc/profile.d/environment.sh)

    Usage

    Change java version

    Choose the version you want to use

    ~$ sudo update-alternatives --config java_home
    
    There are 2 choices for the alternative java_home (providing /opt/java/current).
    
      Selection    Path                    Priority   Status
    ------------------------------------------------------------
      0            /opt/java/jdk-11.0.2     200       auto mode
      1            /opt/java/jdk-11.0.2     200       manual mode
    * 2            /opt/java/jdk1.8.0_201   100       manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 
    

    Check version

    ~$ java -version
    openjdk version "11.0.2" 2019-01-15
    OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
    OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
    
    ~$ javac -version
    javac 11.0.2
    

    Tip

    Add the following line to ~/.bash_aliases file :

    alias change-java-version="sudo update-alternatives --config java_home && java -version && javac -version"
    

    Now use the change-java-version command to change java version

    0 讨论(0)
  • 2020-11-28 23:05

    You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
    Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:

    When using the --config option, alternatives will list all of the choices for the link group of which given name is the master link. You will then be prompted for which of the choices to use for the link group. Once you make a change, the link group will no longer be in auto mode. You will need to use the --auto option in order to return to the automatic state.

    And I believe auto mode is set when you install the first/only JRE/JDK.

    0 讨论(0)
提交回复
热议问题