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.
-
1Can 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.– Kevin WorkmanMar 20, 2014 at 16:54
-
It was a friends computer- something about installing command line tools. I'll comment again when I get a chance to inspect it again.– PopKernelMar 20, 2014 at 17:40
-
It 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.– Kevin WorkmanMar 20, 2014 at 17:41
-
Only few java programs need a JDK, IDEs, servlet containers like tomcat to compile JSPs and some others. What kind of application has the problem?– Arne BurmeisterApr 24, 2014 at 11:35
6 Answers
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"
(edit: command -v java
will look up java on your PATH, will be much faster than find
when java is installed and correctly placed on PATH).
-
2@MaciejCygan, Why is my JAVA_HOME pointing to a JRE instead of a JDK?– PacerierDec 4, 2014 at 8:53
-
@Pacerier Why ?? i do not know that for sure lol!. Maybe you do not have JDK installed ? Running windows or linux ?? Dec 10, 2014 at 21:04
-
-
@Pacerier You can edit / or make a entry if none found - inside your environmental variables. Dec 11, 2014 at 13:19
-
@MaciejCygan, Yea, but why is my JAVA_HOME pointing to a JRE instead of a JDK?– PacerierDec 12, 2014 at 2:35
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.
-
3I 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
)– kmoteFeb 22, 2016 at 16:18 -
Yeah, you are right cause javac is actually located in %JAVA_HOME%\bin– HamedJul 14, 2017 at 22:07
@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!
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
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
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.