vendredi 4 mars 2016

How to get the output of the "System.out.print()" inside the main function when invoked using Method.invoke()

iam trying to get the result of compiling and executing a java code on th fly , i am on a server environment and i cant create .java and .class files , so i am using the java built in compiler , the code i am executing is always a class called "Main" has a public static void main() method that is printing something using system.out.print .

here is what i got :

 String resa = "output string , will be replaced with the output";
 Class<?> enclosingClass = classLoader.loadClass("Main");
  if (enclosingClass != null) {
  Method= enclosingClass.getDeclaredMethod("main", new Class[] { String[].class });
 Object value = method.invoke(null, new Object[] { null });
    resa = (String) value;}

i realised that "value" here is the value returned by the method not the output stream caused by system.out.print , please help me , how can i get that output ?





Aucun commentaire:

Enregistrer un commentaire