Ubuntu: change the path from OpenJDK 6 to Oracle JDK 7

前端 未结 5 1183
花落未央
花落未央 2020-12-13 20:25

After downloading the latest .tar file I ran tar zxvf jdk-7u45-linux-x64.tar.gz to extract java files.

Set the path in .bashrc file (

相关标签:
5条回答
  • 2020-12-13 20:32

    Ubuntu (and Debian) have an elegant way to manage libraries like the jdk.

    Using update-alternatives you can manage multiple jdk libraries on the same system, choosing which one you want to use as the main one.

    First you have to install an alternative for the new installed jdk:

    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_45/bin/java" 1
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_45/bin/javac" 1
    

    In this way you install the new jdk as an alternative to the original one. Then you can choose which one you wan to use:

    sudo update-alternatives --config java
    sudo update-alternatives --config javac
    

    You will be asked to choose which jdk you want to use, on my system I have:

    There are 2 choices for the alternative java (providing /usr/bin/java).
    
      Selection    Path                                           Priority   Status
    ------------------------------------------------------------
      0            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      auto mode
      1            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      manual mode
    * 2            /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java   1051      manual mode
    
    Press enter to keep the current choice[*], or type selection number: 
    

    At any time you can see what alternatives you have for java or javac using the --list option:

    sudo update-alternatives --list java
    sudo update-alternatives --list javac
    

    To see more options check the update-alternatives man page.

    0 讨论(0)
  • 2020-12-13 20:38

    Try typing the following in your terminal.

    sudo update-alternatives --config java

    The output will be some choices and you can select the correct one which installed to your computer.

    0 讨论(0)
  • 2020-12-13 20:41

    You probably want to do

    export PATH=/usr/lib/jvm/jdk1.7.0_45/bin:$PATH
    

    OpenJDK is probably still in the path, and Linux will use the first java it finds.

    If you don't need it, I would recommend uninstalling OpenJDK.

    0 讨论(0)
  • 2020-12-13 20:45

    Running command in terminal:

    sudo update-alternatives --config java 
    

    from the command line to set the version of the JRE you want to use as like available version install of java : 1.6 and 1.8
    See below :

    and choose 1 option and set java-6 version because already current choice set in java-8.

    0 讨论(0)
  • 2020-12-13 20:47

    Run

    sudo  update-java-alternatives --list
    

    to list off all the Java installations on a machine by name and directory, and then run

    sudo  update-java-alternatives --set [JDK/JRE name e.g. java-8-oracle]
    

    to choose which JRE/JDK to use.

    If you want to use different JDKs/JREs for each Java task, you can run update-alternatives to configure one java executable at a time; you can run

    sudo  update-alternatives --config java[Tab]
    

    to see the Java commands that can be configured (java, javac, javah, javaws, etc). And then

    sudo  update-alternatives --config [javac|java|javadoc|etc.]
    

    will associate that Java task/command to a particular JDK/JRE.

    You may also need to set JAVA_HOME for some applications: from this answer you can use

    export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
    

    for JREs, or

    export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")
    

    for JDKs.

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