2

For example, we can enable java remote debug by adding following to command line.

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

But my application is running in yarn, I'm not sure which port is available.

So I want enable java debug in my code.

First I detect a available port and log in my program, then I can use this port to debug my application.

1

2 Answers 2

1

The address property specifies host (optionally) and port (only the port if host is left out). So address=5005 specifies the port 5005 in your case. If you want your program to wait until you connect your debugger, switch suspend=n to suspend=y.

Edit: Maybe I misunderstood your question. In case you want to enable debugging programmatically, this won't be possible as the debugging facility JPDA is not exposing a Java API nor any other way to start and stop it programmatically.

Sign up to request clarification or add additional context in comments.

2 Comments

So it is impossible to do this?
As stated: No, it's not possible to start a debugger after the JVM has started your program. Same is true for stopping the debugger.
1

I'm not sure this can be done from code; however according to an answer to this old question, it is possible to enable debugging for an already-running JVM using jsadebugd

As mentioned in said answer, the feature is marked experimental and unsupported so your mileage may vary.

1 Comment

jsadebugd enables remote connection of some debugging tools like jmap, jinfo and jstack. They help in some areas of debugging, but not the in the same way jpda jdb do. BTW it is possible to connect jdb to any running process (regardless of -agentlib:jdwp... setting), but that can be used to inspect few things and freezes the running application. Uncertain if that is what the author of this question wanted to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.