2

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

Helic
  • 41
  • 5
  • What environment do you have and what tools are you using for debugging? – Hulk Sep 19 '12 at 11:41
  • Maybe using two different Eclipse instances (or Eclipse and Netbeans) with Java configured for remote debugging? – Raffaele Sep 19 '12 at 11:53
  • @Raffaele wouldn't you come across the problem of having different instances of the program running? if you are trying to debug in different IDE's (say one IDE invokes an object from java, that object isn't carried to the other IDE, because they are both different processes on a different memory buffer) as far as I know netbeans and eclipse aren't atomic with one another. – classicjonesynz Sep 19 '12 at 11:54
  • @Raffaele atomicity means `all or nothing` in concurrency or `An atomic operation is an operation which is performed as a single unit of work without the possibility of interference from other operations.` (its to do with how things synchronize with one another) – classicjonesynz Sep 19 '12 at 12:42
  • @Killrawr I know what *atomic operations* are, just can't figure out what you mean by "*Netbeans and Eclipse are not atomic with one another*" - BTW I posted a working example of my concept – Raffaele Sep 19 '12 at 13:18
  • @Raffaele yeah I noticed +1 rep, I just mean because the IDE's are on a different memory space. You wouldn't be able to debug one program and then netbeans pick up where eclipse left off (or vice versa). – classicjonesynz Sep 19 '12 at 13:23
  • @Killrawr I see. *Maybe* it's not possible, but not because of the IDE processes address space. It would be a limitation of the debugging framework (but I remark **maybe** because frankly I don't know the Java architecture for debugging, neither the Python equivalent, so it may even be possible - but I don't think so) – Raffaele Sep 19 '12 at 13:35
  • @Raffaele Thanks. It is through the Java remote debug interface. I thought Eclipse has some plugin for this purpose. If I want to debug programs in any other languages in the same way and there is no remote debug feature, what to do then? – Helic Sep 19 '12 at 16:08
  • I answered your question, which explicitely names a Python script that launches a Java program. For other languages I don't know, but I guess you have to find the equivalent of *Java remote debugging*. For example the Eclipse PHP debugger has an option to *stop at first line*, and you should append a parameter to the URL to invoke it – Raffaele Sep 19 '12 at 20:18

2 Answers2

3

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.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
0

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

Not that I know of (SO: CBT and Java on Eclipse Debug). You could try other debugging methods such as logging events with log4j (for your java exceptions and so on) or go about building an ant script with junit and pyunit unit testing techniques, and aim for good coverage.

Try this website Testing Java with Jython and PyUnit, I think this is what you are after.

JUnit, the unit test framework written by Erich Gamma and Kent Beck, is not the only alternative for unit testing in the Java environment. This article provides a simple demonstration on how to use Jython, PyUnit and Ant to unit test a Java project. To provide better elements of comparison, the example provided is the same as the one available in the JUnit distribution: MoneyTest.

This article assumes that the reader has some basic knowledge of unit testing, Java, Jython or Python and possibly Apache Ant. For more information about each of those technologies, see section Resources at the end of this article. (Burgaud, 2012)

Good luck!

Community
  • 1
  • 1
classicjonesynz
  • 4,012
  • 5
  • 38
  • 78