Java is a popular programming language and framework in the IT industry. There are a lot of applications developed with Java. Java requires Java Development Kit to develop applications. Compiled java applications can run on systems those are installed Java Runtime Environment a.k.a JRE. We will look in this post on how to set and check JRE or JDK or Java Home Path.
Java是IT行业中流行的编程语言和框架。 有许多用Java开发的应用程序。 Java需要Java Development Kit来开发应用程序。 编译的Java应用程序可以在已安装Java Runtime Environment或JRE的系统上运行。 我们将在这篇文章中介绍如何设置和检查JRE或JDK或Java Home Path。
安装 (Installation)
There are two main different versions of JDK and JRE. The original vendor is Oracle which distributes and sets standards of Java. Second is an open version of JDK and JRE that are supported by Oracle. In this post, we will install the open version named as openjdk-9-jre-headless
.
JDK和JRE有两个主要的不同版本。 最初的供应商是Oracle,它分发和设置Java标准。 其次是Oracle支持的JDK和JRE的开放版本。 在本文中,我们将安装名为openjdk-9-jre-headless
的开放版本。
$ sudo apt-get install openjdk-9-jre-headless -y
![Installation](https://i-blog.csdnimg.cn/blog_migrate/6ac9512933fc238b31bf2f47d9f82bfd.png)
检查Java状态(Check Java Status)
We will look if java path already sets simple running java command which is a compiler for Java applications.
我们将查看java路径是否已经设置了简单运行的java命令,该命令是Java应用程序的编译器。
$ java
![Check Java Status](https://i-blog.csdnimg.cn/blog_migrate/7504a812d0b5a3a4fcf3b6d093c5a786.png)
更新定位数据库(Update Locate Database)
This is auxiliary operation to use locate OpenJDK location we update locate database
这是使用locate OpenJDK位置的辅助操作,我们将更新locate数据库
$ sudo updatedb
查找JDK位置 (Find JDK Location)
We will look at where is JDK located and also get the JDK version. This operation can be used Oracle JDK too. Just the JDK directory name will be different.
我们将查看JDK的位置,并获取JDK版本。 该操作也可以用于Oracle JDK。 只是JDK目录名称会有所不同。
$ locate openjdk
![Find JDK Location](https://i-blog.csdnimg.cn/blog_migrate/a99f7d0ad0cc098b9bac8526c9295443.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0b0fafee5db1b820505065b67236560b.png)
设置Java主页(Set Java Home)
As we see there is a lot of output for JDK search we can see that the path of JDK is /usr/lib/jvm/java-9-openjdk-amd64
because it is located in usr lib. Now we will set PATH for Java
正如我们看到的,JDK搜索有很多输出,我们可以看到JDK的路径是/usr/lib/jvm/java-9-openjdk-amd64
因为它位于usr lib中。 现在我们将为Java设置PATH
$ export JAVA_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
![Set Java Home](https://i-blog.csdnimg.cn/blog_migrate/13813fa4bea84d02aa3f975cf4470be7.png)
使Java Home配置永久存在(Make Java Home Configuration Persistent)
The previous step makes JAVA HOME
available for subshells but if the system is restarted the path will be lost. So we need to make this path definition persistent. There are different methods to make persistent. We will look single and simple one. We will add path definition to the system-wide environment definition configuration. This operation requires root privileges.
上一步使JAVA HOME
可用于子外壳,但是如果系统重新启动,则路径将丢失。 因此,我们需要使该路径定义持久化。 有多种方法可以使持久化。 我们将看起来很简单。 我们将路径定义添加到系统范围的环境定义配置中。 此操作需要root特权。
$ echo "JAVA_HOME='/usr/lib/jvm/java-9-openjdk-amd64'" >> /etc/environment
![Make Java Home Persistent](https://i-blog.csdnimg.cn/blog_migrate/f1cc1c4a4b8276fb8c442c488a63e13c.png)
An alternative method without the need for root privileges is adding Java home path definition to the normal user .bashrc file like below.
不需要root特权的另一种方法是将Java主目录定义添加到普通用户.bashrc文件中,如下所示。
$ echo "JAVA_HOME='/usr/lib/jvm/java-9-openjdk-amd64'" >> .bashrc
![Make Java Home Configuration Persistent](https://i-blog.csdnimg.cn/blog_migrate/2011024d68e2597da0292ee899633bbb.png)
翻译自: https://www.poftut.com/how-to-set-java-home-path-in-linux/