Python Java update checker

Keeping Java up-to-date isn’t always straightforward. Especially if you’ve disabled the automatic update checker because it eats up your RAM.

import subprocess, time, sys
from urllib.request import urlopen

proc = subprocess.Popen("java -version", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
localVersion = (proc.stdout.read() + proc.stderr.read()).decode("utf-8").split('"')[1]

print ("Your local Java version is {0}.".format(localVersion))

print ("Checking online version...")
try:
	tmp = urlopen("http://www.java.com/applet/JreCurrentVersion2.txt").read().decode("utf-8")
	latestVersion = tmp[:-2]
	print ("Java version online: " + latestVersion)

	if not localVersion == latestVersion:
		print ("\n     There is a newer version available: {0} .".format(latestVersion))
		if input("     Do you want to open the webpage? Y/N ").upper() == "Y":
			print ("     Opening the webpage...")
			subprocess.Popen("start http://java.com/nl/download/manual.jsp", shell=True)
	else:
		print ("\n     You are up-to-date!")

except Exception as e:
	print ("\nAn error occured: {0}.\nPlease check your internet.".format(str(e)))

time.sleep(2)
sys.exit()

I’m using subprocess.Popen to execute commandline commands. First I read the available Java version, then I grab the current version from this page¬†and compare them. Because Python 3 wants all strings as unicode, I have to decode everything I read.