简体   繁体   中英

Is it possible to debug an already running Java program in Eclipse?

What I'm asking is if it's possible to do the following steps in order:

  1. Run as... Java application
  2. Let the program run until it reaches the point I want to debug
  3. Debug that program when it gets to that point.

The reason why I launch the program using "Run as..." and not "Debug as..." is because the program hangs (I suspect an infinite loop) at some point, but I'm not able to replicate it consistently. What I mean is that I can make it hang consistently, but it happens during random occasions.

For what it's worth, my program is a game and it hangs at different points in the game, which is why I can't be sure where to put a breakpoint, so it would be great if i could let it run first and then switch to debug at the point where it hangs/loops.

Why don't you just execute your program with "Debug as..." without setting any breakpoint. Then, when it hangs, switch to Eclipse's debug perspective and suspend your program with the double vertical bars button?

If you always run it with "Debug as..." you can pause execution in the event that the program hangs. This will open the debugger and allow you to inspect the threads that are running.

Apart from the solutions suggesting not to set any breakpoints, you could also look into attaching the java debugger to a running program.

There is basic explanation of how to it at InformIT .

A fast google search yielded this tutorial on how to do it with eclipse.

If I remember correctly you have to start your program with additional parameters to allow attaching - this might prove a problem if your program is completely frozen, as this might prevent the attaching from working: check this stackoverflow question for even more information.

This will actually start your application in debug mode, but you can postpone attaching a debugger until you really need it.

You can use 'Remote Java Application' functionality in eclipse to attach to a Java virtual machine. You need to add additional parameters when starting your application:

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

You can reed more here: http://blog.javachap.com/index.php/debugging-in-java/

Maybe you can add some debug logging to pinpoint the place where it hangs.

Best way to log it into file to have it in hisory. System.out.println("before some code"); some code that may be problematic System.out.println("after some code");

When you will se that "after some code" is not reached you know it is hanging before this part. Or if you use log4j do it with log4j some debug info with time stamps, then you can go closer and put more debug information closer to place where it hangs, and eventualy you will know where to put breakpoint and step trough the code.

'Run As' never picks up breakpoints and hence it will not stop at the place where you want to debug and so the answer to your question is a No. But the point no 2 in your question contradicts your statement in the paragraph below in which you explain why you chose not to use 'debug as'. If you do not know where the program hangs how can you let the program run until it reaches the point you want to debug? (point no 2).

I would debug it using 'debug as' as @Nicola Musatti has pointed

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