title: Redirect in-memory class log to ObjectOutputStream
body: <p>I'm trying to build a client-server IDE, and I'm having problems with getting the error log/exception log of runtime exceptions from the in-memory compiled/run class. </p>
  
  <p>My code looks like this:</p>
  
  <pre><code>public class ServerThread implements Runnable {
  
  private ObjectInputStream objectInputStream;
  private ObjectOutputStream objectOutputStream;
  private ByteArrayOutputStream byteArrayOutputStream;
  private PrintStream out;
  
  public ServerThread(Socket socket) {
      try {
          objectInputStream = new ObjectInputStream(socket.getInputStream());
          objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
          byteArrayOutputStream = new ByteArrayOutputStream();
          out = new PrintStream(byteArrayOutputStream);
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  }
  
  @Override
  public void run() {
      try {
          StringBuffer sourceCode = new StringBuffer();
          sourceCode.append(objectInputStream.readObject());
  
          Class&lt;?&gt; myClass = MyJavaCompiler.compile("Test", sourceCode.toString());
          Method mainMethod = myClass != null ? myClass.getDeclaredMethod("main", String[].class) : null;
          Object myObject = myClass.getConstructor().newInstance();
          try {
              System.setOut(out);
              System.out.print(mainMethod.invoke(myObject, new Object[]{null}));
              objectOutputStream.writeObject(byteArrayOutputStream.toString());
          } catch (InvocationTargetException e) {
              Class&lt;?&gt;[] exceptionsList = mainMethod.getExceptionTypes();
              for(Class&lt;?&gt; c : exceptionsList){
                  objectOutputStream.writeObject(c.getName());
                  }
              }
          } catch (Exception e) {}
      }
  }
  </code></pre>
  
  <p>and this is the compiler:</p>
  
  <pre><code>import javax.tools.*;
  import java.util.Arrays;
  
  /**
   * Created by alin on 8/23/15.
   */
  public class MyJavaCompiler {
  
  private static JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
  
  public static Class&lt;?&gt; compile(String className, String sourceCodeInText) throws Exception {
  
      JavaSourceFromString sourceCode = new JavaSourceFromString(className, sourceCodeInText);
  
      CompiledCode compiledCode = new CompiledCode(className);
      Iterable&lt;? extends JavaFileObject&gt; compilationUnits = Arrays.asList(sourceCode);
      DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(ClassLoader.getSystemClassLoader());
      MyJavaFileManager fileManager = new MyJavaFileManager(javac.getStandardFileManager(null, null, null), compiledCode, dynamicClassLoader);
  
      JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, null, null, null, compilationUnits);
      boolean result = task.call();
      if(result){
          System.out.println("SUCCESS");
          return dynamicClassLoader.loadClass(className);
      }
      else{
          System.out.println("FAIL");
          return null;
          }
      }
  }
  </code></pre>
  
  <p>At this moment, when I have, for example NullPointerException in my in-memory class, I get "[]" sent to client.</p>
  
  <p>EDIT:</p>
  
  <p>Thanks Holger for your answer and feedback. I managed to get the code to do what i wanted by simply adding e.printStackTrace(PrintStream), code below:</p>
  
  <pre><code>try {
              System.setOut(out);
              System.out.print(mainMethod.invoke(myObject, new Object[]{null}));
              objectOutputStream.writeObject(byteArrayOutputStream.toString());
          } catch (InvocationTargetException e) {
              e.printStackTrace(out);
              objectOutputStream.writeObject(byteArrayOutputStream.toString());
          }
  </code></pre>
  
  <p>The idea was to redirect my error output from my in-memory class to client from server.</p>