在linux上安装jdk的正确方法是什么

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

What's the correct way of installing jdk on linux

linuxjava

提问by Moonlit

I am running the debian based Crunchbang linux system and i want to install the Oracle JDK(not openjdk) on my system.

我正在运行基于 debian 的 Crunchbang linux 系统,我想在我的系统上安装Oracle JDK(不是 openjdk)。

I've spent some time to google for it, but i was not able to find a clear description of how to install (and configure) it. So here are my questions:

我花了一些时间在谷歌上搜索它,但我找不到关于如何安装(和配置)它的清晰描述。所以这里是我的问题:

WHERE is the correct/best location to install the downloaded JDK? (most people prefer something like usr/lib/jvm. why?

安装下载的 JDK 的正确/最佳位置在哪里?(大多数人更喜欢类似的东西usr/lib/jvm。为什么?

when i have installed the jvm (i.e. copied the content of the jdk<version>.tar.gzfile to the chosen location, i have to setup my system to refer to the jdk.

当我安装了 jvm(jdk<version>.tar.gz即将文件的内容复制到所选位置时,我必须设置我的系统以引用 jdk。

here I can use for example: sudo update-alternatives --install "/usr/bin/java" "java" \ "/usr/lib/jvm/jdk<version>/bin/java" 1. i have to do the same with javacright?

在这里我可以使用例如:sudo update-alternatives --install "/usr/bin/java" "java" \ "/usr/lib/jvm/jdk<version>/bin/java" 1。我必须做同样的事情javac吗?

finally i have to set the JAVA_HOME variable by adding the lines:

最后,我必须通过添加以下行来设置 JAVA_HOME 变量:

JAVA_HOME=/usr/lib/jvm/jdk<version>/
export JAVA_HOME

to the .bashrcfile.

.bashrc文件。

But the problem: This way i always have to update my alternatives whenever i upgrade my jdk right? How can i solve this? Can anybody give a clear description how to install the jdk on linux systems in a clever and correct way?

但问题是:这样,每当我升级 jdk 时,我总是必须更新我的替代品,对吗?我该如何解决这个问题?任何人都可以清楚地描述如何以聪明和正确的方式在 linux 系统上安装 jdk 吗?

thank you

谢谢你

采纳答案by Linn Htoo

You can install and stay up to date with the latest Oracle Java 7 and all you have to do is manually add the PPA repository to the Software Sources.

您可以安装最新的 Oracle Java 7 并保持最新状态,您所要做的就是将 PPA 存储库手动添加到软件源中。

From http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html:

http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html

su -
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a     /etc/apt/sources.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886
apt-get update
apt-get install oracle-java7-installer
exit

For Ubuntu, http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html. With this, I've managed to installed JDK on my ubuntu server.

对于 Ubuntu,http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html。有了这个,我已经设法在我的 ubuntu 服务器上安装了 JDK。

回答by Moonlit

Step 1: Open Applicaction>> Accessories>> Terminal

第一步:打开应用>>附件>>终端

Step 2: Type commandline as below...

第 2 步:键入命令行如下...

  sudo apt-get install openjdk-6-jdk

Step 3: Type commandline as below...

第 3 步:键入命令行如下...

  apt-cache search jdk

(Note: openjdk-6-jdk is symbolically used here you can choose jdk version as per your requirment.)

(注意:openjdk-6-jdk 在这里象征性地使用,您可以根据需要选择 jdk 版本。)

How to set "Environment Variables" for "Open jdk" in Ubuntu(Linux)?

如何在 Ubuntu(Linux) 中为“Open jdk”设置“环境变量”?

Step 4: For "JAVA_HOME" (Environment Variable) type command as shown below, in "Terminal" using your installation path...

第 4 步:对于“JAVA_HOME”(环境变量)类型命令,如下所示,在“终端”中使用您的安装路径...

  export JAVA_HOME=/usr/lib/jvm/java-6-openjdk

(Note: "/usr/lib/jvm/java-6-openjdk" is symbolically used here just for demostration you should use your path as per your installation.)

(注意:“/usr/lib/jvm/java-6-openjdk”在这里象征性地使用只是为了演示,你应该根据你的安装使用你的路径。)

Step 5: For "PATH" (Environment Variable) type command as shown below, in "Terminal" using your installation path...

第 5 步:对于“PATH”(环境变量)类型命令,如下所示,在“终端”中使用您的安装路径...

  export PATH=/usr/lib/jvm/java-6-openjdk/bin

(Note: "/usr/lib/jvm/java-6-openjdk" is symbolically used here just for demostration you should use your path as per your installation.)

(注意:“/usr/lib/jvm/java-6-openjdk”在这里象征性地使用只是为了演示,你应该根据你的安装使用你的路径。)

Step 6: Check for "open jdk" installation, just type command in "Terminal" as shown below java

第 6 步:检查“open jdk”安装,只需在“终端”中键入命令,如下所示 java

回答by Axel

Just make a symlink /usr/lib/jvm/jdkthat points to /usr/lib/jvm/jdk<version>. Then all you have to do after updating the JDK is update the symlink to point to the new location.

只需创建一个/usr/lib/jvm/jdk指向/usr/lib/jvm/jdk<version>. 然后在更新 JDK 后您要做的就是更新符号链接以指向新位置。

回答by The Dead Guy

In 2020 here is how we roll!

2020 年,这就是我们的滚动方式!

NB: This will also work for Command Line on Linux Systems.

注意:这也适用于 Linux 系统上的命令行。

Kudos to this dude here

荣誉给这家伙在这里

Step 1:Download the latest JDK(jdk-8u231-linux-x64.tar.gz) from this official site. using this

第一步:从官网下载最新的JDK(jdk-8u231-linux-x64.tar.gz)。使用这个

 wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u231-linux-x64.tar.gz

because we are installing remotely if not just download from here

因为我们正在远程安装,如果不只是从这里下载

Step 2:Open the terminal (Ctrl + Alt + T) and enter the following command.

第 2 步:打开终端(Ctrl + Alt + T)并输入以下命令。

sudo mkdir /usr/lib/jvm

Step 3:Enter the following command to change the directory.

第三步:输入以下命令更改目录。

cd /usr/lib/jvm

Step 4:Extract the jdk-8u231-linux-x64.tar.gz file in that directory using this command.

第 4 步:使用此命令解压缩该目录中的 jdk-8u231-linux-x64.tar.gz 文件。

sudo tar -xvzf ~/Downloads/jdk-8u231-linux-x64.tar.gz

According to this command, the JDK filename is jdk-8u231-linux-x64.tar.gz and which is located in the ~/Downloads folder. If your downloaded file is in any other location, change the command according to your path.

根据此命令,JDK 文件名为 jdk-8u231-linux-x64.tar.gz,位于 ~/Downloads 文件夹中。如果您下载的文件位于任何其他位置,请根据您的路径更改命令。

Step 5:Enter the following command to open the environment variables file.

第五步:输入以下命令打开环境变量文件。

sudo nano /etc/environment

In the opened file, add the following bin folders to the existing PATH variable. (in my case the file was empty so i just add the content and saved).

在打开的文件中,将以下 bin 文件夹添加到现有的 PATH 变量中。(在我的情况下,文件是空的,所以我只添加内容并保存)。

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_231/bin:/usr/lib/jvm/jdk1.8.0_231/db/bin:/usr/lib/jvm/jdk1.8.0_231/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_231"
J2REDIR="/usr/lib/jvm/jdk1.8.0_231/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_231"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_231/db"

This is how your file will look after modifying...

这是您的文件在修改后的外观...

Save the changes and close the gedit.

保存更改并关闭 gedit。

Step 7:Enter the following commands to inform the system about the Java's location. Depending on your JDK version, the paths can be different.

步骤 7:输入以下命令以通知系统 Java 的位置。根据您的 JDK 版本,路径可能不同。

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_231/bin/java" 0

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_231/bin/javac" 0

sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_231/bin/java

sudo update-alternatives --set javac /usr/lib/jvm/jdk1.8.0_231/bin/javac

Step 8:To verify the setup enter the following commands and make sure that they print the location of java and javac as you have provided in the previous step.

第 8 步:要验证设置,请输入以下命令并确保它们打印出您在上一步中提供的 java 和 javac 的位置。

update-alternatives --list java

update-alternatives --list javac

Step 9:Restart the computer (or just log-out and login) and open the terminal again. (I dint have to do this because step 10 already came through)

步骤9:重新启动计算机(或只是注销并登录)并再次打开终端。(我必须这样做,因为第 10 步已经通过)

Step 10:Enter the following command.

步骤 10:输入以下命令。

java -version

Step 11:Enjoy!!!

第 11 步:享受!!!