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.

yunfan
  • 760
  • 1
  • 11
  • 24
  • Does this answer your question? [Debug a java application without starting the JVM with debug arguments](https://stackoverflow.com/questions/376201/debug-a-java-application-without-starting-the-jvm-with-debug-arguments) – Vadzim Feb 28 '20 at 22:38

2 Answers2

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.

Florian Fray
  • 274
  • 1
  • 6
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.

brain99
  • 883
  • 5
  • 15
  • 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. – Florian Fray Sep 30 '17 at 12:58