Search code examples
javadelphidelphi-xe6

Java console Input/Output/Error redirection to another (Delphi) application


I am constructing a Command Prompt Process in my Delphi application which is able to interact with the JDB to compile, run and debug a Java application. Cmd input/output/errors are handled through pipelining from the cmd to/from my Delphi application UI.

I want the output of the java program when using 'System.out' / 'System.err' to be directed to a component of my Delphi Application and I want input from my delphi application to be sent to 'System.in' allowing me to form a console in my Delphi UI similar to the console in the Eclipse IDE.

A few thoughts on approaches;

  • An obscure flag (I haven't found) in the Java compiler allowing me to redirect
  • Using 'System.setOut'/'System.setErr'/'System.setIn' along with a main method in a class which performs this initialization before pointing to the normal main method to run the users code.

NB - I have tried searching through the Eclipse sourcecode to see how they did it but as it is written in Java, I suspect they wouldn't face cross language issues I would face.


Solution

  • I have found a solution to my problem. You need two command prompt processes, each constructed with a Read, Write and Error pipe in Delphi.

    One of them runs the Java application and is set to wait for a debugger to be attached before executing and the other is for debugging and is attached to the waiting Java application.

    This is for the main application; all application console input/output/errors will be handled through this process.

    java -agentlib:jdwp=transport=dt_shmem,address=DelphiExecutingAppAddress,server=y,suspend=y MyClassFile.java
    

    This is for the debugger; all jdb console input/output/errors will be handled through this process (break points, resuming, getting object details).

    jdb -attach DelphiExecutingAppAddress
    

    N.B. Perhaps this is what David was referring to? It looks like it was my mistake when reading the jdb documentation in not finding this on the first work-through. Perhaps this thread may help others.