Java check latest version programmatically

前端 未结 9 1629
不思量自难忘°
不思量自难忘° 2020-12-14 02:36

Goal: check java\'s version on a machine (I can get this from java -version). Compare it with latest available from java website

I woul

相关标签:
9条回答
  • 2020-12-14 03:00

    To get all system variables

    Properties properties = System.getProperties();
    System.out.println(properties);
    

    Sample output, this might be different in your system depending on your OS and Java JDK/JRE version.

    {
        java.runtime.name = Java(TM) SE Runtime Environment,
        sun.boot.library.path = C:\Program Files\Java\jdk1.8.0_31\jre\bin,
        java.vm.version = 25.31-b07,
        java.vm.vendor = Oracle Corporation,
        java.vendor.url = http://java.oracle.com/,
        path.separator = ;,
        idea.launcher.port = 7534,
        java.vm.name = Java HotSpot(TM) 64-Bit Server VM,
        file.encoding.pkg = sun.io,
        user.country = NP,
        user.script = ,
        sun.java.launcher = SUN_STANDARD,
        sun.os.patch.level = ,
        java.vm.specification.name = Java Virtual Machine Specification,
        user.dir = C:\Users\...\roid,
        java.runtime.version = 1.8.0_31-b13,
        java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment,
        java.endorsed.dirs = C:\Program Files\Java\jdk1.8.0_31\jre\lib\endorsed,
        os.arch = amd64,
        java.io.tmpdir = C:\Windows\TEMP\,
        line.separator = ,
        java.vm.specification.vendor = Oracle Corporation,
        user.variant = ,
        os.name = Windows 8.1,
        sun.jnu.encoding = Cp1252,
        java.library.path = C:\Program...roid,
        java.specification.name = Java Platform API Specification,
        java.class.version = 52.0,
        sun.management.compiler = HotSpot 64-Bit Tiered Compilers,
        os.version = 6.3,
        user.home = C:\Users\Xxx,
        user.timezone = Asia/Kathmandu,
        java.awt.printerjob = sun.awt.windows.WPrinterJob,
        file.encoding = UTF-8,
        idea.launcher.bin.path = C:\Program Files (x86)\xxx\bin,
        java.specification.version = 1.8,
        java.class.path = C:\Program Files\Java\jdk1.8.0_31\jre\lib\charsets.jar;...,
        user.name = Xxx,
        java.vm.specification.version = 1.8,
        sun.java.command = com.xxxx.ameras,
        java.home = C:\Program Files\Java\jdk1.8.0_31\jre,
        sun.arch.data.model = 64,
        user.language = en,
        java.specification.vendor = Oracle Corporation,
        awt.toolkit = sun.awt.windows.WToolkit,
        java.vm.info = mixed mode,
        java.version = 1.8.0_31,
        java.ext.dirs = C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext;...,
        java.vendor = Oracle Corporation,
        file.separator = \,
        java.vendor.url.bug = http://bugreport.sun.com/bugreport/,
        sun.io.unicode.encoding = UnicodeLittle,
        sun.cpu.endian = little,
        sun.desktop = windows,
        sun.cpu.isalist = amd64
    }
    

    Retrive only specific variable

    String javaVersion = System.getProperty("java.version");
    System.out.println(javaVersion);
    

    Output

    1.8.0_31
    
    0 讨论(0)
  • 2020-12-14 03:01

    @MarcelStör's solution no longer works - the version in the file is 1.8.0_51, while the actual latest version is 1.8.0_91/92. If you go to the Java test page in Firefox or Chrome and open the development console you can get the variable latest8Version which currently is 1.8.0_91. This could be wrapped in a Selenium/Firefox solution, but is an incredibly hacky way of getting this information.

    0 讨论(0)
  • 2020-12-14 03:15

    An URL very similar to the now defunct "JreCurrentVersion2.txt":

    http://javadl-esd-secure.oracle.com/update/baseline.version

    The contents of the link look like this:

    1.8.0_111
    1.7.0_121
    1.6.0_131
    1.5.0_99
    1.4.2_43
    

    You can easily parse the contents to find the latest JRE versions.

    0 讨论(0)
  • 2020-12-14 03:15
    System.getProperty("java.vm.specification.version");
    System.getProperty("java.version");
    
    0 讨论(0)
  • 2020-12-14 03:19

    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 https://java.com/en/download/installed8.jsp | grep latest8Version.

    Update 2018-08-19

    http://javadl-esd-secure.oracle.com/update/baseline.version is another hot spot as mentioned in some other answer.

    0 讨论(0)
  • 2020-12-14 03:20

    I don't know what information you are exactly looking for, but you can get some version information using

    System.getProperty("java.version");
    

    If this is not what you're looking for, check the other available properties here: http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#getProperties()

    As for the latest available version, I guess you'd have to parse this site manually: http://java.com/en/download/index.jsp

    The latest version is on there, currently it's

    Version 7 Update 9

    You write that this is not what you want because "Oracle can change their website and styles". However, you want to find out the latest version of Java by accessing their service (website in this case). As long as you're not paying for this, they have no obligation to you, and can change the service whenever they want without your consent. And even when you're a paying customer, the best you can hope for is that they will inform you of upcoming changes, and your maintenance issues will remain.

    Remember, it's THEIR service you want to use.

    0 讨论(0)
提交回复
热议问题