mardi 6 mars 2018

Ignoring System#exit() from reflective call [duplicate]

This question already has an answer here:

I have the following question. Suppose that one has ClassA with a bog standard main class. ClassA performs a reflective invocation to ClassB#main(). ClassB's main method does a system exit at some point which in turn will also terminate the execution of ClassA#main().

Now my question is the following, is there a way to ignore the System#exit() from ClassB's main thus not killing the JVM and resuming running the main method of ClassA. Could something like that happen without a lot of tampering on ClassB?

Consider the following very simple example as a use case:

public class ClassA  {

    public static void main(String[] args) {
    try {
        ClassB.class.getMethod("main", String[].class)
            .invoke(null, (Object) null);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        e.printStackTrace();
    }
    }

}

public class ClassB {

    public static void main(String[] args) {
        System.out.println("I am running!");
        System.exit(1);
    }

}





Aucun commentaire:

Enregistrer un commentaire