Java 如何判断是否安装了 JRE 或 JDK
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22539779/
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
How to tell if JRE or JDK is installed
提问by PopKernel
I have one computer that I intentionally installed JDK on. I have another computer with JRE, for, among other things, testing. However, when I got a java application working on this computer, and then tried it on another, it complained that JDK was required. How can I check if JDK was somehow installed on my system? Note: the computer in question is a Mac.
我有一台我特意安装了 JDK 的计算机。我有另一台带有 JRE 的计算机,用于测试等。但是,当我在这台计算机上运行一个 java 应用程序,然后在另一台计算机上尝试它时,它抱怨需要 JDK。如何检查 JDK 是否以某种方式安装在我的系统上?注意:有问题的计算机是 Mac。
采纳答案by Maciej Cygan
You can open up terminal and simply type
您可以打开终端并简单地输入
java -version // this will check your jre version
javac -version // this will check your java compiler version if you installed
this should show you the version of java installed on the system (assuming that you have set the path of the java in system environment).
这应该会显示系统上安装的 java 版本(假设您已经在系统环境中设置了 java 的路径)。
And if you haven't, add it via
如果你还没有,通过添加它
export JAVA_HOME=/path/to/java/jdk1.x
and if you unsure if you have java at all on your system just use find
in terminal
如果您不确定您的系统上是否有 Java,请find
在终端中使用
i.e. find / -name "java"
IE find / -name "java"
回答by Shiraaz.M
Normally a jdk installation has javac in the environment path variables ... so if you check for javac in the path, that's pretty much a good indicator that you have a jdk installed.
通常,jdk 安装在环境路径变量中有 javac ......因此,如果您在路径中检查 javac,这几乎是您安装了 jdk 的一个很好的指标。
回答by Yu Liang
according to JAVA documentation, the JDK should be installed in this path:
根据JAVA文档,JDK应该安装在这个路径:
/Library/Java/JavaVirtualMachines/jdkmajor.minor.macro[_update].jdk
See the uninstall JDK part at https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html
请参阅https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html 上的卸载 JDK 部分
So if you can find such folder then the JDK is installed
因此,如果您可以找到这样的文件夹,则安装了 JDK
回答by Shayan Amani
@maciej-cygan described the process well, however in order to find your java path:
@maciej-cygan 很好地描述了这个过程,但是为了找到你的 java 路径:
$ which java
$ which java
it gives you the path of java
binary file which is a linked file in /usr/bin
directory. next:
它为您提供java
二进制文件的路径,该文件是/usr/bin
目录中的链接文件。下一个:
$ cd /usr/bin/ && ls -la | grep java
$ cd /usr/bin/ && ls -la | grep java
find the pointed location which is something as follows (for me):
找到如下所示的指向位置(对我而言):
then
cd
to the pointed directory to find the real home directory for Java. next:
$ ls -la | grep java
$ ls -la | grep java
which is as follows in this case:
在这种情况下如下:
so as it's obvious in the screenshot, my Java home directory is /usr/lib/jvm/java-11-openjdk-amd64
. So accordingly I need to add JAVA_HOME to my bash profile (.bashrc
, .bash_profile
, etc. depending on your OS) like below:
因此,在屏幕截图中很明显,我的 Java 主目录是/usr/lib/jvm/java-11-openjdk-amd64
. 所以因此我需要JAVA_HOME添加到我的bash的个人资料(.bashrc
,.bash_profile
,等视操作系统而定),如下图所示:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Here you go!
干得好!
回答by tresf
A generic, pure Java solution..
一个通用的纯 Java 解决方案..
For Windows and MacOS, the following can be inferred (most of the time)...
对于 Windows 和 MacOS,可以推断出以下内容(大部分时间)...
public static boolean isJDK() {
String path = System.getProperty("sun.boot.library.path");
if(path != null && path.contains("jdk")) {
return true;
}
return false;
}
However... on Linux this isn't as reliable... For example...
但是......在Linux上这并不可靠......例如......
- Many JREs on Linux contain
openjdk
the path - There's no guarantee that the JRE doesn't also contain a JDK.
- Linux 上的许多 JRE 都包含
openjdk
路径 - 不能保证 JRE 也不包含 JDK。
So a more fail-safe approach is to check for the existence of the javac
executable.
因此,更安全的方法是检查javac
可执行文件是否存在。
public static boolean isJDK() {
String path = System.getProperty("sun.boot.library.path");
if(path != null) {
String javacPath = "";
if(path.endsWith(File.separator + "bin")) {
javacPath = path;
} else {
int libIndex = path.lastIndexOf(File.separator + "lib");
if(libIndex > 0) {
javacPath = path.substring(0, libIndex) + File.separator + "bin";
}
}
if(!javacPath.isEmpty()) {
return new File(javacPath, "javac").exists() || new File(javacPath, "javac.exe").exists();
}
}
return false;
}
Warning:This will still fail for JRE + JDK combos which report the JRE's sun.boot.library.path
identically between the JRE and the JDK. For example, Fedora's JDK will fail (or pass depending on how you look at it) when the above code is run. See unit tests below for more info...
警告:对于sun.boot.library.path
在 JRE 和 JDK 之间报告 JRE相同的JRE + JDK 组合,这仍然会失败。例如,当上面的代码运行时,Fedora 的 JDK 将失败(或通过,取决于你如何看待它)。有关更多信息,请参阅下面的单元测试...
Unit tests:
单元测试:
# Unix
java -XshowSettings:properties -version 2>&1|grep "sun.boot.library.path"
# Windows
java -XshowSettings:properties -version 2>&1|find "sun.boot.library.path"
# PASS: MacOS AdoptOpenJDK JDK11
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/lib
# PASS: Windows Oracle JDK12
c:\Program Files\Java\jdk-12.0.2\bin
# PASS: Windows Oracle JRE8
C:\Program Files\Java\jre1.8.0_181\bin
# PASS: Windows Oracle JDK8
C:\Program Files\Java\jdk1.8.0_181\bin
# PASS: Ubuntu AdoptOpenJDK JDK11
/usr/lib/jvm/adoptopenjdk-11-hotspot-amd64/lib
# PASS: Ubuntu Oracle JDK11
/usr/lib/jvm/java-11-oracle/lib
# PASS: Fedora OpenJDK JDK8
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-1.b16.fc24.x86_64/jre/lib/amd64
#### FAIL: Fedora OpenJDK JDK8
/usr/java/jdk1.8.0_231-amd64/jre/lib/amd64
回答by tresf
the computer in question is a Mac.
有问题的计算机是 Mac。
A macOS-only solution:
仅适用于 macOS 的解决方案:
/usr/libexec/java_home -v 1.8+ --exec javac -version
Where 1.8+
is Java 1.8 or higher.
1.8+
Java 1.8 或更高版本在哪里。
Unfortunately, the java_home
helper does not set the proper return code, so checking for failure requires parsing the output (e.g. 2>&1 |grep -v "Unable"
) which varies based on locale.
不幸的是,java_home
帮助程序没有设置正确的返回码,因此检查失败需要解析2>&1 |grep -v "Unable"
根据区域设置而变化的输出(例如)。
Note, Java may also exist in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin
, but at time of writing this, I'm unaware of a JRE that installs there which contains javac
as well.
注意,Java也可能存在/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin
,但在写这篇的时候,我不知道安装有其中包含JRE的javac
为好。