Cannot debug Java application with Eclipse - java

I have a Java (Vert.x) application on my machine that I am trying to attach Eclipse to for debugging.
I usually start the Java application in my console like so:
java -jar build/libs/my-app.jar
Upon reading about debugging, I am attempting to start the app as follows:
java -jar build/libs/my-app.jar -Xdebug -Xrunjdwp:transort:transport=dt_socket,address=8001,server=y,suspend=n
The app seems to start up in the console fine when I run this.
I then go into Eclipse, and try to connect to the app via debugging via Run -> Debug Configurations. This is what my debug configuration looks like:
When I click debug, I get an error box that pops up and says that the connection is refused (I covered the name of my real app). See below:
What am I doing wrong? How can I get remote debugging to connect to my app with Eclipse?

According to my reading of this JDWP documentation, your -Xrunjdwp option is incorrect:
-Xrunjdwp:transort:transport=dt_socket,address=8001,server=y,suspend=n
should be
-Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n
If your system has multiple IP addresses, there could be some confusion about which IPs the agent is listening for connections on. You could force a specific address; e.g.
-Xrunjdwp:transport=dt_socket,address=127.0.0.1:8001,server=y,suspend=n
and use the matching IP and port in the Eclipse debug connection parameters.
AND ... as Dave Thompson spotted ... all JVM options must be placed before the -jar argument. (Anything after the -jar name.jar will be treated as command line arguments for your application.)

Related

Cannot connect to the remote server via Intellij for debug

I have a remote Linux server and an application on it which I need to debug. I start the .jar file through the terminal in Intellij with line:
sudo java -Dspring.profiles.active=test -Dspring.config.additional-location=file:/.../external.properties -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar /.../JARFILE.jar
Jar file is started correctly, the application is working, but I cannot debug it remotely via IDEA with those settings: remote debugging configuration.
It throws an error saying "java.net.ConnectException: Connection timed out: connect." after a few seconds from I clicked the debug button.
What should I do?
You say that you are unable to connect to the JVM via a debugger.
I'd expect that the reason for this error is the fact that you are not launching the JVM with the correct input arguments so that it can run in debug mode.
The easiest fix for this issue would be to allow IntelliJ to run it for you with correct configuration.
You say that you want to deploy the app to a remote server and debug it from there. Why not use this functionality in IntelliJ, which will guarantee that correct flags are set.
I think this value address=*:5005 might be wrong, try running it with address=5005

How to enable remote debugging for an Eclipse Application?

Im trying to enable remote debugging for an eclipse plugin project, for the purpose of using IntelliJ IDEA as a debugger.
The steps I've taken so far:
Launch Eclipse.
Run -> Debug Configurations...
Create a new "Eclipse Application" (as to launch a local workspace which loads the plugin).
Add -Xdebug -Xrunjdwp:transport=dt_socket,address=1044,server=y,suspend=n to VM arguments.
Press the Debug button.
I get the following error:
ERROR: Cannot load this JVM TI agent twice, check your java command line for duplicate jdwp options.
Error occurred during initialization of VM
agent library failed to init: jdwp
What steps should I be taking to get IntelliJ IDEA to be able to debug an Eclipse Plugin running in a runtime workspace?
Run -> Debug Configurations...
Add -Xdebug -Xrunjdwp:transport= dt_socket,address=1044,server=y,suspend=n to VM arguments.
This is redundant. Using a debug configuration already starts the application with the eclipse debugger attached to the process. Start it as a run configuration instead if you want to enable debugging through a server socket.
You can then create an additional "remote java application" debug configuration to attach to that socket.
I would suggest you NOT try to launch the Eclipse app from within Eclipse.
Export the app to be a stand alone eclipse application.
Modify the eclipse.ini file and add the necessary debug parameters to the '-vmargs' entry.
Launch the stand alone app as you normally would.
Attach the development Eclipse instance to the test app as you would normally do for a remote debug.
Tomcat Configuration:->
By default remote debug happens at 8000 port.If you want to change this then go to catalina.bat file and update this line
set JPDA_ADDRESS=localhost:8000 to desired port no.
And you can also set this property in setenv.bat file. Generally this file is not there in tomcat so just create one batch file with name
setenv and write this line set JPDA_ADDRESS=localhost:8000.
Now go to bin directory then open command promt and write "catalina jpda start". It will make the tomcat run in debug mode. You can verify it
by seeing something like this "Listening for transport dt_socket at address: 8000"
1)Eclipse
Go to run->debug configuration->Remote Java Application->Click on new Launch configuration
And then fill the detials like project ,port(Give same port no as you have configured in tomcat), host(If you are using local you can give
local then apply and debug
I am using eclipse 2020.09 .
Eclipse remote debug img
2)Intellij Idea:
Go to Add configuration->click on +->and select remote jvm debug->Then give the same port no and name(any name) then apply.
Intellij Idea Remote Debug img

How do I add a jar to jconsole classpath in windows?

I'm trying to invoke a JMX MBean via Jconsole, but the method that I'm calling receive an object that needs to be on jconsole classpath in order to work.
I've tried this and the jconsole does not open (and no error is shown):
jconsole -J-Djava.class.path=%JAVA_HOME%\lib\jconsole.jar;path_to_newjar_\newjar.jar
Take jconsole.jar out of the -J classpath. It's already set in the jconsole launcher.
===== Update =====
Hmmm.... I take back my suggestion. I have a windows batch file I use [which works] which adds a JAR to the classpath. The intent is to add the JMXMP JMX client into jconsole and then launch to the argument specified JMX Service URL. It looks like this:
#echo off
start /B jconsole -J-Djava.class.path=%JAVA_HOME%\lib\jconsole.jar;MY-JAR-PATH\jmx-optional-1.0-b02-SNAPSHOT.jar service:jmx:jmxmp://localhost:%1
If a command like that does not work (i.e. jconsole still will not launch, but does not error out either), then you need to figure out where it is stalling. 2 suggestions for this:
Launch another jconsole and attach to the stalled jconsole by PID, switch to the Threads tab and eyeball what's going on in the main thread.
Or, (since it looks like you're in windows) hit Ctrl-Break and hopefully it will print out a thread dump to the console and you can then eyeball the main thread going-ons there.
Post back if you get anything (or if you don't....)
If you want to have access to both local & remote processes, the above answers are still missing a step.
From the Java 8 oracle docs:
If the JMX agent uses a connector which is not included in the Java platform, you need to add the connector classes to the class path when you run the jconsole command, as follows.
$ jconsole -J-Djava.class.path=JAVA_HOME/lib/jconsole.jar:JAVA_HOME/lib/tools.jar:connector-path
In the command above, connector-path is the directory or the Java archive (Jar) file containing the connector classes that are not included in the JDK, that are to be used by JConsole.
In your case, then, the command would be:
$ jconsole -J-Djava.class.path=%JAVA_HOME%\lib\jconsole.jar;%JAVA_HOME%/lib/tools.jar:path_to_newjar_\newjar.jar
When you leave off the JAVA_HOME/lib/tools.jar, local processes are no longer available.
Solution in Windows is to use quotes on your classpath, for example:
jconsole -J-Djava.class.path="%JAVA_HOME%\lib\jconsole.jar;path_to_newjar_\newjar.jar"

eclipse: debug programmatically initiated process

I'm programmatically executing a java process via another java process in eclipse:
Process process = Runtime.getRuntime().exec(command, envp, dir);
Is there any way to tell eclipse to debug the child process?
Alternatively, I could solve this by chaining multiple launch configurations, ie launch process A, on completion launch process B - provided B could be launched in debug mode.
If you use the "Remote Debug" feature in Eclipse then you can point it at any JVM instance. You just need to make sure that each instance is told to use a unique JDWP port. This is how you'd do it from the command line:
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8998,server=y
So, change the 'address' part to whatever port you want, then you can point Eclipse at that port. Here's some more information:
http://java.dzone.com/articles/how-debug-remote-java-applicat
Add a command line option to the child process as follows:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=9999
(you can use a different port number than 9999, it's up to you, but best choose a number above 1024)
Then in Eclipse, create a Debug Configuration of type "Remote Java Application". Set the host as localhost and the port as 9999.
This developerWorks article has plenty of extra information.
EDIT: Incidentally for more flexibility in specifying the command line and environment of your child process, use the java.lang.ProcessBuilder class instead of Runtime.exec(...).

Remote debugging a Java application

I have a java application running on linux machine. I run the java application using the following:
java myapp -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000, suspend=n
I have opened port 4000 for TCP on this Linux machine. I use eclipse from Windows XP machine and try to connect to this application. I have opened the port in windows also.
Both machines are on the LAN but I can't seem to connect the debugger to the Java application. What am I doing wrong?
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!
For JDK 1.3 or earlier :
-Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006
For JDK 1.4
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006
For newer JDK :
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=6006
Please change the port number based on your needs.
From java technotes
From 5.0 onwards the -agentlib:jdwp option is used to load and specify
options to the JDWP agent. For releases prior to 5.0, the -Xdebug and
-Xrunjdwp options are used (the 5.0 implementation also supports the -Xdebug and -Xrunjdwp options but the newer -agentlib:jdwp option is preferable as the JDWP agent in 5.0 uses the JVM TI interface to the
VM rather than the older JVMDI interface)
One more thing to note, from JVM Tool interface documentation:
JVM TI was introduced at JDK 5.0. JVM TI replaces the Java Virtual Machine Profiler Interface (JVMPI) and the Java Virtual Machine Debug Interface (JVMDI) which, as of JDK 6, are no longer provided.
Answer covering Java >= 9:
For Java 9+, the JVM option needs a slight change by prefixing the address with the IP address of the machine hosting the JVM, or just *:
-agentlib:jdwp=transport=dt_socket,server=y,address=*:8000,suspend=n
This is due to a change noted in https://www.oracle.com/java/technologies/javase/9-all-relnotes.html#JDK-8041435.
For Java < 9, the port number is enough to connect.
Steps:
Start your remote java application with debugging options as said in above post.
Configure Eclipse for remote debugging by specifying host and port.
Start remote debugging in Eclipse and wait for connection to succeed.
Setup breakpoint and debug.
If you want to debug from start of application use suspend=y , this will keep remote application suspended until you connect from eclipse.
See Step by Step guide on Java remote debugging for full details.
I'd like to emphasize that order of arguments is important.
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar app.jar command opens debugger port,
but java -jar app.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 command doesn't. It will pass everything after app.jar as command-line arguments.
This is how you should setup Eclipse Debugger for remote debugging:
Eclipse Settings:
1.Click the Run Button
2.Select the Debug Configurations
3.Select the “Remote Java Application”
4.New Configuration
Name : GatewayPortalProject
Project : GatewayPortal-portlet
Connection Type: Socket Attach
Connection Properties:
i) localhost ii) 8787
For JBoss:
1.Change the /path/toJboss/jboss-eap-6.1/bin/standalone.conf in your vm as follows:
Uncomment the following line by removing the #:
JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n"
For Tomcat :
In catalina.bat file :
Step 1:
CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
Step 2:
JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
Step 3: Run Tomcat from command prompt like below:
catalina.sh jpda start
Then you need to set breakpoints in the Java classes you desire to debug.
for everybody that has the problem that if you really do remote debugging from 1 machine to the other then using :
-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n
is not enough because that binds now (at least on unix/osx machines) to localhost so you can only connect to it from localhost.
If you try to remote debug this then you will get connection refused for this. From i think Java 9 on you need to do:
-agentlib:jdwp=transport=dt_socket,server=y,address=*:8000,suspend=n
or give an ip that it needs to bind on for hat *

Categories

Resources