How to log the execution of program from start to finish? - java

I want to see what lines of code are being executed in my Java program when it runs and terminates. One way I can do this is by using an IDE such as IntelliJ or Eclipse and setting a break point in the main method, then going through each line of code one after another using the step-over and step-into buttons. This becomes extremely tedious to do after a while though, because the program freezes every time I want to know what line of code is executed next.
Is there another way to see what's happening in my Java program, other than using an IDE debugger and setting breakpoints? I would also like to be able to use my program like a normal user would be able to (i.e. not being paused every few seconds because of my IDE).

Related

IDE hangs in debug mode on break point in java fx application

I have a problem while debugging in IntelliJ IDEA, it hangs in debug mode on break point in listeners in javafx application. I tried to increase heap space, but it's not help. Maybe someone had such problem too, please, suggest me what to do.
Set this as a VM parameter:
-Dsun.awt.disablegrab=true
It will mess up drag-and-drop, and leave an artifact on your screen while the debugger is paused - but it will let you debug. It happens whenever you block the JavaFX thread.
This can happen for a simple reason: The application has a lock on the desktop, for example a modal dialog or a popup or an open menu. Then it stops in a breakpoint. This notifies the IDE. Now the IDE tries to do something on your desktop but can't since the application still has a lock on the whole desktop -> deadlock.
You can use a tool like Chronon which records the whole program and lets you move back and forth on the timeline.
The last option is logging or poor man's debugger (System.out) instead.
[EDIT]
it's hard to check with System.out which of 20 parameters not equal.
It's actually pretty easy:
System.out.println("check");
if(!a1.equals(b2)) System.out.println(a1+"!="+b1);
Then duplicate the last line. That way, you will only get output when something is actually interesting (and not for the 19 equal parameters). Add some patterns to the output if you can't distinguish aX from aY (i.e. both are true):
if(!a1.equals(b2)) System.out.println("a1:"+a1+"!="+b1);

Check thread eclipse plugin

Is there a tool I can use to see what parts of the code are in which thread (colour code it or something)? I'm mostly concerned about EDT, but writing that isOnEventDispatchThread()-ish command every other line and then keeping track of it's output while the program is running is tedious and time consuming.
The major problem I'd see with this is code that can be called from any thread, such as constructors. You can however use Eclipse's debugger at runtime. Simply run the program in the debugger until a breakpoint or with manual pause then look at the "debug" view that is generally in the upper left. You'll see threads and may select one to jump to its location.

MATLAB hangs when I try to use the java package jdde, but only for the first time after a system reboot

I'm using the external java package jdde in MATLAB. Please note that for the following example, the DLL file that comes with the package needs to be on the MATLAB librarypath. The method to do this is different depending on your MATLAB Version.
Using jdde in MATLAB works fine, except for the first time after I reboot the computer or I logoff/logon in Windows. When I run the following code for the first time after a computer reboot, MATLAB will stay in busy mode forever (with 0% CPU). When this happens, I kill the MATLAB process in the task manager and restart MATLAB. When I run the same code again, it will execute instantly (not staying busy forever).
javaaddpath('C:\pretty-tools-JDDE-1.0.2.jar')
a = com.pretty_tools.dde.client.DDEClientConversation;
a.connect('','');
To sum it up, the above code will cause MATLAB to stay busy forever the first time I run it after a system reboot or user logoff/logon. When I run it again after killing the MATLAB process, it will work perfectly fine (not hanging up MATLAB).
I have seen this behavior on different computers, and in different Versions of MATLAB (2010 and 2012). I'm using Windows 7 x64.
In the code example, the a.connect command is the one that causes MATLAB to stay busy forever. Putting this command in a try/catch block would not help, because the a.connect doesn't cause an error, it just never does continue.
I'm not sure if this problem is caused by MATLAB or by the java package.
Any ideas how to get rid of this behavior would be much appreciated.
Note: The input argument of a.connect does not matter, it will always hang, so I just gave '' as input in this example.
Code hangs without any know reason in DdeInitialize() method. New build JDDE-2.0.3 contains workaround for this problem.
Try running the add path command on its own so that there is a second or two before it tries to execute code dependant on the jar. I have found this to often be the problem with intermittent issues having to do with jars in Matlab
Switch over to classic mode initially so that u will get rid of that.

Strange behavior re-runing java application in a loop

Unix daemon runs a script a loop, the script calls a java program: java {java_args} myClas.jar
The java program is heavy program with multiple threads.
The problem is very strange: First execution works as expected.
But the second execution is stuck some where and I can't find a reason (very hard to debug this).
Is there a possibility that when first execution is finished there are still not-cleaned resources or threads left from this execution?
If yes, is it possible to clean and kill everything right after process completes?
If by resources, you mean threads, then no. When the VM shuts down, everything on the heap, all threads, objects and monitors are disposed of. However if you're depending on the existence/absence of a file for locking or something similar, a deadlock is possible. Also, is it possible that the first process is still running when you launch the second one?
If your java process is stuck on the second run, you can attach jvisualvm to it and should be able to figure out where it's stuck.

how to get the last executed method in code that doesn't terminate?

I'm modifying a rather big codebase and I'm in the process of debugging. My code doesn't terminate, so I don't get a stacktrace. I tried to isolate the trouble code with breakpoints, but unfortunately it runs through sections that are executed all the time (transaction manager), so I'm clicking to death. Furtermore, i get the impression that the code breaks only under certain conditions, but runs fine most of the time.
Is there a way in Intellij to see the last method that was / the current method that is executed?
thanks
You can use BTrace, a tracing tool, to print out the name of every method entered in your program. There is a sample script that comes with BTrace, called AllMethods, which does just that.
All of you have to do is start your java process and then run the BTrace script against the JVM's PID. It will print out every method entered. You can restrict it to certain packages if you like. For more information about using BTrace check out this tutorial.
Use a profiler. It will give you a breakdown of which methods are executing, how much time your program spends in each method etc etc. It should be quite easy to find out from there where your program is spending its time.
JProfiler is an example. I think it has a trial / demo version which is fully functional.

Categories