How to setup java remote debugging in eclipse

转载 2012年03月26日 13:38:05

How to remote debug java application in Eclipse IDE

Remote debugging is not a new concept and many of you are aware of this just for who don’t know what is remote debugging? It’s a way of debugging any process could be Java or C++ running on some other location from your development machine.  Since debugging is essential part of development and ability to debug your application not only saves time but also increase productivity. Local debugging is the best way in my opinion and should always be preferred over remote debugging but if local debugging is not possible and there is no way to debug your process then remote debugging is the solution.
Many of us work on a project which runs on Linux operating system and we do development mostly on Windows. Since I am working in Investment banking and finance domain I have seen use of Linux server for running electronic trading application quite a lot, which makes development difficult because you don't have code running on your development machine.

Some time we managed to run the project in windows itself which is essential for development and debugging purpose but many times its not possible due to various reason e.g. your project depends upon some of the platform dependent library or some Linux module whose windows version may not be available or your project is too big to run on windows and its heavily connected to upstream and downstream system its almost impossible to create same environment in your windows machine for development.
On such situation my approach to work is isolate the work I am doing and test that with the help of mock objects, Threads or by trying to run that module independently but this is also not a desired solution in some cases where you need to debug the project at run time to find out some subtle issues.

Remote debugging in Java with Eclipse

Eclipse provides us most useful feature called "Remote debugging" by using which you can debug your Linux running process from your windows machine. believe me this become absolutely necessary in some condition and knowing how to setup remote debugging and working of remote debugging in eclipse can greatly improve your productivity. In this Eclipse tutorial I will try to explain eclipse remote debugging or how to setup remote debugging in eclipse.

Now let's see how we can setup remote debugging in Eclipse:

1) First setup your java project in Eclipse.

2) Select your project, go to "Run" Menu option and select "Debug Configurations"
remote debugging eclipse
Remote debugging with eclipse 1

3) This will open Debug Configuration window select "Remote Java Application" icon on left side, Right click and say "New".
How to do  remote debugging in eclipse
eclipse remote debugging 2

4) After clicking on New, Eclipse will create Remote Java Application configuration for your selected project. Now next step is to setup host and port for remote debugging.
How to setup eclipse remote debugging
Remote debugging in Eclipse 3

5) Now put the host name and port on which your process is listening for debugger in Linux machine. Check the "Allow termination of remote VM" check box if you would like to close java application running on Linux from eclipse.

6) Now you are all set to remote debug your project. but before starting to debug make sure your java process is started with java debug settings and listening on same host and port, otherwise eclipse will not able to connect successfully.

7) To debug just click the "Debug" button in last screen where we have setup host and port.

8) You can also debug by going to "Debug Configurations" selecting your project in "Remote Java Application” and clicking on "DEBUG".

Java remote debug setting and JVM debug options

In order to remote debug a java application, that application must be started with following JVM debug options:

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y  suspend=y -jar stockTradingGUI.jar

This will start java applicaiton stockTradingGUI into debug mode using Java Debug Wire Protocol (jdwp) protocol and it will listen on port 8001
suspend=y will ensure that that application will not start running until eclipse connect it on speicified debug port.It also important to note
That application must be start before eclipse tries to connect it other wise Eclipse will throw error "Failed to connect to remote VM. Connection refused" or "Connection refused: connect" 

Enjoy remote debugging in Eclipse J

Tip: In JVM DEBUG parameters there is a parameter called "suspend" which takes value as "y" or "n". so if you want to debug the process from start set this parameter as  "suspend=y" and your Java application will wait until Eclipse remotely connect to it. Otherwise if you want to run your program and later want eclipse to be connected that set this as "suspend=n" so your java application will run normally and after eclipse remotely connected to it, it will stop on breakpoints.

Tip: Use start up script to put JVM debug parameter and use a variable e.g. isDebugEnabled and also REMOTE_DEBUG_PORT in the script and export this variable when you want to remote debug your Java application. This will be very handy and will require just one time setup work.

Tip: if you get error "Failed to connect to remote VM. Connection refused" or "Connection refused: connect" then there might be two possibility one your java program is not running on remote host and other you are giving incorrect port or host name after verifying these two things if issue still persists then try giving full name of the host.

Tip: You also need to ensure that you run the same codebase in eclipse which is deployed in your remote machine so that what you debug and see in eclipse is true and real. you also need to ensure that your code is compile with debug option "-g" so that eclipse can easily gather debug info e.g. information about local variable. by default java only generate  line numbers and source file information.with  debug option -g your class file size might be more because it would contain some debug information.

Note: Recently I have wrote another article 10 tips on debugging Java Program in eclipse which is a collection of my java debugging tips and explains some advanced java debugging concept like conditional break point, how to debug multi-threaded programs in Java, Step filtering to avoid debugging system classes in Java, logical view to see the content of collection classes like HashMap or Arraylist in Java.

If you like to learn some really useful Eclipse keyboard shortcuts you can check  here  top 20 useful Eclipse keyboard shortcuts

Read more:

用Eclipse远程调试(Remote debug)Tomcat

1. 配置Tomcat支持remote debugging给JVM加上如下的启动参数:-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server...
  • zmhinzaghi
  • zmhinzaghi
  • 2010-10-26 10:32:00
  • 13504

How to setup debugging target system for WinDbg

  • 2012年11月06日 11:13
  • 2KB
  • 下载

How to Enable USB Debugging Mode on Android

Android 2.0-2.3.xSettings > Applications > Development > USB Debugging Android 3.0- 4.1.xSettings >...
  • voyager3
  • voyager3
  • 2016-05-08 20:48:19
  • 844

Error: The Microsoft Visual Studio Remote Debugging Monitor on the remote computer is running as a d Error: The Microsoft Visual Studio ...
  • shuixin536
  • shuixin536
  • 2013-03-13 13:35:57
  • 2296

VS2013 建立 remote debugging 远程debug的方法

  • liuxingrui4p
  • liuxingrui4p
  • 2015-03-27 23:02:36
  • 1587

Administrator privileges required for ole remote procedure call debugging :this feature will not wor

Administrator privileges required for ole remote procedure call debugging :this feature will not wor...
  • koalacoco
  • koalacoco
  • 2013-12-05 14:38:27
  • 2683

Chrome Remote Debugging on Android

Remote debugging overview Remote debugging with the ADB Chrome extension 1. Install the ADB ext...
  • fengda2870
  • fengda2870
  • 2013-10-13 14:31:45
  • 3813

openstack windows下远程debug调试(komodo)

本文讲解openstack的两个调试工具:pycharm、komodo(以对openstack的nova api远程调试为例) 注:后续有时间再对eclipse进行讲解。 一:komodo(官方文...
  • tantexian
  • tantexian
  • 2014-07-30 11:12:00
  • 5014

安卓系统上的远程 JS 调试 Remote JavaScript Debugging on Android

每当在 Android 移动设备上调试网页时,开发人员往往都会不自觉陷入调试的泥潭中去。《Android开发指南》提供了一个解决方案,却有点繁琐复杂。因此,许多 Web 开发人员会倾向于使用类似 Fi...
  • zhangxin09
  • zhangxin09
  • 2011-12-15 10:24:22
  • 6344

Remote Debugging on Android with Chrome

Remote Debugging on Android with Chrome The way your web content behaves on mobile can be dramatic...
  • hhdsyxwei
  • hhdsyxwei
  • 2015-03-06 15:09:26
  • 792
您举报文章:How to setup java remote debugging in eclipse