Java check latest version programmatically

前端 未结 9 1630
不思量自难忘°
不思量自难忘° 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:21

    UPDATE: I don't recommend this method because this JRE is the one that has the Ask.com toolbar. You're better off downloading it yourself and distributing it yourself.

    The jusched.exe program accesses the following URL to find out what versions are available. I think it's less likely to change because jusched is installed on millions of computers.

    https://javadl-esd-secure.oracle.com/update/1.7.0/map-m-1.7.0.xml

    Here is a snippet of what it returns for me:

    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
    
    <java-update-map version="1.0">
          <mapping>
          <version>1.7.0_17</version>
          <url>https://javadl-esd-secure.oracle.com/update/1.7.0/au-descriptor-1.7.0_25-b17.xml</url>
       </mapping>
       <mapping>
          <version>1.7.0_21</version>
          <url>https://javadl-esd-secure.oracle.com/update/1.7.0/au-descriptor-1.7.0_25-b17.xml</url>
       </mapping>
       </java-update-map>
    

    To get the actual version that it is pointing to you have to fetch the above URL. Here is another snippet of what this XML looks like:

    xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <!-- XML file to be staged anywhere, and pointed to by map.xml file -->
    <java-update>
      <information version="1.0" xml:lang="en">
        <caption>Java Update - Update Available</caption>
        <title>Java Update Available</title>
        <description>Java 7 Update 25 is ready to install. Installing Java 7 Update 25 might uninstall the latest Java 6 from your system. Click the Install button to update Java now.  If you wish to update Java later, click the Later button.</description>
        <moreinfo>http://java.com/moreinfolink</moreinfo>
        <AlertTitle>Java Update Available</AlertTitle>
        <AlertText>A new version of Java is ready to be installed.</AlertText>
        <moreinfotxt>More information...</moreinfotxt>
        <url>http://javadl.sun.com/webapps/download/GetFile/1.7.0_25-b17/windows-i586/jre-7u25-windows-i586-iftw.exe</url>
        <version>1.7.0_25-b17</version>
        <post-status>https://nometrics.java.com</post-status>
        <cntry-lookup>http://rps-svcs.sun.com/services/countrylookup</cntry-lookup>
        <predownload></predownload>
        <options>/installmethod=jau FAMILYUPGRADE=1 SPWEB=http://javadl-esd.sun.com/update/1.7.0/sp-1.7.0_25-b17</options>
        <urlinfo>24595ec7f861bc67e572f1e4ad3992441335e1a7</urlinfo>
      </information>
    </java-update>
    

    The version tag contains the full version number.

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

    You could parse the Java SE Downloads page to extract the Java versions.
    That way, you get the version of both JDK6 and JDK7, which allows you to test your particular JDK (6 or 7) against the latest Oracle one.
    (As opposed to the Free Java Download page, which only lists the JDK7)

    Her is a crude script in Go, which you can compile on Windows, Unix, MacOs into a single independent executable, and use within a command line or a script:

    package main
    
    import (
        "bytes"
        "encoding/xml"
        "fmt"
        "io/ioutil"
        "net/http"
        "os/exec"
        "regexp"
    )
    
    type Jdk struct {
        Url    string
        Ver    string
        update string
    }
    
    func main() {
        resp, err := http.Get("http://www.oracle.com/technetwork/java/javase/downloads/index.html")
        if err != nil {
            fmt.Printf("Error on http Get: %v\n", err)
            return
        }
        bodyb, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            fmt.Printf("QueriesForOwner: error in ReadAll: %v\n", err)
            return
        }
        br := bytes.NewBuffer(bodyb)
        jdkre, err := regexp.Compile(`h3[^\r\n]+(/technetwork/java/javase/downloads/(jdk(?:6|7)(?:u(\d+))?)-downloads-\d+\.html)`)
        if err != nil {
            fmt.Printf("extract: error in regexp compilation: %v\n", err)
            return
        }
        jdks := jdkre.FindAllSubmatch(br.Bytes(), -1)
        jdk7 := Jdk{string(jdks[0][4]), string(jdks[0][5]), string(jdks[0][6])}
        jdk6 := Jdk{string(jdks[1][7]), string(jdks[1][8]), string(jdks[1][9])}
        fmt.Printf("Jdk7: %v\nJdk6: %v\n", jdk7, jdk6)
        jver, err := exec.Command("java", "-version").CombinedOutput()
        if err != nil {
            fmt.Printf("*ExitError from java -version:", err)
            return
        }
        fmt.Println("JVer: '", string(jver), "'")
        jverre, err := regexp.Compile(`1.(\d).\d(?:_(\d+))"`)
        jvers := jverre.FindSubmatch(jver)
        jj := string(jvers[0])
        jv := string(jvers[1])
        ju := string(jvers[2])
        jdk := jdk6
        if jv == "7" {
            jdk = jdk7
        }
        if jdk.update != ju {
            fmt.Println("Local JDK *NOT* up-to-date: you have ", jj, ", Oracle has ", jdk.Ver)
        } else {
            fmt.Println("Local JDK *up-to-date*: you have ", jj, ", equals to Oracle, which has", jdk.Ver)
        }
    }
    

    Again, this is a crude script, oriented toward JDK, and you would need to adapt it to your specific need, making its output and exit status match what you need for your script.

    On my (PC) workstation, it returns:

    Jdk7: {/technetwork/java/javase/downloads/jdk7u9-downloads-1859576.html jdk7u9 9}
    Jdk6: {/technetwork/java/javase/downloads/jdk6u37-downloads-1859587.html jdk6u37 37}
    JVer: ' java version "1.6.0_31"
    Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
    Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode, sharing)
     '
    Local JDK *NOT* up-to-date: you have  1.6.0_31" , Oracle has  jdk6u37
    
    0 讨论(0)
  • 2020-12-14 03:27

    I have solved a similar issue some time ago with this groovy script (disclaimer: is somehow a "toy" script):

    @Grapes([
        @Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2.1')
    ])
    
    def slurper = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
    def url = new URL("http://www.java.com/download/manual.jsp")
    
    def html
    url.withReader { reader ->
        html = slurper.parse(reader)
    }
    def lastJava = html.body.div.div.div.strong.text()
    
    println "Last available java version: ${lastJava}"
    println "Currently installed java version: ${System.properties["java.version"]}"
    

    It yields something like:

    Last available java version: 
    Version 7 Update 9
    
    Currently installed java version: 1.7.0_07
    

    If you want to avoid maintenance issues due to changes to the page structure, maybe a better option is to search for a line containing "Version x Update y".

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