This might not be the best way to write an autoupdater, but it's the way I've done it:
Setup:
On server, have the jar file (CMHEssMiner.jar) and a versioninfo file.
CMHEssMiner.jar is the jar file (duh)
versioninfo contains one line with the version (eg. "0.8.3 BETA" -- no quotes)
In the code, create a constant:
public final String version = "0.8.3 BETA"
in the onStart method:
URL url = null;
try {
url = new URL("http://osbot.no-ip.org/distfiles/versioninfo");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = null;
try {
is = url.openConnection().getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
String line = reader.readLine();
if (!line.equals(currentVersion)) {
updateScript(line);
canStart = false;
return;
}
} catch (IOException e) {
}
I created a separate method for the updating part:
private void updateScript(String version) {
try {
URL url = new URL(
"http://osbot.no-ip.org/distfiles/CMHEssMiner.jar");
InputStream in = url.openStream();
int bytesRead;
byte[] buf = new byte[4 * 1024];
String filename;
if (System.getProperty("os.name").startsWith("Window")) {
filename = "\\OSBot\\scripts\\CMHEssMiner.jar";
} else {
filename = "/OSBot/scripts/CMHEssMiner.jar";
}
OutputStream os = (new FileOutputStream(
System.getProperty("user.home") + filename));
while ((bytesRead = in.read(buf)) != -1) {
os.write(buf, 0, bytesRead);
}
os.flush();
os.close();
in.close();
JOptionPane.showMessageDialog(null, "Script has been automatically updated to version" + version + ". Please refresh your scripts list and run the script again.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
If anyone has a better way to do this or suggestions, I'd love to hear them
Eric