Debugging a Java child process

Hi Guys,
My project is divided to a few modules each has its own gradle build file and compiled to a jar lib.
The main module is a Swing desktop application which when needed, forks a child process which is running the JME graphics.
Here is the code for running the child process using ProcessBuilder class:

    List<String> command = new ArrayList<>();
    command.add("java");
    command.add("-XX:MaxDirectMemorySize=1024m");
    command.add("-jar");
    command.add(launcherName);

    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command(command);
    Process process = processBuilder.start();

    StreamGobbler sg = new StreamGobbler(process.getInputStream(),System.out::println);
    Executors.newSingleThreadExecutor().submit(sg);
    exitCode = process.waitFor();
    System.out.printf("Program ended with exitCode %d", exitCode);

The problem with this approach is that the child process is not attached to the debugger (I’m using InteliJ) so I can’t use breakpoints etc. and it slows down my work.
What are you doing for debugging a child process in Java?

Thanks and happy new year! :slight_smile:

I’ve not used it personally but that sounds like a use case for attaching a debugger to a running process which intelliJ supports

(Somewhat off topic but why are you forking the JVM, I didn’t think that was necessary).

This plugin may also be useful (but again I haven’t used it)

1 Like

I saw AttachMe , installed & run it but it didn’t auto attach my child process. I guess the way I’m forking it is missing something… but I’m not sure what.
The JME process is compiled to an independent jar file. This separation from the main desktop app gives me the ability to protect the desktop app from crashes of the engine + easier upgrades + complete cleanup when finishing the rendering process so I can run it again and again without any fear from memory leaks.

OK, so I found this interesting video:

which has a section about remote debugging. that did the job and I was able to debug the child process by adding this line:

command.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005");

to the process builder

1 Like