implemeting java application update with AppLoader.java -
i hava java swing application , use auto update using apploader.java class found online at
has had experience class. can not seem implement class application , getting errors: java.io.filenotfoundexception: c:\users\home\documents\netbeansprojects\test_update\build\classes (access denied)
and
java.lang.runtimeexception: main jar not found!
yep, code seems not working. did modification code make work. please follows:
- download file through http://reportmill.com/snap1/snapcode1.jar.pack.gz
- copy file c:\users\home\documents\netbeansprojects\test_update\build\classes
copy , paste code below , give run
import java.io.; import java.lang.reflect.method; import java.net.; import java.text.; import java.util.jar.; import javax.swing.*; import java.util.zip .gzipinputstream;
/** * app */ public class apploader { // constants static final string appdirname = "snapcode"; static final string jarname = "snapcode1.jar"; static final string jarurl = "http://reportmill.com/snap1/snapcode1.jar.pack.gz"; static final string mainclass = "snap.app.app"; /** * main method - reinvokes main1() on swing thread in exception handler. */ public static void main(final string args[]) { // invoke real main exception handler try { main1(args); } catch (throwable e) { joptionpane.showmessagedialog(null, e.tostring()); e.printstacktrace(); } } /** * main method: - gets main jar file default, if missing - updates main * jar file local update file, if loaded - load main jar * urlclassloader, load main class , invoke main method - check * update remove site in background */ public static void main1(final string args[]) throws exception { // make sure default jar in place try { copydefaultmainjar(); } catch (exception e) { joptionpane.showmessagedialog(null, e.tostring()); e.printstacktrace(); } // if update jar exists, copy place file jar = getappfile(jarname); file updatejar = getappfile(jarname + ".update"); if (updatejar.exists()) { copyfile(updatejar, jar); jar.setlastmodified(updatejar.lastmodified()); updatejar.delete(); } // if jar doesn't exist complain bitterly if (!jar.exists() || !jar.canread()) throw new runtimeexception("main jar not found!"); // check updates in background thread if (args.length == 0 || !args[0].equals("-snap")) new thread(new runnable() { public void run() { checkforupdatessilent(); } }).start(); // create urlclassloader main jar file, app class , invoke // main // urlclassloader ucl = new urlclassloader( // new url[] { jar.touri().tourl() }); // class cls = ucl.loadclass(mainclass); // ucl.close(); // method meth = cls.getmethod("main", new class[] { string[].class }); // meth.invoke(null, new object[] { args }); // if (cls == object.class) // ((closeable) ucl).close(); // getting rid of warning message ucl } /** * copies default main jar place initial run. */ private static void copydefaultmainjar() throws ioexception, parseexception { // main jar app package , location of working jar file url url = apploader.class.getprotectiondomain().getcodesource() .getlocation(); string path0 = url.getpath(); path0 = urldecoder.decode(path0, "utf-8"); path0 = path0 + "snapcode1.jar.pack.gz" ; file jar0 = getappfile(jarname); file jar1 = new file(path0); // if app package main jar newer, copy place , set time if (jar0.exists() && jar0.lastmodified() >= jar1.lastmodified()) return; copyfile(jar1, jar0); } /** * check updates. */ private static void checkforupdatessilent() { try { checkforupdates(); } catch (exception e) { e.printstacktrace(); } } /** * check updates. */ private static void checkforupdates() throws ioexception, malformedurlexception { // url connection , lastmodified time file jarfile = getappfile(jarname); url url = new url(jarurl); urlconnection connection = url.openconnection(); long mod0 = jarfile.lastmodified(), mod1 = connection.getlastmodified(); if (mod0 >= mod1) { system.out.println("no update available @ " + jarurl + '(' + mod0 + '>' + mod1 + ')'); return; } // update file , write jarname.update system.out.println("loading update " + jarurl); byte bytes[] = getbytes(connection); system.out.println("update loaded"); file updatepacked = getappfile(jarname + ".pack.gz"), updatefile = getappfile(jarname + ".update"); writebytes(updatepacked, bytes); system.out.println("update saved: " + updatepacked); unpack(updatepacked, updatefile); system.out.println("update unpacked: " + updatefile); updatefile.setlastmodified(mod1); updatepacked.delete(); // let user know swingutilities.invokelater(new runnable() { public void run() { joptionpane .showmessagedialog(null, "a new update available. restart application apply"); } }); } /** * returns main jar file. */ private static file getappfile(string aname) { return new file(getappdir(), aname); } /** * returns main jar file. */ private static file getappdir() { return getappdatadir(appdirname, true); } /** * * utility methods apploader. * */ /** * copies file 1 location another. */ public static file copyfile(file asource, file adest) throws ioexception { // input stream, output file , output stream fileinputstream fis = new fileinputstream(asource); file out = adest.isdirectory() ? new file(adest, asource.getname()) : adest; fileoutputstream fos = new fileoutputstream(out); // iterate on read/write until bytes written byte[] buf = new byte[8192]; (int = fis.read(buf); != -1; = fis.read(buf)) fos.write(buf, 0, i); // close in/out streams , return out file fis.close(); fos.close(); return out; } /** * writes given bytes (within specified range) given file. */ public static void writebytes(file afile, byte thebytes[]) throws ioexception { if (thebytes == null) { afile.delete(); return; } fileoutputstream filestream = new fileoutputstream(afile); filestream.write(thebytes); filestream.close(); } /** * unpacks given file destination file. */ public static file unpack(file afile, file adestfile) throws ioexception { // dest file - if unpacked, return file destfile = getunpackdestination(afile, adestfile); if (destfile.exists() && destfile.lastmodified() > afile.lastmodified()) return destfile; // create streams: fileinputstream -> gzipinputstream -> jaroutputstream // -> fileoutputstream fileinputstream fileinput = new fileinputstream(afile); gzipinputstream gzipinput = new gzipinputstream(fileinput); fileoutputstream fileout = new fileoutputstream(destfile); jaroutputstream jarout = new jaroutputstream(fileout); // unpack file pack200.newunpacker().unpack(gzipinput, jarout); // close streams fileinput.close(); gzipinput.close(); jarout.close(); fileout.close(); // return destination file return destfile; } /** * returns file given packed file saved using * unpack method. */ public static file getunpackdestination(file afile, file adestfile) { // dest file - if null, create packed file minus .pack.gz file destfile = adestfile; if (destfile == null) destfile = new file(afile.getpath().replace(".pack.gz", "")); // if dest file directory, change file inside packed file // minus .pack.gz else if (destfile.isdirectory()) destfile = new file(destfile, afile.getname().replace(".pack.gz", "")); // return destination file return destfile; } /** * returns appdata or application support directory file. */ public static file getappdatadir(string aname, boolean docreate) { // user home + appdatadir (platform specific) + name (if provided) string dir = system.getproperty("user.home"); if (iswindows) dir += file.separator + "appdata" + file.separator + "local"; else if (ismac) dir += file.separator + "library" + file.separator + "application support"; if (aname != null) dir += file.separator + aname; // create file, actual directory (if requested) , return file dfile = new file(dir); if (docreate && aname != null) dfile.mkdirs(); return dfile; } /** * returns bytes connection. */ public static byte[] getbytes(urlconnection aconnection) throws ioexception { inputstream stream = aconnection.getinputstream(); // stream // connection byte bytes[] = getbytes(stream); // bytes stream stream.close(); // close stream return bytes; // return bytes } /** * returns bytes input stream. */ public static byte[] getbytes(inputstream astream) throws ioexception { bytearrayoutputstream bs = new bytearrayoutputstream(); byte chunk[] = new byte[8192]; (int len = astream.read(chunk, 0, 8192); len > 0; len = astream .read(chunk, 0, 8192)) bs.write(chunk, 0, len); return bs.tobytearray(); } // whether windows/mac static boolean iswindows = (system.getproperty("os.name") .indexof("windows") >= 0); static boolean ismac = (system.getproperty("os.name").indexof("mac os x") >= 0);}
your problem in copyfile method fileinputstream takes wrong file object
Comments
Post a Comment