Can i change things while debug mode is running in Eclipse? - java

so as the title states, can i change things while debug mode is running an application in Eclipse? You know like colors or stuff like that, I've seen Notch (Creator of Minecraft) do this thing when he was making "Escape" in 48 hours. I think that if I can do that then is more easy for me to change things like, moving buttons in the main menu, changing backgrounds or at least text colors.
PS: I'm using Slick2D
Thanks and have a nice day.

If I understand your question correctly, you can do the following:
Window->Open perspective->Debug
Add a breakpoint somewhere in your code where applicable
Window->Show view->Variables
Run->Debug
When the debugging pauses because of your breakpoint, go to the variables panel and change whatever value has been assigned so far (in the "Values" column).
Run->Resume (or Step Over, or Step Into) to continue debugging
The program will resume with your new value assigned to the variable

Related

Status Monitor in Eclipse?

I am pretty new at developing larger software and I am using Eclipse as IDE.
When my program is running, I want to have certain informations about the status in different classes (like values of certain objects). Up until now I was just printing it all out on the console with System.out.println(object.value);.
After a while the console became confusing with all the different values printed higgledy-piggledy. Now I am searching for a plugin or something, with which I can do something like
StatusMonitor monitorSize = new StatusMonitor();
StatusMonitor monitorHeight = new StatusMonitor();
monitorSize.print(object.size);
monitorHeight.print(object.height);
And then Eclipse has two different terminals/windows where the specific variables are printed.
Is there a possibility to achieve that?
YOU SHOULD DEBUG IT. FOR THIS ADD BREAKPOINTS IN THE CODE(To define a breakpoint in your source code, right-click in the left margin in the Java editor and select Toggle Breakpoint. Alternatively you can double-click on this position.) WHERE YOU WANT TO CHECK THE VALUES OF VARIABLES. WHEN YOU HAVE PUT THE BREAKPOINTS IN YOUR CODE THEN RIGHT CLICK ON THE CLASS WITH MAIN METHOD THEN SELECT --> DEBUG AS--> JAVA APPLICATION. THEN A DIALOG BOX WLL OPEN CLICK YES ON IT AND NOW YOUR CODE WILL BE OPEN IN DEBUG MODE. ON THE TOP PANEL .There will be options such as STEP INTO ETC. ALSO THERE WILL ARE FEW SHORTCUTS:
F5-->Executes the currently selected line and goes to the next line in your program. If the selected line is a method call the debugger steps into the associated code.
F6-->F6 steps over the call, i.e. it executes a method without stepping into it in the debugger.
F7-->F7 steps out to the caller of the currently executed method. This finishes the execution of the current method and returns to the caller of this method.
F8-->F8 tells the Eclipse debugger to resume the execution of the program code until is reaches the next breakpoint or watchpoint.

How can I check if an object has been created/called upon in Java? (Eclipse IDE)

Suppose I was making a game and within the render method which cycles hundreds of times per second I have two lines of code e.g.
Sprite sprite = new Sprite (...);
screen.renderSprite(...);
Which creates an object and renders it onto the screen so it shows. But suppose it didn't show; it would be natural to assume that if there is no error then it just wasn't called.
So in Eclipse how I can check if these two lines of code have been called in line x? If you tell me to use breakpoints tell me step by step because I feel that would only obfuscate the render method. (?) I want to check if the object has been created and utilized.
If you need more information don't hesitate to ask.
11. Sprite sprite = new Sprite (...);
12. screen.renderSprite(...);
Right click on line 12 (in my example but will be different in your code.)
Click on Toggle breakpoint
Click Run > Debug As an then either selct Java Application or an existing launch configuration
Once the code starts and enters screen.renderSprite, the program will halt and you'll be able to check it.
First of all you will have to identify some lines in your code at which you know that your sprite wasn't rendered (maybe a state flag?).
From that you have two options:
If you maintain the source code of method #renderSprite(...) you should consider using a logger (like apache logging which should be already included as a plugin of your Eclipse IDE). Thus your application wouldn't obfuscate your rendering processes.
The other option would be debugging: Set up a breakpoint (double-click left of said lines) and run your application in debug mode. You can also set conditions for the breakpoint to stop only in certain cases. Therefor you'll have to right-click a breakpoint and choose "Breakpoint Properties..." and check the "conditional"-box. In the textfield below you can define your condition in Java (you'll have access to all variables just like in your Editor). For more information see the debug help page of Eclipse or Lars Vogel's tutorial on debugging. By debugging your rendering would be paused, but it should be easier for you to find the concrete source of your problem.
A line with a breakpoint set, whose bytecode has been executed, will have a little checkmark on the breakpoint image.

Is there a way to see the program counter in NetBeans debugger?

Is there a way to see the line/method that's currently executing in the NetBeans debugger, without doing step-by-step execution?
I know I could set breakpoints, but I'm curious if there's a way to do it without breakpoints.
VisualVM, which comes bundled with java JDK, can show you live trackable method executions.
From the right border of the code editor, you should see a lot of colored short lines (which indicate warnings, errors, searches etc), among them there is an line with an small green box in the middle of the line, click it and you will locate the program counter.
The line is small so not easy to click.
In most IDEs, there is a tool bar button and menu to locate current executing line.
#Leon is showing how to locate the current position of the caret, which does not necessarily coincide with the program counter.
In the right border of the code editor, there are, indeed, small colored lines (which indicate lines where there are warnings, errors, breakpoints, etc.), among which is one line with a small box in its middle. But it shows the line where the caret happens to be located.
This is not necessarily the location of the program counter because the caret position may have changed since reaching the last breakpoint or other line of code.
The program counter being represented by a (too-small) green block arrow in the left margin of the code editor, where a breakpoint symbol and others can further obfuscate it, no amount of enlarging the editor window contents will make it significantly easier to find the program counter.
Of course, the program counter will change to where it will be visible by stepping out of, into, or over whatever's at the current program counter line. This isn't ideal. And there's a better way.
Alt+Shift+9 will open the Debugging window (below), which will show a number that represents the line number of the program counter.
Moreover, an expanded + dropdown indicator will show a line containing the same pertinent info, but double-clicking this line will open the window containing the program counter and clearly enough show it.
At least that's my experience so far.

Mouse press and release outside of window

I have two booleans: leftPressed and rightPressed. I need them to be set true when their respective mouse buttons are pressed and false when they are not. The location of the mouse and whether or not the program has focus should not matter. This program will not have a GUI. Is this even possible?
Definitely possible, very time consuming though. Use JNA to create a global mousehook. You would have to provide implementations for each platform you planned for it to run on, etc.. For example, here's someone who did it with windows.
I was looking for something similar at one point but found a better way to go about it in my code. However, I did run into this library. It may work out for you.

Debugging in Eclipse (Java); can't hover over a variable when breaking to view the value

I'm using Eclipse to code Java (for Android) and I'm trying to debug the code as I normally do (i normally do C# though).
From what I can tell, debugging in Eclipse is really bad. I don't know if I'm doing something wrong, but it seems to be just awful.
This is the code that is being run, I get some sort of exception and I want to see what the Exception is, by breaking in the "catch"-clause and viewing the variable "e":
try
{
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo.State state = mobNetInfo.getState();
}
catch(Exception e)
{
Log.v("MyAp", e.toString()); // I break on this line, and want to see what e is
}
The problem is that I cannot see "e" by holding my mouse over it (as I can in my beloved Visual Studio).
Now, I know that I can bring up the tab "Variables" which lists all variables accessible from when I'm breaking, but that's not optimal.
Just so I know - Eclipse has no way of "hovering" over the variable you are interested in, as in Visual Studio?
Edit
Thanks for the answers. However, still have the same problem =(
Go to Preferences-> Java-> Editor-> Hovers and tick the box 'Variable Values'. If you want Hover to happen automatically then leave the Key Modifier section blank.
There seem to be bugs in Eclipse related to this:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=317045
https://bugs.eclipse.org/bugs/show_bug.cgi?id=330061
https://bugs.eclipse.org/bugs/show_bug.cgi?id=286237
So, yes it is supposed to work, but it doesn't always. I'm having this issues on some classes. It seems to be consistent which classes work and which don't, probably related to what jar the class is coming from etc, but I have no real clue as to what causes it. Would love to see some reproduction scenario.
You can also highlight the variable, right click, and choose to watch it. Then it shows up on the watch tab. You can also set automatic break points that trigger on the exception being thrown rather than at a particular line of code. There is not 'hover' option to view the variable as in Visual Studio in recent versions of Eclipse.
This might seem a bit strange, but do you know that the code window has focus? I have observed that sometimes the appearance of the tooltip depends on whether a view has focus or not (which I guess is an SWT bug).
I have also found that in the "Debug" view (the tab with the little green bug as an icon) where it shows all the threads, Eclipse sometimes doesn't show the variable's values if the currently breakpointed thread isn't selected.
If anyone else runs across this problem, then you might have hit a breakpoint, but the currently running thread isn't the one highlighted in the "Debug" window.
As far as I can tell, you have to be in Debug view in order to hover over objects and see their properties. At least that's how my copy of Eclipse 3.4 works.
Of course, you also need to be in Debug view to have the Step Into, Step Over, Continues, etc... buttons...
Go to Preferences -> Java -> Editor -> Hovers, and click Restore Defaults.
Found it, Goto: Preferences > Java > Editor > Hovers - Javadoc (click on it and remove the ctrl+shift keys from there), then it should work normally.
I think that the correct answer is the #RogerThomas one.
Go to 'Window' -> 'Preferences' -> 'Java' -> 'Editor' -> 'Hovers'
Then check 'Variable Values'.
If the 'Variable Values' option collides with 'Combined Hover', uncheck last one or configure new keys for it.

Categories