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.
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).
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
ie find / -name "java"
通常,jdk 安装在环境路径变量中有 javac ......因此,如果您在路径中检查 javac,这几乎是您安装了 jdk 的一个很好的指标。
@maciej-cygan described the process well, however in order to find your java path:
$ which java
it gives you the path of java
binary file which is a linked file in /usr/bin
directory. next:
$ 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
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_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Here you go!
according to JAVA documentation, the JDK should be installed in this path:
/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
So if you can find such folder then the JDK is installed
A generic, pure Java solution..
For Windows and MacOS, the following can be inferred (most of the time)...
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...
openjdk
the path So a more fail-safe approach is to check for the existence of the javac
executable.
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...
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
the computer in question is a Mac.
A macOS-only solution:
/usr/libexec/java_home -v 1.8+ --exec javac -version
Where 1.8+
is Java 1.8 or higher.
Unfortunately, the java_home
helper does not set the proper return code, so checking for failure requires parsing the output (eg 2>&1 |grep -v "Unable"
) which varies based on locale.
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.
On macos terminal you can type:
java -version
(for jre)
javac -version
(for the compiler version)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.