dimanche 21 février 2021

Get value of System.out.println from main method called via reflection

I'm calling the main method of a class via reflection. For example:

Object o = clasz.getDeclaredConstructor().newInstance();
Method method = clasz.getMethod("main", String[].class);
method.invoke(o, new String[1]);

The called code looks as:

public class Test {
    public static void main(String[] args) {
        System.out.println("This is a test");
    }
}

The reflection works fine and I can see the message in the console.

Is there a way to register something like a binding to the method invocation, for example a PrintWriter or a custom decorated Writer, so I can get the print value as a String?





Aucun commentaire:

Enregistrer un commentaire