Search code examples
javapythondebuggingjvmpy4j

Connect to JVM for debugging that has been spawned by a Python script


I have a Python code, which I run with IntelliJ. The Python code will execute a java command, which will eventually spawn a JVM. The Python and JVM communicates using Py4J and custom sockets. How can I connect to the JVM for debugging purposes using IntelliJ?


Solution

  • I assume that your Python code spawns java command using os.system call. If you want to debug it from your IDE then you should turn on debugging on the spawned JVM (see this topic for possible options to enable JVM debugging):

        os.system("java -jar your.jar -Xdebug -Xrunjdwp:server=y, transport=dt_socket,address=4000, suspend=y");
    

    In above code the spawned JVM will listen on port 4000 for incoming debugger connections. Now you can use InteliJ remote debug configuration feature to connect to the spawned JVM using provided port. Note the suspend=y parameter - it means that the JVM will be started as suspended until debugger is connected. This will prevent the case that you miss the interesting JVM execution part you want to debug or prevents the case when JVM exited after main thread had finished before you can even started your InteliJ remote debug session.