Ericthecmh Posted July 11, 2013 Share Posted July 11, 2013 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 Link to comment Share on other sites More sharing options...
Diclonius Posted July 11, 2013 Share Posted July 11, 2013 Looks nice, only thing I can think to suggest is using getVersion() and update the version in the script manifest instead of making a new variable to hold the version Link to comment Share on other sites More sharing options...
Ericthecmh Posted July 11, 2013 Author Share Posted July 11, 2013 Looks nice, only thing I can think to suggest is using getVersion() and update the version in the script manifest instead of making a new variable to hold the version The version variable in the script manifest is a double right? That wouldn't allow me to number my versions as things like 0.8.3 because 0.8.3 can't be stored as a double. But yeah it does get annoying to change the version number in three different places instead of just updating the version in the script manifest :P Eric Link to comment Share on other sites More sharing options...
Diclonius Posted July 11, 2013 Share Posted July 11, 2013 (edited) Yeah and also say I was releasing version 30 of my script, I would write the version as 1.30 but it becomes 1.3 Edited July 11, 2013 by Diclonius 1 Link to comment Share on other sites More sharing options...
H0ppy Posted July 13, 2013 Share Posted July 13, 2013 Yeah and also say I was releasing version 30 of my script, I would write the version as 1.30 but it becomes 1.3 Time to release 2.0 then xD Nice updater! I'll test this out and improve if I can! Thx for thx for the snippet! Grtz H0ppy Link to comment Share on other sites More sharing options...
Doout Posted July 13, 2013 Share Posted July 13, 2013 This part of the script is useless if (System.getProperty("os.name").startsWith("Window")) { filename = "\\OSBot\\scripts\\CMHEssMiner.jar"; } else { filename = "/OSBot/scripts/CMHEssMiner.jar"; } just use "/" for both. Link to comment Share on other sites More sharing options...
Ericthecmh Posted July 13, 2013 Author Share Posted July 13, 2013 This part of the script is useless if (System.getProperty("os.name").startsWith("Window")) { filename = "\\OSBot\\scripts\\CMHEssMiner.jar"; } else { filename = "/OSBot/scripts/CMHEssMiner.jar"; } just use "/" for both. Does windows take both? I didn't know that. Thanks Eric Link to comment Share on other sites More sharing options...
Cory Posted July 15, 2013 Share Posted July 15, 2013 This part of the script is useless if (System.getProperty("os.name").startsWith("Window")) { filename = "\\OSBot\\scripts\\CMHEssMiner.jar"; } else { filename = "/OSBot/scripts/CMHEssMiner.jar"; } just use "/" for both. Does windows take both? I didn't know that. Thanks Eric If you really wanted it to be cross platform, just use System.getProperty("file.separator") 1 Link to comment Share on other sites More sharing options...
Kronos Posted July 18, 2013 Share Posted July 18, 2013 (edited) If you really wanted it to be cross platform, just use System.getProperty("file.separator") The only correct answer I've seen on this topic Also "user.dir" will give you OSBot's directory: String fs = System.getProperty("file.separator"); String dir = System.getProperty("user.dir") + fs + "scripts" + fs + jarName; All in all quite a good updater though. A bit sad that this isn't already included in the client. Normally you would write the file to a temp location just in case the existing one is currently being used, but in OSBot's case you don't need to. Edited July 18, 2013 by Kronos Link to comment Share on other sites More sharing options...