vendredi 28 septembre 2018

How to wait for a reflection thread to finish

I have an application class which allows the user to download a jar file, this jar file is then accessed using reflection.

public void install() {
    File app = new File("C:/Temp/" + this.name + ".jar");
    if(!app.exists())
        download();

    URLClassLoader appLoader;
    Class<?> appBuilder = null;

    try {
        appLoader = URLClassLoader.newInstance(new URL[] { new URL("C:/Temp/" + this.name + ".jar") });
        appBuilder = appLoader.loadClass("iezon.app.App");
    } catch (MalformedURLException | ClassNotFoundException e) {
        WebSocket.addError(e);
    }

    this.application = appBuilder;
}

private void download() {
    try {
        FileUtils.copyURLToFile(new URL(this.downloadUrl), new File("C:/Temp/" + this.name + ".jar"));
    } catch (IOException e) {
        WebSocket.addError(e);
    }
}

Here, I am creating a new instance of the jar file:

public void start() {
    try {
        App.createWindow(this.application.getClass());
    } catch (InstantiationException | IllegalAccessException e) {
        WebSocket.addError(e);
    }
}

Window is custom extension of a JFrame that is used as a base template for GUI design.

public static int createWindow(Class<?> window) throws InstantiationException, IllegalAccessException {
    factory.add((Window) window.newInstance());
    factory.get(factory.size() - 1).windowId = factory.size() - 1;
    factory.get(factory.size() - 1).run();
    return factory.size() - 1;
}

Since the jar file, once instanced, cannot access this code to load the home screen window on exit, I was wondering how I can wait for the jar file instance to be closed and then relaunch the home screen:

ie in suedo code:

when (create App.createWindow(this.application.getClass()))
dies
create App.createWindow(HomeScreen.class)

Is there a way I can use wait() and notify() methods?





Aucun commentaire:

Enregistrer un commentaire