fuin.org

Small Open Source Java Tools and Libraries

AppStarter

The "AppStarter" is a small application that starts any application or applet by injecting the necessary jars, directories with jars or classpath directories into the classpath.
 
Screenshot:
Screenshot
 
Try it with Webstart:
Webstart me now!
 
Example source:
package org.fuin.utils4swing.appstarter;

import javax.swing.SwingUtilities;

import org.fuin.utils4swing.common.Utils4Swing;

/**
 * Shows the different start options for the "AppStarter".
 */
public final class Examples {

    /**
     * Private constructor.
     */
    private Examples() {
        throw new UnsupportedOperationException(
                "Creating an instance is not allowed (utility class!)!");
    }

    /**
     * Create a pre-populated configuration for the Sun "password store" swing
     * example.
     *
     * @return New config instance.
     */
    private static AppStarterConfig createExampleConfig() {

        // Create a configuration and set title for the "AppStarter" frame
        final AppStarterConfig config = new AppStarterConfig();
        config.setFrameTitle("MyAppStarter");

        // We want to start a "standalone" Java app
        config.setStartType(StartType.APPLICATION);

        // Add a single jar, a directory with libraries and a binary directory
        // to the CP
        config.addClasspathEntry(new ClasspathJarFileEntry(
                "C:\\swing-examples\\password-store-example.jar"));
        config.addClasspathEntry(new ClasspathJarsDirEntry(
                "C:\\swing-examples\\lib"));
        config.addClasspathEntry(new ClasspathBinDirEntry(
                "C:\\swing-examples\\classes"));

        // Set the name of the main class
        config.setMainClass("passwordstore.ui.PasswordStoreApplication");

        // Add some (in case of the "PasswordStoreApplication" never used)
        // command line arguments
        config.addArgument("useless");
        config.addArgument("dummy");
        config.addArgument("arguments");

        return config;
    }

    /**
     * Start the "AppStarter" with a pre-populated config screen.
     */
    private static void prepopulateConfigScreen() {

        // Create a configuration and set title for the "AppStarter" frame
        final AppStarterConfig config = createExampleConfig();

        // Create a listener to be informed when the target application was
        // started
        final AppStarterListener listener = new AppStarterListener() {
            public void appStarted() {
                System.out
                        .println("The 3rd Party application has just started!");
            }
        };

        // Start the configuration screen pre-populated with above config
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Utils4Swing.initSystemLookAndFeel();
                (new AppStarter(config, listener)).execute();
            }
        });

    }

    /**
     * Start the "AppStarter" with an empty config screen.
     */
    private static void startEmptyConfigScreen() {

        // Create a configuration and set title for the "AppStarter" frame
        final AppStarterConfig config = new AppStarterConfig();
        config.setFrameTitle("MyAppStarter");

        // Create a listener to be informed when the target application was
        // started
        final AppStarterListener listener = new AppStarterListener() {
            public void appStarted() {
                System.out
                        .println("The 3rd Party application has just started!");
            }
        };

        // Start the configuration screen
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Utils4Swing.initSystemLookAndFeel();
                (new AppStarter(config, listener)).execute();
            }
        });
    }

    /**
     * Use the "AppStarter" without config screen to start the target
     * application.
     */
    private static void startWithoutConfigScreen() {

        // Create a configuration and set title for the "AppStarter" frame
        final AppStarterConfig config = createExampleConfig();

        // Create a listener to be informed when the target application was
        // started
        final AppStarterListener listener = new AppStarterListener() {
            public void appStarted() {
                System.out
                        .println("The 3rd Party application has just started!");
            }
        };

        // Start the target application
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Utils4Swing.initSystemLookAndFeel();
                try {
                    AppStarter.start(config.getStartType(), config
                            .getClassPath(), config.getMainClass(), config
                            .getArguments(), listener);
                } catch (StartApplicationException e) {
                    throw new RuntimeException(
                            "Sorry, starting the target app failed!", e);
                }
            }
        });
    }

    /**
     * Main method to start the "AppStarter" with different modes.
     *
     * @param args
     *            Command line arguments. Only argument is the mode:
     *            "prepopulate", "start" or NULL.
     */
    public static void main(final String[] args) {
        if ((args == null) || (args.length == 0)) {
            startEmptyConfigScreen();
        } else {
            if (args[0].equalsIgnoreCase("prepopulate")) {
                prepopulateConfigScreen();
            } else if (args[0].equalsIgnoreCase("start")) {
                startWithoutConfigScreen();
            } else {
                startEmptyConfigScreen();
            }
        }
    }
}