ProcessBuilder debugging
Asked Answered
T

2

7

I created an executable jar and executed it using process builder from another java program. Here's my code -

public class SomeClass {
public static void main(String[] args) {
    Process p = null;
    ProcessBuilder pb = new ProcessBuilder("java", "-jar", "src.jar");
    pb.directory(new File("/Users/vivek/servers/azkaban-0.10/TestApp/src"));
    try {
        p = pb.start();
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

}

I'm trying to now debug the src.jar from eclipse. I provided the project src as external project in my debug configuration, but it still never hits any of my break points. Is there a way to set up a debug environment for something like this?

Tetrapod answered 9/2, 2012 at 3:37 Comment(2)
So let me see if I understand you correctly -- you're calling the SomeClass code above from within Eclipse and want to debug Java source in a jar file that's being called outside of Eclipse in a separate JVM provided by the OS? I don't see this ending well for you. I'm thinking that you may do well to add logging code to the Java code held by src.Citizenry
I am so sorry for responding so late! I managed to sort out the issue. When I create a new ProcessBuilder, I just add the Xdebug option to the command and specify a port to connect to. Then just create a debug configuration in eclipse and connect to that port to debug.Tetrapod
T
8

Ok, so I managed to get this to work. Unfortunately, I cannot find the sample project I used for this, so I'll try to explain the best I can. Consider this line from above -

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "src.jar");

All I needed to do was add Xdebug as a parameter to this -

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005", "src.jar");

Then I created a debug environment in eclipse, set the port as 5005 and set a few breakpoints in the jar's source code and it worked!

Tetrapod answered 25/3, 2012 at 6:1 Comment(1)
If anyone's trying to set this up in Eclipse, I foudn the following example useful.Enact
F
0

the method of debug jar package in Eclipse:

  1. Select the jar in Referenced Lib of the project.
  2. In java source attachment you can specify the location of the source code.
  3. Invoke some classes of jar file from your project.
  4. Add break points and debug your project.

The jar file and your project is running in the same process in this case.

But when you run the jar file by ProcessBuilder, the jar file is running in another process independent of your project, of course independent of eclipse, i.e. the jar file is running the way in which you enter 'java -jar jarfile' in the command line.

Flume answered 9/2, 2012 at 3:51 Comment(1)
Your last note matches my comment that I made made earlier. Again, I think his best bet is to forget trying to debug this way and instead to get program state via logging.Citizenry

© 2022 - 2024 — McMap. All rights reserved.