How do you debug Java Applets?

JavaDebuggingApplet

Java Problem Overview


Currently, the only information I have is a one-line error message in the browser's status-bar.

Do you know how I could get a stack-trace for example ?

Java Solutions


Solution 1 - Java

Aside from the obvious use of the Java console and the applet viewer, starting from Java 6 update 7, you can use the VisualVM that comes with the JDK (JDK_HOME/bin/visualvm). It allows you to view the stack traces of each thread and even view all object instances.

AppletViewer is very handy, you can do a "Run as / Java Applet" from Eclipse to run, or "Debug As / Java Applet" to debug your applet classes.

However, sometimes to debug some security related stuff the browser plugin environment is just too different from appletviewer. Here's what you can do to effectively debug applets in the browser:

1) Obtain debugging info for the binaries

Backup the .jar files from JRE_HOME/lib

(Download and) Install a JDK for the same version as your JRE.

Copy the .jar files from JDK_HOME/jre/lib to JRE_HOME/lib

The files inside the JDK were compiled with the debugging information included (source-code line number information, variable names, etc) and the JRE files don't have this information.

Without this you won't be able to meaningfully step into core class code in your debugger.

2) Enable debugging for the Java Plug-in

Go to the Java Control Panel / Java / Java Runtime Settings / View / User / Runtime Parameters

And add the options to enable debugging. Something like this:

-Djava.compiler=NONE -Xnoagent -Xdebug -Xrunjdwp:transport=dt_socket,address=2502,server=y,suspend=n

The interesting options are the port (using 2502 here, you can use pretty much any free port, just write it down for later) and the suspend - if you need to debug the applet startup, classloading, etc, set this to "y". That way when you access an applet page, the browser will appear to freeze as the JVM immediately gets suspended waiting for a debugger to connect.

3) Use your favorite IDE to Remotely debug the Java Plug-in

In Eclipse, for instance, choose Run / Debug Configurations ... / Remote Java Application

Click on the "New" button.

Make sure connection type is "Socket Attach", choose localhost as the host if your browser is local, and the port you chose earlier (2502 in the example).

You might have to inlude the src.zip in your JDK on the sources tab to have the Java core class sources available.

Save the configuration, and once your browser is running the plug-in (with the JVM suspended or not) run the remote debugger to connect to the plug-in JVM, with a project containing your applet sources open.

Solution 2 - Java

This article is a bit old but is still relevant (including a section entitled "How to Debug Applets in Java Plug-in").

Edit: perhaps a better way to get stacktraces is to use the Java plugin console. If you hit "t" in that window, you'll see the following:

> Prints out all the existing thread > groups. The first group shown is Group > main. ac stands for active count; it > is the total number of active threads > in a thread group and its child thread > groups. agc stands for active group > count; it is the number of active > child thread groups of a thread group. > pri stands for priority; it is the > priority of a thread group. Following > Group main, other thread groups will > be shown as Group , where name > is the URL associated with an applet. > Individual listings of threads will > show the thread name, the thread > priority, alive if the thread is alive > or destroyed if the thread is in the > process of being destroyed, and daemon > if the thread is a daemon thread.

The other command that I've used most often from that console is the trace level from 0-5:

> This sets the trace-level options as described in the next section, Tracing and Logging.

From that page, you'll see that the levels look like this:

> * 0 — off > * 1 — basic > * 2 — network, cache, and basic > * 3 — security, network and basic > * 4 — extension, security, network and basic > * 5 — LiveConnect, extension, security, network, temp, and basic

These tools can all be fairly useful as you're trying to unravel what in the world has gotten into the head of your applets. I know that they've worked for me.

Solution 3 - Java

The applet viewer supports debug options.

Solution 4 - Java

Stack traces from uncaught exceptions will appear to the console. This can be enabled from the Java Control Panel (Advanced > Java console > Show console) or some browsers have various options or plugins for enabling it.

You can attach a debugger to the running PlugIn process.

Perhaps the best way is not to debug at all. Write tests. Write code that doesn't couple to unnecessary assumptions - for instance that you are running as an applet. Unfortunately most GUI/applet example code is written very badly.

Solution 5 - Java

I faced issue doing remote applet debugging, every time while trying to connect from eclipse, its throws Connection refused error, my jre version was 64 bit and eclipse 32-bit, when I did replace with 32-bit jre, it worked for me. Also if we have install both 32-bit & 64-bit jre versions, IE by default uses 64-bit jre for applets, chrome and FF may use 32-bit jre versions.

Solution 6 - Java

Uncaught exceptions are sent to the console. You can also use System.out to write your own messages to the console. To view the results you'll need to open the console by right clicking the Java icon in the systray and opening the console (note this is different for Microsoft's VM).

To debug applets properly, you can setup Eclipse to debug applets. Right click the applet source file and click Debug as Applet. (If you have parameters for the applet you'll need to set this up.) Then you can step through the applet code as you would debug any other Java code.

Solution 7 - Java

For me the most important action to get applet debugging in eclipse, is to set in java control panel(tab java) the right binaries to use, that have debug symbols. Only JRE included in jdk have this symbols. So to debug applet in java control panel, tab java, press view button, after find the correct jre under jdk folder, for me for example "C:\Programmi\Java\jdk1.7.0_03\jre", and put check enabled only for this entry. This is for me the clean way to do what Sami Koivu say.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzimbatmView Question on Stackoverflow
Solution 1 - JavaSami KoivuView Answer on Stackoverflow
Solution 2 - JavaBob CrossView Answer on Stackoverflow
Solution 3 - JavaBrian AgnewView Answer on Stackoverflow
Solution 4 - JavaTom Hawtin - tacklineView Answer on Stackoverflow
Solution 5 - JavaranjeetcaoView Answer on Stackoverflow
Solution 6 - JavaPoolView Answer on Stackoverflow
Solution 7 - JavascotchwiskyView Answer on Stackoverflow