Ecplise View flow of running program - java

I'm currently writing a pretty large program that calls the same methods from different places.
Now I would really like to see how the program goes from one method to another as it is running. Like a live view that shows when what method is opened (and why?). Call Hierarchy doesn't suit my needs at this point. Is there a way?

One way to follow the logic of your application is by placing breakpoints at the line of code you want your application to stop at but, to do this you'll have to setup it up in debug mode.
Every major IDE will let you do this, including Eclipse.
Have a look at this tutorial:
Java Debugging with Eclipse
Once you setup your program in debug mode you can add a breakpoint in the gutter next to the line numbers.

Related

How to track the methods used in a Java application while running

I am working on understanding a project which was already created by someone else. It did not followed a proper coding method (no comments and so on). I'm finding it difficult to find where a method is used during the use of the app .So ideally I want to track how each method is used and when it's called while manipulating the applciation . I've already searched the problem here and nothing worked for me. So can anyone please help me out.
I am using Intellij btw .
Thank you .
You can use the debugger of Intellij, that's a really cool tool and besides your problem, it can be used to find bugs in your code.
The basic and easiest way to use it is:
Add breakpoints on lines where you want the application to stop just before executing that line. You can add as many breakpoints as you want and in as many files you want. To add a breakpoint, just click on the empty space beside the line numbering on the left side of the editor window.
then turn on the debugger from the run menu or Shift + F9.
then use the step over option or F8 to jump to the next breakpoint from the current location.
In your case, you can add a breakpoint in the method for which you wanna test, and then see if the program approaches that particular breakpoint during execution.
You can read the more detailed documentation on debugging from this link: Complete debugging guide: Jetbrains.
Hope this helps to solve your issue.
use Find Usages for the method to see where it is used
use a method call hierarchy to see which methods invoke this method
use method breakpoint to stop in debugger any time the method is invoked

How can I make it so Eclipse automatically updates my code in a window as I edit it?

How can I make it so Eclipse automatically updates my code in a window as I edit it? I've seen the feature before in youtube videos but I cannot find it. For example : I change a JApplet rectangle width from 20 to 10, I want to see it update immediately.
I've seen Notch do this on development videos (Minecraft), it is awesome but I don't know exactly how he does it.
-- EDIT --
This has been bugging me so I went and googled "how does notch code" and found this on a blog page https://gun.io/blog/what-i-learned-from-watching-notch-code/. It doesn't say exactly how it was done but gives a good hint (HotSwap) and makes it seem like he set it up himself without external software. Here's the most relevant section:
Incredibly Fast Testing
He began by building the engine, and to do this he used the ‘HotSwap’ functionality of the Java JVM 1.4.2, which continuously updates the running code when it detects that a class has changed.
When building the engine, Notch wrote a function which would continuously pan the camera around and clip through the walls and keep the view on top, so he could make changes to the code and see the effects they made in real time. I’m used to testing by writing a function, building it, installing it on the device I’m testing on, and then seeing the result, which can take up to a minute at a time, so it’s easy to see how HotSwapping could save a lot of development time.
--- ORIGINAL POST CONTINUED ---
I get a similar effect by using groovysh though, works smoothly and can use all your java classes as is.
What I'll usually do is write all my code in java, then go and fire up "Groovysh" where it will give you a little window to enter commands (You may have to ensure the classpath works correctly outside of eclipse). I can then "new" any of my classes and call methods on them one line at a time. When you do myFrame.setSize([100,100]) you will see it change immediately.
A good test is to just run groovysh and type something like:
import javax.swing.*
f=new JFrame()
f.setVisible(true)
f.setSize(100,100)
or the groovier version:
f=new JFrame(visible:true, size:[100,100])
and you will see your frame resize on the screen. You can even drag it bigger and then do something like:
println f.getWidth()
to show your new width. It's fun to interact this way but it's more complicated if you want to actually change your class definition and see it pick up the change, I have no idea how Notch did that. I looked into it a little--it's possible he was using something like JRebel
It requires something special since you would have to dynamically reload the classfile into your running system on every save--something that should have serious classloader issues.
By the way there is also a way to get your Java program to throw out a little GroovyConsole which will allow you to inspect and modify all the variables in your running code (but again you can't replace definitions of existing classes).
Also see answer here:
Change a method at runtime via a hot swap mechanism

How on earth does he debug a running application like this, and more importantly, how can I?

"Debugception!"
You may notice that within the first 15 seconds of this YouTube video (from 1:01:01 to 1:01:16), Markus Persson (aka "Notch", creator of Minecraft) has somehow managed to save/update an application and attach a debugger to it while it was already under the process of being debugged, supposedly all with a simple keyboard shortcut. The previously coded application somehow magically became the newly edited one, and seemingly without relaunching it or spawning a new process... It's possible that this is just some form of locally remote debugging, but something about it just doesn't seem quite right.
I've spent several days Googling and asking around on how he was able to do this, yet to no avail. I've found no such option under Eclipse preferences, and whenever I try to save & debug an already running application, it simply launches a separate instance of the newly updated application, side-by-side with the older, outdated one.
Am I missing something? How was this possible?
How was he able to utilize such an astounding, powerful debugging feature?
Thanks in advance!
Update
Okay, so this appears to be a standard feature specific to Eclipse.
Coming from a background in NetBeans and Visual Studio, I'm astounded that this doesn't seem to exist elsewhere (or at least in NetBeans!)...
This is a built-in feature of Eclipse. If you edit a method while the program is running in debug mode, it will compile the new method, and replace the old method with the new version. If some thread was already running that method, it will jump back to the beginning (AFAIK; this might only happen when the program is paused).
You don't need to re-launch the program or set any special preferences. Just edit and save, and the magic will happen.
Eclipse can't always figure out how to merge your changes into the running program - usually if you changed anything outside a method body (including the method's parameters or return type). In this case, you will get a warning dialog, with the option to stop the program, restart the program or ignore the changes.

How do I know which method/class is called when I test java GUI in eclipse?

I am a beginner using eclipse for java programming. Recently I downloaded certain source code online and ran it in eclipse successfully. I want to learn how it runs. However, I failed to find a way to monitor the progress during the program running. For example, if I run the application and click certain button in the application GUI, how do I know which class/method is called? In other words, how can I use eclipse to monitor the process of program running?
The general answer is to use the debugger. For example, set a breakpoint in some method and then use "Debug As" instead of "Run As" to run the application within Eclipse.
Here are a couple of tutorials / articles on using the Eclipse Debugger.
http://www.vogella.com/articles/EclipseDebugging/
http://agile.csc.ncsu.edu/SEMaterials/tutorials/eclipse-debugger/
http://www.ibm.com/developerworks/library/os-ecbug/
Put a breakpoint(double click the left side of the line) on your method to be debugged and debug your program.
Simply put breakpoints in the code lines you need to look and run the application in the Debug mode then it'll wait at the relevant code lines you need to check.

Debugging with JUNIT4: eclipse "stops" at breakpoints but actually keeps running

I'm using Eclipse Juno to debug a java class while testing with junit, but the debugger has been acting flakey. I will reach the breakpoint I want and all seems well with the variable values, but in my console, all of the print statements I set up later in the code are popping out as if I were stepping through it!
(note: they aren't printing as fast as they normally would if I had simply run it, and the output itself is a bit garbled compared to normal output).
Somehow, the code is executing through without me pressing a button. If I try to step through, my view of the code and variables is correct, but debugging stops once Eclipse thinks the program has finished.
Something I should probably mention is that I recently put Fedora on my laptop, so I haven't been running eclipse on it for very long and this is my first time trying junit. Is is possible that I need to download something for my debugger?

Categories

Resources