How do you debug Java Applets? - java

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 ?

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.

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.

The applet viewer supports debug options.

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.

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.

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.

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.

Related

Netbeans RCP application not working outside the IDE when built as OS independent zip

I am building a statistical analysis application on Netbeans RCP for solving tests faster and with less effort https://github.com/PaulMaxAvalosAguilar/Statistikos-Klubas.
The thing is very simple:
1.-There's a module called TrabajosViewer which uses the nodes API to display workspaces where you can organize your samples with some meaningful name, first you create a workspace and then you add some data to it.
2.- You open editor top component which calculates some descriptive satistics stuff for all the sample you entered
3.- All stuff you need for a test is done! Samples are stored in an embedded H2 database(datos module) and results are calculated each time you click on a TrabajosNode.
However when I was testing the app I had to add the following sample:
https://1drv.ms/t/s!AkZmosJJMvdIu3c_IiVkD6JAVVgk (3344 elements); as the app had multithreading capabilities everything was fine, except that after building a release as OS independent zip the app froze from the swing Top Component which was very rare as that wasn't happening on inside Netbeans IDE. I think it has something to see with the build script since I built a Gradle version an everything worked fine https://github.com/PaulMaxAvalosAguilar/Statistikos-Klubas2
If you have an app - any Java app really, not just NetBeans RCP - and it freezes then what you want to do is obtain a thread dump. The thread dump will almost always give the clue as to why there's a freeze.
Java has 5 or 6 different ways to obtain a thread dump for a running application (just google it). Out of these methods, the one preferred is the so-called Ctrl-Break method because it is the one which gives most information. The downside of the method is that you need to have started your application from a console window. But if you can consistently replicate the freeze, then I don't think that's a problem for you. Here's what you would do if you are on Windows:
Start cmd.exe
From the command window, start your application, e.g. bin\sillyapp64.exe.
Wait for the freeze to happen on your application
Now press Ctrl-Break in the command window. This will give you a thread dump printed into your command window.
Upload the thread dump as part of your question. (or host it somewhere if it is too big)
Instructions for Linux/MacOSX are similar albeit in step 4 you would instead send your process a QUIT signal using the kill command.

Cannot get Java Applets to work in any browser

We are trying to download something from GE that uses Java to download when logging into the site. This is a windows 7 Professional PC. I have other computers that are able to do this successfully. Here is my order of operations:
Log into the site and select the file I want to download
Click download
It takes me to the page that says it will start in a few seconds but nothing happens. It is supposed to have a box that asks for Java to run.
I have reinstalled Java fresh and still nothing. Tried with multiple user accounts. Added the site to the list of exceptions in the firewalls and Java configuration. I have tried an earlier version of Java. This happens in Mozilla, Chrome and IE. I have made sure that the Java plugin shows up and is enabled. I just cant think of what I am missing. And since we are a contractor GE is not going to help us. Can anyone here think of anything?
Are you sure you have the Java plugin enabled? e.g. in Firefox, go to Settings, Plugins, and change Java Platform SE8 'Next Generation Java Plugin' to 'always activate'?
This sounds similar to the issues I had with Cisco WebVPN, Java-style. Once you have Java installed correctly and set as a plugin for any of the browsers you would like to use, see below.
Before you even start looking at browsers - if you think it is already set up correctly
Look at your Anti-Virus programs or anything else that could prevent it from working. McAfee Host Intrusion Protection is known to cause many Java programs to fail. Kapersky had issues, a while back, with Java on Windows (Java Applets not loading in Windows 8 ).
You must have a 64-bit browser to use 64-bit Java (also mentioned in the Chrome link below).
See below for any specific things that can be modified in the browser.
Chrome 43 is the more complicated browser to set up. They have a dedicated page with instructions.
How do I use Java with the Google Chrome browser?
Firefox 38 will prompt you.
In Internet Explorer 11, it's under Internet Options->Security. I recommend adding the hostname the applet is on as a Trusted Site (Select Trusted Sites and click the Sites button, then add the first part of the url). Click the Custom level button and make sure that Scripting of Java applets is not disabled.
If you still have problems with the applet:
Verify your Java version will work with the applet you are accessing
Verify the plugin is enabled for the browser through the Java Control Panel, which is available in Windows Control Panel, or on Mac/Linux, execute it from the JDK directory ($JAVA_HOME/ControlPanel ).
I ended up fixing the issue. I had to allow their UK site on the list for Java and enable the SSL 2.0 for HTTP in Java config as well

Where is option for Java Next-Generation Plugin?

I have a problem starting a commercial JavaWS application on IE11 on some computers at work. The application eventually starts but the user has to try several times before it works whick is very annoying.
We are currently using JRE 1.8.0_40 on Windows 7.
I have tried all possible things and in my research I've seen some references to Java Next-Generation Plugin and that It should be activated. When I open the Java Configuration I can't find this option. Why is that?
I have been reading this article where they mention Next-Generation Plugin and different ways of checking that it's activated.
If you read the article I linked thoroughly you find this way of checking/setting the option in the registry:
Go to Start/Run and type in “regedit.exe” in the Run dialog box. Click OK
Navigate to the following area in the registry (32-bit Java keys)
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Plug-in\<version>
Find or create the registry key UseNewJavaPlugin (REG_DWORD)
Ensure this key is set to value 1
Close the registry and restart your browser sessions for the setting to take affect

Java Web Start doesn't launch my .jar without enabling the "show console"

As stated in the title, my software won't launch unless the "Show console" option is selected in Java settings. I'm not sure what kind of code you'd like me to post, so please ask.
When I try to run it, the Java dialog pops up and the progress bar that loads the software completes before it closes. When the console is up, the software finally loads and displays afterwards. Without the console, nothing happens.
I've tried to run the web start on different computers, and they all had the same problem. The console doesn't print out any error messages, so I don't have anything to use for debugging the problem.
Some information about the software:
It's a JavaFX project, compiled using Oracle jdk 1.7.0_65 on OS X with Netbeans. It requires full security permission, and all jars (I use a bunch of libraries) are being signed using a key store from a trusted vendor.
EDIT
I tried to launch my program through console using the following command: javaws -viewer http://www.website.com/software.jnlp, and - without launching my software - it returned the following result to the terminal:
java.lang.NullPointerException
at sun.awt.SunToolkit.getSystemEventQueueImplPP(SunToolkit.java:1003)
at sun.awt.SunToolkit.getSystemEventQueueImplPP(SunToolkit.java:998)
at sun.awt.SunToolkit.getSystemEventQueueImpl(SunToolkit.java:993)
at java.awt.Toolkit.getEventQueue(Toolkit.java:1749)
at java.awt.EventQueue.invokeLater(EventQueue.java:1245)
at javax.swing.SwingUtilities.invokeLater(SwingUtilities.java:1290)
at com.oracle.deploy.update.UpdateCheck.updateStateChange(Unknown Source)
Could this be of any help?
For others who might have this problem, this is what caused it for me:
I had a thread running in the background that would show the stage once it was done loading all the assets. This stopped the whole thing from executing, so I had to change the loading logic inside of my code instead to allow for the primary stage to use the show-method in the UI-thread.

Cannot run two instances of java webstart

I am unable to run more than one instance of java webstart at any given time.
For example, I am unable to run both the production & QA instance of an application at once, both of which are launched via java webstart. Additionally, I am unable to run the java webstart cache viewer at the same time as either the production or QA instance of my application.
I am however able to run any of the above three webstart launches when they are run in isolation of each other. When I try to bring up a second option, I see the 'Java Loading...' screen which then disappears and nothing happens.
Additionally, I have tried to delete the webstart cache (via the java webstart cache viewer) and I receive the following error regardless of which JRE I point to:
"Bad installation. Error invoking Java VM (execv)
'path to my javaw.exe'"
I expect both the problems I mention above are interlinked. I do not believe I have changed any configuration recently and I have been happily running java webstart for years.
Has anyone seen such a problem before?
Thanks,
Jack
EDIT: When the second instance of webstart attempts to run, during the display of the 'Java Loading...' screen I can see in the task manager that a new javaw.exe process is spawned. This process almost immediately dies though. I'm not sure how to inspect the failure in that process, but I expect it is similar to the failure when trying to clear my cache through the webstart cache viewer.
You may be able to use javaws from the command line to run a second instance in -offline mode. The verbose option is handy, too.
javaws -offline -verbose MyApplication.jnlp
I think it is because both instances of the application use the same folder as current working directory. I do not remember exactly but it is somewhere under user home and the folder contains the application name or something...
So, if this is correct the solution is to change the application name like "My Application - QA" vs. "My Application" used on production.
The name is somewhere in jnlp.xml.
The reason may be the startup parameters for client java/javaw, which do not allow to run more than one instance of Java. For example because of set debug port. These parameters can be set in the command line or in the Java Control Panel -> Java -> button View.

Categories

Resources