简体   繁体   English

Ubuntu:将路径从OpenJDK 6更改为Oracle JDK 7

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

After downloading the latest .tar file I ran tar zxvf jdk-7u45-linux-x64.tar.gz to extract java files. 下载最新的.tar文件后,我运行tar zxvf jdk-7u45-linux-x64.tar.gz来提取java文件。

Set the path in .bashrc file ( vi ~/.bashrc ) as below; .bashrc文件( vi ~/.bashrc )中设置路径如下;

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_45/bin/java
export PATH=$PATH:/usr/lib/jvm/jdk1.7.0_45/bin
export JDK_HOME=/usr/lib/jvm/jdk1.7.0_45    
export JRE_HOME=/usr/lib/jvm/jre1.7.0_45

Now, running command java -version or which java, java PATH still pointing to the older java version ( java version "1.6.0_27" ). 现在,运行命令java -version或者java,java PATH仍然指向较旧的java版本( java version "1.6.0_27" )。

I know default ubuntu takes OpenJDK path. 我知道默认的ubuntu采用OpenJDK路径。 I have to change the path as latest version as my system environment variable set in .bashrc file. 我必须将路径更改为.bashrc文件中设置的系统环境变量的最新版本。

Also, sudo update-alternatives --config java 另外, sudo update-alternatives --config java

Selection    Path                                      Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-7-oracle/jre/bin/java    1062      auto mode
  1            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java    1062      manual mode
* 3            /usr/lib/jvm/jdk1.7.0_45/bin/java          1         manual mode
Press enter to keep the current choice[*], or type selection number: 3

Ubuntu (and Debian) have an elegant way to manage libraries like the jdk . Ubuntu(和Debian)有一种优雅的方式来管理像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. 使用update-alternatives您可以在同一系统上管理多个jdk库,选择要用作主要库的库。

First you have to install an alternative for the new installed jdk: 首先,您必须为新安装的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. 通过这种方式,您可以安装新的jdk作为原始jdk的替代方案。 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: 您将被要求在我的系统上选择您要使用的jdk:

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: 您可以随时使用--list选项查看javajavac替代方法:

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

To see more options check the update-alternatives man page. 要查看更多选项,请查看update-alternatives手册页。

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. OpenJDK可能仍在路径中,Linux将使用它找到的第一个java

If you don't need it, I would recommend uninstalling OpenJDK. 如果你不需要它,我建议你卸载OpenJDK。

Run

sudo  update-java-alternatives --list

to list off all the Java installations on a machine by name and directory, and then run 按名称和目录列出计算机上的所有Java安装,然后运行

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

to choose which JRE/JDK to use. 选择要使用的JRE / JDK。

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; 如果要为每个Java任务使用不同的JDK / JRE,可以运行update-alternatives来一次配置一个java可执行文件; you can run 你可以跑

sudo  update-alternatives --config java[Tab]

to see the Java commands that can be configured (java, javac, javah, javaws, etc). 查看可配置的Java命令(java,javac,javah,javaws等)。 And then 然后

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

will associate that Java task/command to a particular JDK/JRE. 将该Java任务/命令与特定的JDK / JRE相关联。

You may also need to set JAVA_HOME for some applications: from this answer you can use 您可能还需要为某些应用程序设置JAVA_HOME:您可以使用此答案

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

for JREs, or 对于JRE,或

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

for JDKs. 对于JDK。

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. 输出将是一些选择,您可以选择安装到您的计算机的正确的一个。

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 从命令行设置要使用的JRE版本,就像java:1.6和1.8的可用版本安装一样
See below : 见下文 :

在此输入图像描述

and choose 1 option and set java-6 version because already current choice set in java-8. 选择1选项并设置java-6版本,因为已经在java-8中设置了当前选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM