Debug python and Java at the same time
Question: I am using Python to invoke many of my Java programs. Is it possible to use
debug perspective for both Python and Java and trace the progress in both
languages at the same time? Thanks


Answer: I downloaded two different Eclipse, one for JavaSE and when for PyDev. A
Python script starts a JVM in remote debugging mode, then the other Eclipse
instance connects via remote debugging to this JVM. This way you are able to
debug both Python and Java code, even if in two different IDEs (I don't know
if this can be done in the same instance, ie if two debugging session can
exist in the same Eclipse instance, however I don't care because I use
different Eclipse instances for Python, Java, Scala, Android...)

Create the following Java program and export a runnable JAR, for example at
the location `/home/raffaele/hello.jar`, and set a breakpoint at the line with
`System.out.println()`

    
    
    public class HelloWorld {
    
        public static void main(String[] args) {
            for (int i = 0; i < 10; i++)
                System.out.println(i);
        }
    
    }
    

Create a Python script, add a breakpoint at line `print i` and hit _Debug as
Python script_. The subprocess output should be redirected to the Eclipse
console and you should see the message **Listening for transport dt_socket at
address: 8000**

    
    
    import subprocess
    
    subprocess.call(["java", "-jar", "-Xdebug",
                     "-Xrunjdwp:transport=dt_socket,address=8000,server=y",
                     "/home/raffaele/hello.jar"])
    
    for i in range(1, 10):
        print i
    

At this point, in the JavaSE Eclipse instance, create a Remote Debug
configuration: _Run_ > _Debug Configuration_ , in the left column select
_Remote Java Application_ and in the right one choose a name, host
`localhost`, transport `socket` port `8000`. Hit _Apply_ , then _Debug_. The
Debug perspective in the JavaSE instance will be opened, and you'll see your
code suspended at `System.out.println`. In the right pane, you can inspect
local variable, for example `i = 0`. Hit _Resume_ for 10 times and the PyDev
instance will blink, because the Python breakpoint has been hit.

