Monday, June 22, 2009

Applets don't initialize until browser window displayed

Here's a problem that first solved many years ago, forgot about, and had to solve in a new bit bit of code recently. My poor documentation originally and deficient memory since then cause me to waste an hour re-fixing the problem -- at least I knew I had solved it before!

I have a utility that uses embedded browser controls that contain java applets. These browsers are launched in the background and operate without user interaction until activated by a controlling GUI interface.

Many years ago Microsoft released an IE update that, as part of their settlement with Eolas meant that embedded ActiveX controls were more of a pain to work with because of a "click to activate" policy. While this has since been resolved in a later update, an odd behaviour traced back to that initial ActiveX update remains in place causing a headache:

When I launch a process that embeds a web page containing an applet, the applet does not initialize (even though the Java Console is there) until the tool containing the browser control and embedded page is somehow displayed on the desktop.

The scenario is as simple as a Win32 app started with this general approach:

hwnd = CreateWindowEx(...);
ShowWindow(hwnd,SW_HIDE);
UpdateWindow(hwnd);

Until hwnd is somehow activated on the screen via some other method, the Java plugin for the applet included on the embedded page starts up but the applet does NOT initialize. As soon as the hwnd is displayed, the JRE initializes the applet.

To avoid this problem, it's as simple as adding in an initial hwnd activation:

hwnd = CreateWindowEx(...);

SetWindowPos(hwnd,HWND_BOTTOM,0,0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),SWP_NOACTIVATE);
ShowWindow(hwnd,SW_HIDE);

UpdateWindow(hwnd);

With this change, the hidden applet merrily initializes and does its normal startup work.

Thursday, June 18, 2009

Java GUI really slow in VMWare guest?

I recently had to create a brand new VMWare guest for Windows XP. Until now I've held off running XP SP3 for various reasons, but for the sake of efficiency I installed this guest from an XP-SP3-VL ISO image.

After saving the base image I went and installed a test environment, which requires running a bunch of java GUI (and browser applet-based) programs simultaneously. For specific reasons, these versions are 1.3.1_06, 1.5.0_04, and 1.6.0_12. In testing this environment, I immediately noticed that any time a program was running in the JRE5 or JRE6 environment, the guest machine (and, to some extent, the host machine) became glacially slow. I'm talking "watch the guest screen repaint inch by inch" slow.

After various troubleshooting and, ultimately, reverting back to an SP2 bare image, it looks like *something* between SP2 and SP3 changed how video Hardware Acceleration behaves in Windows XP, at least inside VMWare, and this causes problems with how Java draws to the screen (well, modern versions at least -- 1.3.1_06 had no problems.) I am running VMWare Workstation 6.5 on a Windows XP SP2 host.

To solve this, do either of the following:
  • Turn Hardware Acceleration to None under Display Properties -> Settings -> Advanced -> Troubleshooting. Not recommended since presumably this is useful for other stuff.
  • Set the global environment variable "J2D_D3D=false" inside the guest OS. Using reskit tools this is "setx J2D_D3D false -m". According to Sun, this setting is used to turn off the Java 2D system's use of Direct3D in Java 1.4.1_02 and later.

I have not bothered figuring out what between SP2 and SP3 caused the problem on my system. I have not seen similar problems in my SP2 but otherwise fully up-to-date guest images on the same host. The key to the solution ultimately was found in VMWare community forums, but that thread discusses a different problem.

Finally a blog..

I'm setting up this blog so I'm able to document random bits of useful information I find, for my own future benefit and for anyone else who knows how to use a search engine. Let the fun begin...