Translated

This commit is contained in:
2021-11-04 19:06:48 +08:00 committed by GitHub
parent aaff325d10
commit 93841bd91e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 157 additions and 157 deletions

View File

@ -1,157 +0,0 @@
[#]: subject: "How to Set JAVA_HOME Variable in Ubuntu Linux Correctly"
[#]: via: "https://itsfoss.com/set-java-home-ubuntu/"
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
[#]: collector: "lujun9972"
[#]: translator: "robsean"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to Set JAVA_HOME Variable in Ubuntu Linux Correctly
======
If you are [running Java programs on Ubuntu][1] using Eclipse, [Maven][2] or Netbeans etc, youll need to set JAVA_HOME to your path. Otherwise, your system will complain that “java_home environment variable is not set”.
In this beginners tutorial, Ill show the steps to correctly set Java Home variable on Ubuntu. The steps should be valid for most other Linux distributions as well.
The process consists of these steps:
* Making sure Java Development Kit (JDK) is installed.
* Finding the correct location of JDK executable.
* Setting the JAVA_HOME variable and making the change permanent.
### Step 1: Check if JDK is installed
The simplest way to check if Java Development Kit (JDK) is installed on your Linux system is by running this command:
```
javac --version
```
The above command checks the version of Java compiler. If it is installed, it will show the Java version.
![Java Compiler is installed][3]
If the command shows an error like javac command not found, youll have to install JDK.
![Java Compiler is not installed][4]
If Java Compiler is not installed on your system, install Java Development Kit using this command:
```
sudo apt install default-jdk
```
This will install the default Java version in your current Ubuntu version. If you need some other specific Java version, youll have to specify it while [installing Java on Ubuntu][5].
Once you have made sure that Java Compiler is present on your system, its time to find its location.
### Step 2: Get the location of JDK executable (Java Compiler)
The executable is usually located in the /usr/lib/jvm directory. I wont left you on your own for a guessing game. Instead, lets find out the path of the Java executable.
[Use the which command][6] to get the location of Java Compiler executable:
```
which javac
```
The problem here is that the location it gives is actually a [symbolic link][7]. Youll have to follow it a couple of times:
![][8]
An easier method is to follow the symbolic link and get to the actual executable file directly using this command:
```
readlink -f `which java`
```
The readlink command follows a symbolic link. I have used ` around _which java_. This is called command substitution and it replaces the command with its output. So basically, the above command is equivalent to _readlink -f /usr/bin/java_ in this case.
In my example, the location of the executable file is **/usr/lib/jvm/java-11-openjdk-amd64/bin/java**. It could be different for you. Copy the correct path you got from the above command in your system. You know, you can [copy paste in the Ubuntu terminal][9].
### Step 3: Setting JAVA_HOME variable
Now that you have got the location, use it to set the JAVA_HOME environment variable:
```
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java
```
Check the value of JAVA_HOME directory:
```
echo $JAVA_HOME
```
![][10]
Try to run your program or project in the SAME TERMINAL and see if it works.
This is not over yet. The JAVA_HOME variable you just declared is temporary. If you close the terminal or start a new session, it will be empty again.
To set JAVA_HOME variable permanently, you should add it to the bashrc file in your home directory.
You can [use the Nano editor for editing files in the Linux terminal][11]. If you do not want that and take a simple copy-paste approach, use the following commands:
Back up your bashrc file (in case you mess it, you can get it back):
```
cp ~/.bashrc ~/.bashrc.bak
```
Next, [use the echo command to append][12] the export command you used at the beginning of this section. _**Change the command below to use the correct path as displayed by your system in**_.
```
echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java" >> ~/.bashrc
```
Verify that it has been correctly added to the end of the file:
```
tail -3 ~/.bashrc
```
The above [tail command][13] will show the last 3 lines of the specified file.
Heres the entire output of the above three commands.
![][14]
Now, even if you exit the session or restart the system, the JAVA_HOME variable will still be set to the value you specified. Thats what you want, right?
Do note that if you change the default Java version in the future, youll have to change the value of JAVA_HOME and point it to the correct executable path.
I hope this tutorial not only helped you to set Java Home, it also taught you how you are doing it.
If you are still facing issues or have any questions or suggestions, please let me know in the comments.
--------------------------------------------------------------------------------
via: https://itsfoss.com/set-java-home-ubuntu/
作者:[Abhishek Prakash][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/abhishek/
[b]: https://github.com/lujun9972
[1]: https://itsfoss.com/run-java-program-ubuntu/
[2]: https://maven.apache.org/
[3]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/09/check-java-compiler-ubuntu.png?resize=750%2C310&ssl=1
[4]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/09/java-compiler-check-ubuntu.png?resize=732%2C300&ssl=1
[5]: https://itsfoss.com/install-java-ubuntu/
[6]: https://linuxhandbook.com/which-command/
[7]: https://linuxhandbook.com/symbolic-link-linux/
[8]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/09/get-java-home-path-ubuntu.png?resize=800%2C283&ssl=1
[9]: https://itsfoss.com/copy-paste-linux-terminal/
[10]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/09/setting-java-home-ubuntu.png?resize=800%2C268&ssl=1
[11]: https://itsfoss.com/nano-editor-guide/
[12]: https://linuxhandbook.com/echo-command/
[13]: https://linuxhandbook.com/tail-command/
[14]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/09/set-java-home-bashrc-ubuntu.png?resize=786%2C348&ssl=1

View File

@ -0,0 +1,157 @@
[#]: subject: "How to Set JAVA_HOME Variable in Ubuntu Linux Correctly"
[#]: via: "https://itsfoss.com/set-java-home-ubuntu/"
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
[#]: collector: "lujun9972"
[#]: translator: "robsean"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
如何在 Ubuntu Linux 中正确地设置 JAVA_HOME 变量
======
如果你 [在 Ubuntu 上运行 Java 程序][1] ,使用 Eclipse[Maven][2] 或 Netbeans 等等,你将需要设置 JAVA_HOME 到你的路径。否则,你的系统将会向你控诉 “java_home environment variable is not set”。
在这篇初学者教程中,我将向你展示在 Ubuntu 上正确地设置 Java_Home 变量的步骤。这些步骤应该也适用于大多数的其它的 Linux 发行版。
设置过程包含这些步骤:
* 确保已经安装 Java 开发工具包 (JDK) 。
* 查找 JDK 可执行文件的正确的位置。
* 设置 JAVA_HOME 变量和永久的更改。
### 步骤 1: 核查 JDK 是否已经安装
核查 Java 开发工具包 (JDK) 是否已经安装在你的 Linux 系统上的最简单的方法是运行这个命令:
```
javac --version
```
上面的命令将核查 Java 编译器的版本。如果已经安装了 Java 编译器,它将显示 Java 版本。
![Java Compiler is installed][3]
如果上面的命令显示一个像未找到 javac 命令的错误信息,你将必须安装 JDK 。
![Java Compiler is not installed][4]
如果在你的系统上并没有安装 Java 编译器,使用这条命令来安装 Java 开发工具包 JDK
```
sudo apt install default-jdk
```
这将在你当前的 Ubuntu 版本中安装默认的 Java 版本。如果你需要一些其它的特定的 Java 版本,那么你必须 [在 Ubuntu 中安装 Java 时][5],具体指出它的版本。
在你确保 Java 编译器存在于你的系统之中后,接下来就到了查找其位置的时机了。
### 步骤 2: 获取 JDK 可执行文件 (Java 编译器) 的位置
可执行文件通常位于 /usr/lib/jvm 目录之中。我不会让你来玩一个猜谜游戏。相反,让我们找出 Java 可执行文件的路径。
[使用 which 命令][6] 来获取 Java 编译器可执行文件的位置:
```
which javac
```
在这里的问题是,它给出的位置实际上是一个 [符号链接][7] 。你将需要按照下图执行几次:
![][8]
最简单的方法是按照符合链接来直接使用这条命令以获取实际的可执行文件:
```
readlink -f `which java`
```
readlink 命令跟着一个符号链接。我在 _which java_ 的外侧使用 ` 。readlink 将使用 which java 的输出来替换符号链接,这被称之为命令替换。因此,在这个实例中,上面的命令大体上相当于 _readlink -f /usr/bin/java_
在我的示例中,可执行文件的位置是 **/usr/lib/jvm/java-11-openjdk-amd64/bin/java** 。对你来说可能会不一样。在你的系统中,复制上述命令所获取的正确的路径。你知道,你可以 [在 Ubuntu 的终端中复制和粘贴][9] 。
### 步骤 3: 设置 JAVA_HOME 变量
现在,你以及获取了位置,使用它来设置 JAVA_HOME 环境变量:
```
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java
```
核查 JAVA_HOME 目录的值:
```
echo $JAVA_HOME
```
![][10]
尝试在同一个终端总运行你的程序或工程,并查看它是否工作。
这尚未结束。你刚刚声明的 JAVA_HOME 变量是临时的。如果你关闭这个终端或开始一个新的会话,它将会再次变成空的。
为了“永久地”设置 JAVA_HOME 变量,你应该将其添加到你 home 命令中的 bashrc 文件中。
你可以 [在 linux 终端中使用 Nano 编辑器来编译文件][11]。 如果你不想使用它,并想采取一种简单的复制和粘贴的方法,使用下面的命令:
备份你的 bashrc 文件 (万一你把它弄坏了,你还可以将其再恢复回来)
```
cp ~/.bashrc ~/.bashrc.bak
```
接下来,[使用 echo 命令来追加][12] 你在这部分开头处所使用的 export 命令。_**你应该适当地更改下面的命令,以便其正确地使用系统所显示的路径**_.
```
echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java" >> ~/.bashrc
```
验证它已经被正确地添加到文件的结尾处:
```
tail -3 ~/.bashrc
```
上面的 [tail 命令][13] 将显示所具体指定文件的最后 3 行。
这里是上面的三个命令的全部的输出。
![][14]
现在即使你退出会话或重新启动系统JAVA_HOME 变量都仍将设置为你所具体指定的值。这就是你所想要的,对吧?
注意,如果你将来更改默认的 Java 版本,你将需要更改 JAVA_HOME 的值并将其指向正确的可执行文件的路径。
我希望这篇教程不仅会帮助你设置 Java_Home ,也会教会你如何完成这项工作。
如果你仍然面临难题或者有一些疑问或建议,请在评论区告诉我。
--------------------------------------------------------------------------------
via: https://itsfoss.com/set-java-home-ubuntu/
作者:[Abhishek Prakash][a]
选题:[lujun9972][b]
译者:[robsean](https://github.com/robsean)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/abhishek/
[b]: https://github.com/lujun9972
[1]: https://itsfoss.com/run-java-program-ubuntu/
[2]: https://maven.apache.org/
[3]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/09/check-java-compiler-ubuntu.png?resize=750%2C310&ssl=1
[4]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/09/java-compiler-check-ubuntu.png?resize=732%2C300&ssl=1
[5]: https://itsfoss.com/install-java-ubuntu/
[6]: https://linuxhandbook.com/which-command/
[7]: https://linuxhandbook.com/symbolic-link-linux/
[8]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/09/get-java-home-path-ubuntu.png?resize=800%2C283&ssl=1
[9]: https://itsfoss.com/copy-paste-linux-terminal/
[10]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2021/09/setting-java-home-ubuntu.png?resize=800%2C268&ssl=1
[11]: https://itsfoss.com/nano-editor-guide/
[12]: https://linuxhandbook.com/echo-command/
[13]: https://linuxhandbook.com/tail-command/
[14]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2021/09/set-java-home-bashrc-ubuntu.png?resize=786%2C348&ssl=1