KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JarNewVersionReplacer


1
2 import javax.swing.JOptionPane JavaDoc;
3
4 import java.util.Date JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.io.File JavaDoc;
7
8 /** class JarNewVersionReplacer. runs in a separate thread, started at app shutdown.
9 * replaces the old with the new jar file.
10 must be kept in Proguard, if obfuscation:
11
12 -keepclasseswithmembers class JarNewVersionReplacer {
13     public static void main(java.lang.String[]);
14 }
15 -keep class JarNewVersionReplacer* # also the inner classes!!
16
17 */

18 public final class JarNewVersionReplacer
19 {
20    public JarNewVersionReplacer(File JavaDoc old, File JavaDoc newFile)
21    {
22       while(true)
23       {
24          try
25          {
26             Thread.sleep(1000);
27             if(old.exists())
28             {
29               SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("MMM dd yyyy");
30               old.renameTo(new File JavaDoc(old.getAbsolutePath()+"_ "+df.format(new Date JavaDoc(old.lastModified()))+".back")); // TODO:+DATE
31
}
32             if(newFile.renameTo(old))
33             {
34                JOptionPane.showMessageDialog(null, "Your snowmail copy has been successfully updated.", "Snowmail updater", JOptionPane.INFORMATION_MESSAGE);
35               break;
36             }
37          }
38          catch(Exception JavaDoc e) {
39            e.printStackTrace();
40            JOptionPane.showMessageDialog(null, "Can't replace "+old+":\n "+e.getMessage(), "Updater error", JOptionPane.ERROR_MESSAGE);
41          }
42       }
43
44       new File JavaDoc("JarNewVersionReplacer").deleteOnExit();
45
46       System.exit(0);
47    }
48
49    /** Launched by the restarter shutdownhook in a separate JVM.
50    * MUST BE KEEP !
51    */

52    public static void main(String JavaDoc[] arguments)
53    {
54       new JarNewVersionReplacer(new File JavaDoc(arguments[0]), new File JavaDoc(arguments[1]));
55    }
56
57 }
Popular Tags