Chris Chris - 3 months ago 19
Java Question

Java check latest version programatically

Goal: check java's version on a machine (I can get this from

java -version
). Compare it with latest available from java website

I would like to know if there is any way I can check for latest Java releases assuming that I have JRE/JDK installed on a machine.

If I can do this through Java itself, my solution would become platform independent. I could use
java.net.URL
class to send a request to Java website and get the HTML, however the response would be dynamic as Oracle can change their website and styles and possibly will have maintenance issues in long run.

I have looked at javatester.org, but I would not want it through an applet but through command line (which I can add to a script).

Through javacpl.exe, I can schedule periodic checks, but I would like to do it on demand.

Thanks in advance.

Answer

The answer is actually quite simple. http://java.com/en/download/testjava.jsp issues a request to http://java.com/applet/JreCurrentVersion2.txt. That file currently contains a single version number: '1.7.0_11'...which is the latest and greatest, indeed.

Java code example

try (BufferedReader br = new BufferedReader(new InputStreamReader(new URL(
    "http://java.com/applet/JreCurrentVersion2.txt").openStream()))) {
  String fullVersion = br.readLine();
  String version = fullVersion.split("_")[0];
  String revision = fullVersion.split("_")[1];
  System.out.println("Version " + version + " revision " + revision);
} catch (IOException e) {
  // handle properly
}

Update 2014-03-20

Eventhough Java 8 was recently released http://java.com/applet/JreCurrentVersion2.txt currently still returns 1.7.0_51.

Update 2016-07-13

Looks like we need to come back to this every few months... Currently you need to scan http://java.com/en/download/installed8.jsp for a JavaScript variable latest8Version. So, you could run curl -s http://java.com/en/download/installed8.jsp | grep latest8Version.