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.
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 pathSo 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 (e.g. 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.
@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!
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.
Source: Stackoverflow.com