How to tell if JRE or JDK is installed
Solution 1
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
i.e. find / -name "java"
Solution 2
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.
Solution 3
@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!
Solution 4
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...
- Many JREs on Linux contain
openjdk
the path - There's no guarantee that the JRE doesn't also contain a JDK.
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
Related videos on Youtube
PopKernel
I'm an amateur programmer! Not much else to say at the moment…
Updated on July 08, 2022Comments
-
PopKernel almost 2 years
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.
-
Kevin Workman about 10 yearsCan you post the full text of the error, including how you're trying to run it? You don't need the JDK to run a Java program, just the JRE.
-
PopKernel about 10 yearsIt was a friends computer- something about installing command line tools. I'll comment again when I get a chance to inspect it again.
-
Kevin Workman about 10 yearsIt sounds like you might be talking about making sure that Java is on your path, but that's just a guess. Either way, you only need the JRE to run Java programs. You need the JDK to compile them.
-
Arne Burmeister about 10 yearsOnly few java programs need a JDK, IDEs, servlet containers like tomcat to compile JSPs and some others. What kind of application has the problem?
-
-
Pacerier over 9 years@MaciejCygan, Why is my JAVA_HOME pointing to a JRE instead of a JDK?
-
Maciej Cygan over 9 years@Pacerier Why ?? i do not know that for sure lol!. Maybe you do not have JDK installed ? Running windows or linux ??
-
Pacerier over 9 years@MaciejCygan, I'm using windows. Yea, I've got a JDK installed too.
-
Maciej Cygan over 9 years@Pacerier You can edit / or make a entry if none found - inside your environmental variables.
-
Pacerier over 9 years@MaciejCygan, Yea, but why is my JAVA_HOME pointing to a JRE instead of a JDK?
-
Maciej Cygan over 9 years@Pacerier probably because you have installed JDK before JRE ? could really be anything. The bottom line is to get the right path after you have installed both.
-
kmote about 8 yearsI realize that the OP was asking about a Mac, so it may be different there. But as for Windows, this test does not seem reliable. I just checked, and I do NOT have
javac
in my path, but I DO have a jdk installed. (In my case, my JAVA_HOME is pointed toC:\Program Files\Java\jdk1.8.0_45
) -
ahsant almost 8 yearsDoes this answer related to windows? if yes, "java -version" is returing with an error saying it is not a recognized command.
-
Maciej Cygan almost 8 years@ahsant if it says so then your environmental variable is not set correctly. Otherwise it would print java info :)
-
Hamed almost 7 yearsYeah, you are right cause javac is actually located in %JAVA_HOME%\bin
-
user3054986 over 6 yearsbtw, find is not necessary in this case. which BINARY_TO__EXECUTE will find your binary file; so, in this case which java will work
-
tolache over 2 yearsHow do I tell from this output if my installation is JRE or JDK?