如果在 Ubuntu 上安装了多个 Java 版本,如何检查 Oracle Java 版本

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

How to check Oracle Java version if multiple versions of Java installed on Ubuntu

javabashubuntu

提问by user1558064

I have both OpenJDKand Oracle Javainstalled on my Ubuntu. If the activated java is OpenJDK, is there a way to check the version of Oracle java in bash shell?

我的 Ubuntu 上同时安装了OpenJDKOracle Java。如果激活的java是OpenJDK,有没有办法在bash shell中查看Oracle java的版本?

采纳答案by Michael Petch

update-java-alternatives -lwill list all the java versions installed via the alternatives system.

update-java-alternatives -l将列出通过替代系统安装的所有 Java 版本。

For instance on one of my systems it will display the version and the path:

例如,在我的其中一个系统上,它将显示版本和路径:

java-1.6.0-openjdk-amd64 1061 /usr/lib/jvm/java-1.6.0-openjdk-amd64
java-7-oracle 1069 /usr/lib/jvm/java-7-oracle

If you want the oracle one then I guess you could do:

如果你想要预言机,那么我想你可以这样做:

update-java-alternatives -l | grep oracle | awk '{ print  }'

This would alternatively find all oracle versions and issue the -versioncommand against each one in the list:

或者,这将查找所有 oracle 版本并-version针对列表中的每个版本发出命令:

update-java-alternatives -l | grep oracle | awk '{system("/bin/java -version")}'

Output may look something like this:

输出可能如下所示:

java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

One step further would be to parse out the java version from the -versioncommand and simply display it:

更进一步的方法是从-version命令中解析出 java 版本并简单地显示它:

(update-java-alternatives -l | grep oracle | awk '{system("""/bin/java -version 2>&1 | grep \"java version\"")}') | awk -F\" '{print }'

The 2>&1is needed because Java will display version to standard error. The output would simply look like this (and could be easily assigned to a bash variable if you needed it that way):

2>&1是必需的,因为 Java 会将版本显示为标准错误。输出看起来就像这样(如果你需要它,可以很容易地将它分配给一个 bash 变量):

1.7.0_67

If you had multiple oracle instances this would display the version for each one. If you wanted to find all the versions for every Java you could simply remove the | grep oracle

如果您有多个 oracle 实例,这将显示每个实例的版本。如果你想找到每个 Java 的所有版本,你可以简单地删除| grep oracle

回答by qbit

Go to the installation folder of the oracle Java, and then open the binfolder. Now open a terminal "here" and type java -version

进入oracle Java的安装文件夹,然后打开该bin文件夹。现在在“这里”打开一个终端并输入java -version

alternativelly,

paste the entire path to that bin folder in the command line followed by java -versioni.e. /path/to/bin/java -version

或者,

将整个路径粘贴到命令行中该 bin 文件夹,然后是java -versionie/path/to/bin/java -version

回答by Cesar Villasana

If you are using the openjdk java, then the oracle Java in your PC is just a folder. There is no command to check the version of that if you are not using it.

如果您使用的是openjdk java,那么您PC中的oracle Java只是一个文件夹。如果您不使用它,则没有命令可以检查它的版本。

If you once used oracle java then it must be in your java alternatives and you can find the folder (usually with version in name) with:

如果您曾经使用过 oracle java,那么它必须在您的 java 替代品中,您可以使用以下命令找到文件夹(通常带有版本名称):

update-alternatives --config java

If it says something like /usr/lib/jvm/jdk1.8.0_05/bin/java, then you have 1.8 in your pc.

如果它说/usr/lib/jvm/jdk1.8.0_05/bin/java 之类的东西,那么你的电脑中有 1.8。

If you haven't used oracle java anytime you simply have to check what you have downloaded.

如果您从未使用过 oracle java,只需检查您下载的内容即可。

If you want to simply see the version you are using:

如果您只想查看正在使用的版本

java -version

Hope it helps

希望能帮助到你

回答by Kent

yes, you need to know the install path of the oracle one. then /path/to/there/bin/java -version. Assume that the openjdkone is your default java.

是的,您需要知道 oracle 的安装路径。然后/path/to/there/bin/java -version。假设openjdk一个是你的默认 java.lang.

回答by Saranga kapilarathna

you should follow bellow two commands

你应该遵循以下两个命令

  1. sudo update-alternatives --config java
  1. 须藤更新替代品--config java

In their show the all already installed Java versions like belowenter image description here

在他们的节目中,所有已经安装的 Java 版本如下所示在此处输入图片说明

2.gedit ~/.bashrc

2.gedit ~/.bashrc

`export JAVA_HOME=/path/to/jdk/folder

`导出JAVA_HOME=/path/to/jdk/folder

usually /usr/java/jdk or /usr/lib/java/jdk

通常 /usr/java/jdk 或 /usr/lib/java/jdk

export PATH=$PATH:$JAVA_HOME/bin`

出口路径=$PATH:$JAVA_HOME/bin`

add to bash

添加到 bash