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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21115133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:41:01  来源:igfitidea点击:

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

javaclasspathubuntu-10.04

提问by Devendra Singh

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

下载最新的 .tar 文件后,我跑去tar zxvf jdk-7u45-linux-x64.tar.gz提取 java 文件。

Set the path in .bashrcfile (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 -versionor which java, java PATH still pointing to the older java version (java version "1.6.0_27").

现在,运行 commandjava -version或 which java, java PATH 仍然指向旧的 java 版本 ( java version "1.6.0_27")。

I know default ubuntu takes OpenJDK path. I have to change the path as latest version as my system environment variable set in .bashrcfile.

我知道默认的 ubuntu 使用 OpenJDK 路径。我必须将路径更改为最新版本作为我在.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

回答by Petter

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 javait finds.

OpenJDK 可能仍在路径中,Linux 将使用java它找到的第一个。

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

如果您不需要它,我建议您卸载 OpenJDK。

回答by Atropo

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

Ubuntu(和 Debian)有一种优雅的方式来管理像jdk.

Using update-alternativesyou can manage multiple jdklibraries 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 jdkas an alternative to the original one. Then you can choose which one you wan to use:

通过这种方式,您可以安装新的jdk作为原始安装的替代品。然后你可以选择你想使用哪一个:

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 javaor javacusing the --listoption:

您可以随时查看您有哪些替代方案javajavac使用该--list选项:

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

To see more options check the update-alternativesman page.

要查看更多选项,请查看update-alternatives手册页。

回答by gihandilanka

Try typing the following in your terminal.

尝试在终端中输入以下内容。

sudo update-alternatives --config java

sudo update-alternatives --config java

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

输出将是一些选择,您可以选择安装到您的计算机的正确选项。

回答by WillC

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; you can run

如果您想为每个 Java 任务使用不同的 JDK/JRE,您可以运行 update-alternatives 一次配置一个 Java 可执行文件;你可以跑

sudo  update-alternatives --config java[Tab]

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

查看可以配置的 Java 命令(java、javac、javah、javaws 等)。进而

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 answeryou 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。

回答by Praveen Kumar Verma

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 :

从命令行设置您要使用的 JRE 版本,就像 java 的可用版本安装:1.6 和 1.8
见下文:

enter image description here

在此处输入图片说明

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

选择 1 个选项并设置 java-6 版本,因为当前选择已在 java-8 中设置。