What are Java command line options to set to allow JVM to be remotely debugged?
I have this article bookmarked on setting this up for Java 5 and below.
Basically run it with:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044
For Java 5 and above, run it with:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044
If you want Java to wait for you to connect before executing the application, replace suspend=n
with suspend=y
.
Enable java remote debug in code
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.
Is it possible to start java jdwp after JVM startup (aka: at runtime) without command line parameters?
In modern JVM (Java 6+) the agents use the JVM TI interface.
The JVM Tool Interface (JVM TI) is a programming interface used by development and monitoring tools. It provides both a way to inspect the state and to control the execution of applications running in the Java virtual machine (VM).
Inside JVM TI, you must enable the desired capabilities
The capabilities functions allow you to change the functionality available to JVM TI--that is, which JVM TI functions can be called, what events can be generated, and what functionality these events and functions can provide.
Which capabilities can be added when (which state of the JVM) is vendor-dependent.
JDWP is just the protocol for debugging between the JVM and the debugger. It simply leverages the capabilities of the JVM TI just like any other agent. Meanwhile, most-probably, the capabilities for debugging can only be added in the OnLoad
phase (in most JVM). (I.e: can_generate_breakpoint_events
, can_suspend
, ...)
Add Capabilities: Typically this function is used in the
OnLoad
function. Some virtual machines may allow a limited set of capabilities to be added in the live phase.
This explains why the jdwp agent must be declared on JVM startup in order to add the proper capabilities to JVM TI.
Doc: https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#capability
Credits to @Holger for pointing the direction.
Remote debugging a Java application
Edit: I noticed that some people are cutting and pasting the invocation here. The answer I originally gave was relevant for the OP only. Here's a more modern invocation style (including using the more conventional port of 8000):
java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n <other arguments>
Original answer follows.
Try this:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=n myapp
Two points here:
- No spaces in the
runjdwp
option. - Options come before the class name. Any arguments you have after the class name are arguments to your program!
Intellij - How to edit JVM command line arguments for remote debugging
No, IntelliJ IDEA only suggests you the options that are needed to enable remote debugging. All the other options for your app should be added manually to the command line when JVM is started.
configure default command line arguments for remote debugging in IntelliJ
Add -Djava.net.preferIPv4Stack=true
and -Djava.net.preferIPv6Addresses=false
into Help | Edit Custom VM Options file and restart IDE.
Debug a java application without starting the JVM with debug arguments
You may be able to use jsadebugd (JDK) to attach a debug server to the process (available on Windows with the Debugging Tools for Windows). It is marked as experimental, so you may want to try it out on a test machine first.
Usage:
jsadebugd <pid>
jdb -connect sun.jvm.hotspot.jdi.SADebugServerAttachingConnector:debugServerName=localhost
The connector name withe arg can be found using jdb -listconnectors
.
Related Topics
Getresourceasstream Returns Null
How Many Ways to Click on Webelement in Webdriver
How to Use Comparator in Java to Sort
Why Does the Foreach Statement Not Change the Element Value
How to Format the Day of the Month to Say "11Th", "21St" or "23Rd" (Ordinal Indicator)
How to Get X and Y Index of Element Inside Gridlayout
Does a Lambda Expression Create an Object on the Heap Every Time It's Executed
How to Run Unix Shell Script from Java Code
Do We Have Any Generic Function to Check If Page Has Completely Loaded in Selenium
Received Fatal Alert: Handshake_Failure Through Sslhandshakeexception
Why Is the Java Main Method Static
Random Errors When Changing Series Using Jfreechart