Java check latest version programmatically

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

提交回复
热议问题