Java Debugging



You can compile your program with certain compiler options that instruct the compiler to generate symbolic information in the object file. If you use the javac compiler to build your code, use the -g compiler option. This option lets you examine local, class instance, and static variables when debugging. You can still set breakpoints and step through your code if you do not compile your classes with the -g option. However, you will not be able to inspect variables.

How can we configure Eclipse to debug Java application?

  1. Click on the name of the project
  2. Click on Run -> Debug Configuration
  3. Click on Remote Debugging

See the weblogic-debug page for details.

How can we configure Eclipse to start debugging when we hit an exception?

This may be handy when we need to debug an application when an exception is thrown but stack trace is not logged in the log file. To configure Eclipse to start debugging when an exception is thrown:

  1. Switch to the Debug perspective.
  2. Click on the Breakpoints tab
  3. Click on the icon with the letter J and the exclamation mark
  4. Scroll down to find "Exception" from java.lang and click on it
  5. Click the OK button

How can we debug a command-line Java application?


-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y

to the command that you use to run your Java application. The suspend=y flag ensure that that application will not start running until Eclipse connect it on speicified debug port. Put the above string before the -jar option, and possibly before other options as well. You may have to change the port number mentioned above and change the port number in your Eclipse to get this to work. We must use the same port number in both places.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License