Monday, January 14, 2013

Determining JDK Installation

I recently read in a book on a Java-based technology that one can determine if the JDK is installed by running java -version and looking for the the Strings "Client" or "Server" in the output of the version command. The authors of that book state that the presence of "Server VM" means the JDK is installed and the presence of "Client VM" means that the JDK is not installed (presumably implying that the JRE only is present because something is returned). Unfortunately, that is not a sufficient way to determine presence of the JDK. In this post, I look at how one can determine that the JDK is installed and on the path.

Before discussing approaches to determining if the JDK is installed, I first want to show why running java -version and looking for "Server" or "Client" to differentiate presence from absence of JDK is insufficient. The following screen snapshot (with yellow highlighting circles added to it) shows running java -version by itself, running java -client -version, and running java -server -version. The important take-away from this screen snapshot is that the it is the server versus client setting of the running JVM that truly determines whether the String "Server" or "Client" is present in the returned Java version information.

This screen snapshot indicates that the default VM is "Client" for this machine. This is not surprising given that the Server-Class Machine Detection page states that the "Default VM" for operating system "Microsoft Windows" on hardware architecture "i586" is "Default client VM." For most other "Sun-supported" architecture/OS combinations, the default tends to be "Server." On 64-bit architectures, the -client option is ignored and it is always the Server VM that is used, though the Java launcher documentation states that this last observation "is subject to change in a future release."

Running java -version does help determine if the Java application launcher is on the user's path. If it is not on the path, an error message indicating that "java" is a command not found would be expected. One can determine if some version of the JDK is on the path using javac -version. Because the Java compiler only comes with the JDK rather than the JRE, this is a simple test to make sure some version of the JDK is on your system and on your path. The -help flag could also be passed to javac to determine the presence of the JDK, but -version has the added benefit of telling the operator which version of the JDK is on the system and on the path. This is shown in the next screen snapshot.

If javac is not already on your path, running javac -version will return an error message stating that the command is not found. This could only mean that it's not on your path, but is still on your system. You can look for it by searching for the javac command using an operating system search tool (such as in Linux with whereis or find or in Windows with its directory/file search tools or via Control Panel's Uninstall/Change Programs tool). If javac -version is found and returns a version, you can determine which that is on Linux with whereis javac and on Windows with a command like grokster's recommendation:

for %i in (javac.exe) do @echo.   %~$PATH:i

The next screen snapshot shows the Windows Control Panel approach for determining which versions of the JRE and JDK are on the Windows machine.

The "Java 7 Update 11" shown is the Java 7 JRE and does not include the JDK. The "Java SE Development Kit 7 Update 6" is a JDK as tipped off by the "Development Kit" portion.

For Windows machines, Java (whether JDK or JRE) might be installed in Program Files/Java and one can examine that directory to see which versions of Java are installed. This is shown in the next screen snapshot.

In the above screen snapshot, the three folders with "jdk" in their names are JDK installations and the folder with "jre" in its name is JRE only (no JDK).

Conclusion

I can understand why the authors of the book I was reading wanted to use presence of "Server" in the VM version information to indicate JDK and "Client" in the VM version to indicate JRE; this would be, if it worked, a much easier way for a reader relatively new to Java to make that determination. Unfortunately, it is not that easy as proven by the StackOverflow thread How to find where is JDK installed on my windows machine? and How do I know if I have Sun Java Development Kit installed? There are quite a few interesting ideas posted in both of those resources.

I didn't even look in this post at the complexity on Windows of multiple JRE deployments because my focus in this post was on finding installed JDKs. By the way, more information on the difference between the client VM and the server VM can be found in Real differences between "java -server" and "java -client"?, Java HotSpot VM Options, and JVM Server vs Client Mode.