Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting Java Application

Tags:

java

How can I introduce automatic updates and restart feature in Java Swing applications. Also I needed to roll back to previous versions. I have made a application jar file and launcher jar file, which launches the application jar file. This attempt was successful. But I can not integrate these two jar files, when creating a installer for MacOSX.

The Launcher class as follows:

public class Launcher {

private final Logger logger = Logger.getLogger(Launcher.class.getName());

private String getVersionNumber() throws IOException {
    try {
        JarFile runningJarFile = new JarFile(new File("Application.jar"));
        String versionNumber = runningJarFile.getManifest()
                .getMainAttributes().getValue("Bundle-Version");
        runningJarFile.close();
        logger.log(
                Level.SEVERE,
                new StringBuilder()
                        .append("The version number of existing Application.jar file is ")
                        .append(versionNumber).toString());
        return versionNumber;
    } catch (IOException e) {
        logger.log(
                Level.SEVERE,
                new StringBuilder()
                        .append("Could not read the version number from existing Application.jar")
                        .append(Arrays.toString(e.getStackTrace()))
                        .toString());
        throw new IOException(e);
    }
}

private void updateApplication() {
    try {
        File updateDirectory = new File("Update");
        if (updateDirectory.isDirectory()) {
            if (updateDirectory.list(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.startsWith("\\.");
                }
            }).length > 0) {
                String versionNumber = getVersionNumber();
                logger.log(
                        Level.SEVERE,
                        new StringBuilder()
                                .append("A new update is available. Rename the existing Application.jar to ")
                                .append(versionNumber)
                                .append(".jar")
                                .append(" and rename the new_Application.jar to Application.jar")
                                .toString());
                File Application = new File("Application.jar");
                Application.renameTo(new File(new StringBuilder()
                        .append(versionNumber).append(".jar").toString()));
                File newApplication = new File("Update/new_Application.jar");
                newApplication.renameTo(new File("Application.jar"));
                newApplication.delete();
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE,
                new StringBuilder().append("Could not update Application")
                        .append(Arrays.toString(e.getStackTrace()))
                        .toString());
    }
}

private void launchApplication() {
    try {
        logger.log(Level.SEVERE, "Lauching Application.jar");
        ProcessBuilder pb = new ProcessBuilder("Java", "-jar", "Application.jar");
        pb.start();
    } catch (IOException e) {
        logger.log(Level.SEVERE,
                new StringBuilder().append("Could not launch Application.jar ")
                        .append(Arrays.toString(e.getStackTrace()))
                        .toString());
    }
}

private void quitLauncher() {
    logger.log(Level.SEVERE, "Launcher is exiting");
    System.exit(0);
}

private void startApplication() {
    updateApplication();
    launchApplication();
    quitLauncher();
}

public static void main(String[] args) {
    Launcher launcher = new Launcher();
    launcher.startApplication();
}

}

Thanks

like image 624
user2067201 Avatar asked Dec 28 '25 11:12

user2067201


1 Answers

First of all, are you creating a standard installer package (mpkg or pkg)?

You should make the launcher download the update, and then execute it (with /usr/sbin/installer ). After that, the installer should have a postflight/postinstall/postprocessing script that kills any app with the name of yours (this can be as tricky as looking for a process with a given name..), and then launches the recently installed app.

This makes that your app will be launched even, after the first install (you may avoid this making it check if it is an update or not -saving a marker file from the Launcher "updating.txt" may suffice- )

Here is a full guide of how to create installer packages (almost form scratch): Making OS X Installer Packages like a Pro - Xcode Developer ID ready pkg

Or well, you may use this cool tool: http://s.sudre.free.fr/Software/Packages/about.html

I expect this all is not an overkill for what are you looking for.

like image 178
DNax Avatar answered Dec 30 '25 23:12

DNax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!