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?
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!
the method of debug jar package in Eclipse:
- Select the jar in Referenced Lib of the project.
- In java source attachment you can specify the location of the source code.
- Invoke some classes of jar file from your project.
- 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.