Debugging Java classes which are called from Windows batch files - java

I have a java project A which is dependent on another project B. I want to debug the java class of project B while starting the batch files from project A. How can I achieve this in Eclipse ?

You can attach tell java to allow connecting to debug by passing the command line parameters (legacy style):
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
The newer style (JVM TI interface used):
-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n
You should then be able to attach eclipse to debug:
Configure Eclipse for remote debugging by specifying host and port.
host: localhost, port:8000
See this answer: What are Java command line options to set to allow JVM to be remotely debugged?

Related

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

Remote debugging Tomcat with Eclipse - breakpoints ignored

The problem:
Debugging a java web application deployed in Tomcat 7 is not working. Eclipse seems to connect to Tomcat but breakpoints are ignored when I try to debug. I did the following:
First of all I have also tried: Remote debugging Tomcat with Eclipse.
started Tomcat7 in debug mode with :
set JPDA_OPTS=-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n
catalina.bat jpda start
Debug as - > Remote Java Application with host : localhost and port : 8000.
Created a few breakpoints on a servlet where the request should suspend and let me debug but it's like there is no breakpoint set.
Thank you all,
Daniel
P.S. This is my first question so please be kind .
One possible explanation is that the class files in your WAR file (deployed on Tomcat) do not contain line numbers.
If you told us with which compiler and compiler plugin (Oracle javac, Eclipse, ANT, Maven, etc.) the class files were produced and what settings were used we may be able to provide further details.
Update
Author says in comment that ANT is used to compile. Follow-up:
The ANT javac task is just a wrapper around a concrete compiler. So, the options you can set depend on the actual javac. However, what you're looking for is debug="true" debuglevel="lines". ANT will then pass -g:lines to javac (see docs).
Sidenote: optimize="true" is ignored (check the docs).

Getting code coverage of my application using JaCoCo Java agent on Tomcat

I want to measure the code coverage of integration tests using the JaCoCo and Sonar tools.
For that, I start my Tomcat 5.5 configured with the JaCoCo agent in order to get the dump file from JaCoCo.
Thus, I set the JAVA_OPTS for that:
set JAVA_OPTS=-Xrs -XX:MaxPermSize=256m -XX:PermSize=256m -XX:NewRatio=3 -Xms512m -Xmx1024m -XX:+UseParallelGC -javaagent:C:\dev\servers\jacoco-agent.jar=destfile=C:\dev\servers\jacoco.exec,append=true,includes=my.application.*
When I start Tomcat, the C:\dev\servers\jacoco.exec file is generated, but no data is filled.
Is there something I forgot in the configuration of my server?
Regards.
I realize this may not have been an option 2 years ago when this question was asked, but presently you have some other options available to fetch the JaCoCo execution data without shutting down Tomcat (or any JVM instrumented with the JaCoCo java agent).
First take a look at the current documentation for the JaCoCo Java Agent: http://www.eclemma.org/jacoco/trunk/doc/agent.html
You can use the output=tcpserver option on the JaCoCo agent to have the Java agent listen for commands. You can set address=* to have the tcpserver listen on all interfaces, and you can set the port=6300 argument to choose the port where the tcpserver should listen.
Through the tcpserver the JaCoCo java agent can be instructed to send you the data whenever you ask for it.
If your JVM is currently exposing JMX you have another option which you can utilize without opening additional ports. By setting the jmx=true option the JaCoCo java agent exposes an MBean which you can interact with.
If you are using maven you can take a look at the plugin I recently wrote in order to gather JaCoCo data from remote JVM's while running. The project for the plugin is located at:
https://github.com/mattcj/jacocotogo
As far as I remember - file would be populated during shutdown of Tomcat.
Besides the maven solution, you can also consider https://www.eclemma.org/jacoco/trunk/doc/cli.html
Basically, you start your service on the remote machine with the javaagent option like (you can change the port number and omit includes if you want to have coverage for all of the classes):
-javaagent:/tmp/jacocoagent.jar=port=36320,destfile=jacoco-it.exec,output=tcpserver,includes=a.b.c.d.*”
Then connect to the remote machine by providing remote host address or open a tunnel to the remote machine. The following example assumes I have set up a port forwarding between local host's 36320 and remote host's 36320
java -jar jacococli.jar dump --port 36320 --destfile /tmp/jacoco-it.exec
If you have multiple .exec files, you need to merge them:
java -jar jacococli.jar merge /tmp/jacoco-it-1.exec /tmp/jacoco-it-2.exec --destfile /tmp/merge
Then generate the html report (path1 can be a path to the jar file or the class files folder)
java -jar jacococli.jar report /tmp/jacoco-it.exec --classfiles path1 --sourcefiles path2 --html /tmp/report

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