[ACCEPTED]-How do you debug Java Applets?-applet
Aside from the obvious use of the Java console 49 and the applet viewer, starting from Java 48 6 update 7, you can use the VisualVM that comes 47 with the JDK (JDK_HOME/bin/visualvm). It 46 allows you to view the stack traces of each 45 thread and even view all object instances.
AppletViewer 44 is very handy, you can do a "Run as 43 / Java Applet" from Eclipse to run, or 42 "Debug As / Java Applet" to debug 41 your applet classes.
However, sometimes to 40 debug some security related stuff the browser 39 plugin environment is just too different 38 from appletviewer. Here's what you can do 37 to effectively debug applets in the browser:
1) Obtain debugging info for the binaries
Backup 36 the .jar files from JRE_HOME/lib
(Download 35 and) Install a JDK for the same version 34 as your JRE.
Copy the .jar files from JDK_HOME/jre/lib 33 to JRE_HOME/lib
The files inside the JDK 32 were compiled with the debugging information 31 included (source-code line number information, variable 30 names, etc) and the JRE files don't have 29 this information.
Without this you won't 28 be able to meaningfully step into core class 27 code in your debugger.
2) Enable debugging for the Java Plug-in
Go to the Java Control 26 Panel / Java / Java Runtime Settings 25 / View / User / Runtime Parameters
And 24 add the options to enable debugging. Something 23 like this:
-Djava.compiler=NONE -Xnoagent -Xdebug -Xrunjdwp:transport=dt_socket,address=2502,server=y,suspend=n
The interesting options are the 22 port (using 2502 here, you can use pretty 21 much any free port, just write it down for 20 later) and the suspend - if you need to 19 debug the applet startup, classloading, etc, set 18 this to "y". That way when you 17 access an applet page, the browser will 16 appear to freeze as the JVM immediately 15 gets suspended waiting for a debugger to 14 connect.
3) Use your favorite IDE to Remotely debug the Java Plug-in
In Eclipse, for instance, choose 13 Run / Debug Configurations ... / Remote 12 Java Application
Click on the "New" button.
Make 11 sure connection type is "Socket Attach", choose 10 localhost as the host if your browser is 9 local, and the port you chose earlier (2502 8 in the example).
You might have to inlude 7 the src.zip in your JDK on the sources tab 6 to have the Java core class sources available.
Save 5 the configuration, and once your browser 4 is running the plug-in (with the JVM suspended 3 or not) run the remote debugger to connect 2 to the plug-in JVM, with a project containing 1 your applet sources open.
This article is a bit old but is still relevant (including a section 30 entitled "How to Debug Applets in Java 29 Plug-in").
Edit: perhaps a better way 28 to get stacktraces is to use the Java plugin console. If you 27 hit "t" in that window, you'll 26 see the following:
Prints out all the existing 25 thread groups. The first group shown is 24 Group main. ac stands for active count; it is 23 the total number of active threads in 22 a thread group and its child thread groups. agc 21 stands for active group count; it is the 20 number of active child thread groups of 19 a thread group. pri stands for priority; it 18 is the priority of a thread group. Following Group 17 main, other thread groups will be shown 16 as Group , where name is the URL associated 15 with an applet. Individual listings of 14 threads will show the thread name, the 13 thread priority, alive if the thread is 12 alive or destroyed if the thread is in 11 the process of being destroyed, and daemon if 10 the thread is a daemon thread.
The other 9 command that I've used most often from that 8 console is the trace level from 0-5:
This 7 sets the trace-level options as described 6 in the next section, Tracing and Logging.
From that page, you'll 5 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 4 tools can all be fairly useful as you're 3 trying to unravel what in the world has 2 gotten into the head of your applets. I 1 know that they've worked for me.
The applet viewer supports debug options.
Stack traces from uncaught exceptions will 11 appear to the console. This can be enabled 10 from the Java Control Panel (Advanced > Java 9 console > Show console) or some browsers 8 have various options or plugins for enabling 7 it.
You can attach a debugger to the running 6 PlugIn process.
Perhaps the best way is not 5 to debug at all. Write tests. Write code 4 that doesn't couple to unnecessary assumptions 3 - for instance that you are running as an 2 applet. Unfortunately most GUI/applet example 1 code is written very badly.
I faced issue doing remote applet debugging, every 8 time while trying to connect from eclipse, its 7 throws Connection refused error, my jre 6 version was 64 bit and eclipse 32-bit, when 5 I did replace with 32-bit jre, it worked 4 for me. Also if we have install both 32-bit 3 & 64-bit jre versions, IE by default 2 uses 64-bit jre for applets, chrome and 1 FF may use 32-bit jre versions.
Uncaught exceptions are sent to the console. You 12 can also use System.out to write your own 11 messages to the console. To view the results 10 you'll need to open the console by right 9 clicking the Java icon in the systray and 8 opening the console (note this is different 7 for Microsoft's VM).
To debug applets properly, you 6 can setup Eclipse to debug applets. Right 5 click the applet source file and click Debug 4 as Applet. (If you have parameters for the 3 applet you'll need to set this up.) Then 2 you can step through the applet code as 1 you would debug any other Java code.
For me the most important action to get 11 applet debugging in eclipse, is to set in 10 java control panel(tab java) the right binaries 9 to use, that have debug symbols. Only JRE 8 included in jdk have this symbols. So to 7 debug applet in java control panel, tab 6 java, press view button, after find the 5 correct jre under jdk folder, for me for 4 example "C:\Programmi\Java\jdk1.7.0_03\jre", and 3 put check enabled only for this entry. This 2 is for me the clean way to do what Sami 1 Koivu say.
More Related questions