简体   繁体   中英

Debugging jar file with source code attached in eclipse

Please let me know how I can debug any jar file with source code attached in eclipse.

When I tried to debug it, implementation is completed as desired but control never reached the breakpoints.

  • Right-click on the jar referenced in your project, select 'properties'
  • From 'Java Source Attachment' click 'Workspace...' button
  • Select the 'src' folder from the jar project in your workspace

or if you want to do more steps:

  • Right-click on project, select 'properties'
  • From 'Jave Build Path' select 'Libraries' tab
  • Expand (click '+') the jar you want to link source to
  • Click 'Source attachment' (it should be saying 'Source attachment: (None)')
  • Press the 'Edit...' button
  • Follow from the second point in the shorter steps above

You try this one:

First open the jar file using JD(Java Decompiler), then click on File -> Save JAR Sources and it will become a .zip file containing the java source files. Then you can add this source zip file in the debug configuration using add source. It will work then for sure.

I also faced the similar issue,sometime while debugging the jar file classes. But after the above procedure the issue got resolved.

Use below command to launch your jar:

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y -jar [JAR NAME]

It should state something like :

Listening for transport dt_socket at address : 8000

In eclipse, create a " Remote Java Application " debug configuration and add the modules of jar.

i have done this many times and it works fine for me. Make sure you have put the debug points at right position. May be because of some condition code will become unreachable at that time debug point will not take effect. Place debug point at calling statement of method means where you have called the method of jar class. If control reaches there then press F5 to enter to the method in class of jar file. If control does not reach there that means you have some conditions in code which prevents calling that method and thus you can not get to your break point in method.

"The workaround is to go to PDE 'Target Platform' preference page and disable all the external plug-ins. They are causing the problem because the model synchronizer that should prevent the appearance of 'twins' is not working properly."

Reference link - https://bugs.eclipse.org/bugs/show_bug.cgi?id=22961 Comment #10.

The above workaround worked well for me.

You got this page, if you don't have source file.. 在此输入图像描述

Click "Attach source" button, you got this page..

在此输入图像描述

If you have source code in your workspace, you can directly select by this way..

在此输入图像描述

If you have source code in external location, select external folder and select src of your source code..

在此输入图像描述

If you added source correctly, you can debug those files..

Using Eclipse version Oxygen.2 (2017), the following procedure was successful for me.

  1. Launch the application to debug with the Xdebug and Xrunjdwp switches.

    cd "C:\\Program Files\\Test_742\\Test 7.4" test_app.exe -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8500

  2. Create a new workspace in Eclipse.

  3. Create a new Java project using File -> New -> Java Project.

  4. Copy the source code of the Jar to debug to the src folder of the new project. I found it necessary to ensure that I copied the complete folder hierarchy of the source code to my source folder.

    For me that meant copying \\com\\test... to my src folder.

    This puts all the source code in the build path.

  5. Copy any dependency JARs to the project. I created a new folder named myJARs.

  6. Refresh Package Explorer to see the new myJARs folder.

  7. Add the dependency JARs to the build path by opening File -> Properties -> Java Build Path -> Libraries (tab), and click Add JARs...

    Add each dependency JAR to the list, then click Apply and Close. This adds the JARs to Referenced Libraries.

  8. Configure a debug session. Click Run -> Debug Configurations -> Remote Java Application. I set Connection Type = Standard .

    Configure Host to point to the machine running the JAR and Port to the port specified in the address portion of the command line switches (8500 in my example).

  9. Notice that on the Source tab, both the source files and the dependency JARs are present.

  10. Click Apply, then Debug.

  11. View the debugging perspective by clicking Window -> Perspective -> Open Perspective -> Debug.

  12. Place breakpoints in the source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM