如何将 Oracle 的 Java 设置为 Ubuntu 中的默认 Java?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6477415/
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-16 06:16:24  来源:igfitidea点击:

How to set Oracle's Java as the default Java in Ubuntu?

javaubuntujava-home

提问by Ashish Agarwal

How do I change the value of JAVA_HOMEin Ubuntu to point to Oracle's Java?

如何更改JAVA_HOMEUbuntu 中的值以指向 Oracle 的 Java?

Should it point to java-6-sunor java-6-sun-1.6.0.24?

它应该指向java-6-sun还是java-6-sun-1.6.0.24

采纳答案by srkiNZ84

I put the line:

我把这条线:

export JAVA_HOME=/usr/lib/jvm/java-7-oracle

in my ~/.bashrcfile.

在我的~/.bashrc文件中。

/usr/lib/jvm/java7-oracleshould be a symbolic link pointing to /usr/lib/jvm/java-7-oracle-[version number here].

/usr/lib/jvm/java7-oracle应该是指向/usr/lib/jvm/java-7-oracle-[version number here].

The reason it's a symbolic link is that in case there's a new version of the JVM, you don't need to update your .bashrcfile, it should automatically point to the new version.

它是符号链接的原因是,如果有新版本的 JVM,您不需要更新.bashrc文件,它应该自动指向新版本。

If you want to set JAVA_HOMEenvironment variables globally and at system level means use should set in /etc/environmentfile.

如果要在JAVA_HOME全局和系统级别设置环境变量,则表示使用应在/etc/environment文件中设置。

回答by Jits

If you want this environment variable available to all users and on system start then you can add the following to /etc/profile.d/java.sh(create it if necessary):

如果您希望此环境变量可供所有用户使用并在系统启动时使用,则可以将以下内容添加到/etc/profile.d/java.sh(如有必要,请创建它):

export JDK_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME=/usr/lib/jvm/java-7-oracle

Then in a terminal run:

然后在终端运行:

sudo chmod +x /etc/profile.d/java.sh
source /etc/profile.d/java.sh


My second question is - should it point to java-6-sun or java-6-sun-1.6.0.24 ?

我的第二个问题是 - 它应该指向 java-6-sun 还是 java-6-sun-1.6.0.24 ?

It should always point to java-7-oracleas that symlinks to the latest installed one (assuming you installed Java from the Ubuntu repositories and now from the download available at oracle.com).

它应该始终指向java-7-oracle最新安装的符号链接(假设您从 Ubuntu 存储库安装了 Java,现在从 oracle.com 上提供的下载)。

回答by metdos

If you want to change it globally and at system level;

如果您想在全局和系统级别更改它;

In

/etc/environment

add this line:

添加这一行:

JAVA_HOME=/usr/lib/jvm/java-7-oracle

回答by Roman Rhrn Nesterov

java 6

爪哇 6

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-amd64

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-amd64

or java 7

或 Java 7

export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64

export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64

回答by prosseek

If you want to use specific version of Java when multiple JDKs are installed, just setting JAVA_HOME may not work.

如果要在安装多个 JDK 时使用特定版本的 Java,仅设置 JAVA_HOME 可能不起作用。

You need to use sudo update-alternatives --config javato set default Java.

您需要使用sudo update-alternatives --config java来设置默认 Java。

Refer to https://askubuntu.com/questions/121654/how-to-set-default-java-version.

请参阅https://askubuntu.com/questions/121654/how-to-set-default-java-version

回答by user987339

If you're doing any sort of development you need to point to the JDK (Java Development Kit). Otherwise, you can point to the JRE (Java Runtime Environment).

如果您正在进行任何类型的开发,则需要指向 JDK(Java 开发工具包)。否则,您可以指向 JRE(Java 运行时环境)。

The JDK contains everything the JRE has and more. If you're just executing Java programs, you can point to either the JRE or the JDK.

JDK 包含 JRE 所拥有的一切以及更多。如果您只是在执行 Java 程序,则可以指向 JRE 或 JDK。

You should set JAVA_HOMEbased on current Java you are using. readlinkwill print value of a symbolic link for current Java and sedwill adjust it to JRE directory:

您应该JAVA_HOME根据您正在使用的当前 Java 进行设置。 readlink将打印当前 Java 符号链接的值sed并将其调整到 JRE 目录:

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

If you want to set up JAVA_HOME to JDK you should go up one folder more:

如果要将 JAVA_HOME 设置为 JDK,则应该再上一个文件夹:

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

回答by Barracuda

to set Oracle's Java SE Development Kit as the system default Java just download the latest Java SE Development Kit from herethen create a directory somewhere you like in your file system for example /usr/javanow extract the files you just downloaded in that directory:

要将 Oracle 的 Java SE 开发工具包设置为系统默认 Java 只需从此处下载最新的 Java SE 开发工具包,然后在文件系统中您喜欢的某个位置创建一个目录,例如/usr/java现在解压缩您刚刚在该目录中下载的文件:

$ sudo tar xvzf jdk-8u5-linux-i586.tar.gz -C /usr/java

now to set your JAVA_HOMEenvironment variable:

现在设置您的JAVA_HOME环境变量:

$ JAVA_HOME=/usr/java/jdk1.8.0_05/
$ sudo update-alternatives --install /usr/bin/java java ${JAVA_HOME%*/}/bin/java 20000
$ sudo update-alternatives --install /usr/bin/javac javac ${JAVA_HOME%*/}/bin/javac 20000

make sure the Oracle's java is set as default java by:

确保通过以下方式将 Oracle 的 java 设置为默认 java:

$ update-alternatives --config java

you get something like this:

你会得到这样的东西:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                           Priority   Status
------------------------------------------------------------
* 0            /opt/java/jdk1.8.0_05/bin/java                  20000     auto mode
  1            /opt/java/jdk1.8.0_05/bin/java                  20000     manual mode
  2            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      manual mode

Press enter to keep the current choice[*], or type selection number:

pay attention to the asterisk before the numbers on the left and if the correct one is not set choose the correct one by typing the number of it and pressing enter. now test your java:

注意左边数字前的星号,如果没有设置正确的数字,请输入数字并按回车键选择正确的数字。现在测试你的java:

$ java -version

if you get something like the following, you are good to go:

如果你得到类似以下的信息,你就可以开始了:

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) Server VM (build 25.5-b02, mixed mode)

also note that you might need root permission or be in sudoers group to be able to do this. I've tested this solution on both ubuntu 12.04 and Debian wheezy and it works in both of them.

另请注意,您可能需要 root 权限或在 sudoers 组中才能执行此操作。我已经在 ubuntu 12.04 和 Debian wheezy 上测试了这个解决方案,它在这两个版本中都有效。

回答by WillC

See this; 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。