macinta
Posts: 9
Joined: Thu Jun 07, 2007 8:59 pm
Contact: Website

Java launcher can't write to system wide preferences (Vista)

Hello,

I have a Java program which needs to write to the system wide preferences via Preferences.systemRoot(). It works correctly when run by double clicking the Jar file but it fails with a BackingStoreException when run using the launcher created by Advanced Installer, I believe because it is being denied permission to write to the Windows Registry. If I right click on the launcher icon and select "Run as administrator", it actually works. It also works if I run the program from the command line or by double clicking the Jar. The problem occurs on Windows Vista when running Java 1.6.0_01-b06 (i.e., the latest release).

I created a bare bones test case to demonstrate this. I created it from scratch and using a different company name and settings from my main project, in order to avoid any Windows registry settings that might be causing this. Here is a program which demonstrates the problem:

Code: Select all


import java.io.*;
import java.util.prefs.*;
import javax.swing.*;

public class SetDummyPref {

    public static void main (String[] arg) {
        String message;

        // attempt to set a preference

        try {
            String path = "/com.pensamos/dummyPref";
            final Preferences prefs = Preferences.systemRoot().node(path);
            prefs.putLong("dummyKey", System.currentTimeMillis());
            prefs.flush();
            message = "Success!";
        }

        // extract stack trace from exception
        
        catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            message = sw.toString();
        }
        
        // print the result;
        
        message = System.getProperty("java.runtime.version") + "\n\n" + message;
        JTextArea ta = new JTextArea(message);
        JScrollPane sp = new JScrollPane(ta);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(sp);
        f.pack();
        f.setVisible(true);
        f.toFront();
    }

}

The manifest file for the Jar is:

Code: Select all


Manifest-Version: Version 1.0
Main-Class: SetDummyPref

I can send the AIP file via email if you want, although I don't think I changed anything beyond what the initial wizard set - the AIP file is pretty basic.

Thanks,
- Tim Macinta
gigi
Posts: 2103
Joined: Tue Apr 11, 2006 9:55 am
Contact: Website

Hi Tim,

If your java product requires administrator privileges then set the "Execution Level" to "Require Administrator" in the "Java Products" page.

Regards,
Gigi
______________
Gheorghe Rada
Advanced Installer Team
http://www.advancedinstaller.com
macinta
Posts: 9
Joined: Thu Jun 07, 2007 8:59 pm
Contact: Website

Hi Gigi,

It doesn't require administrator privileges when run without the launcher - that's the problem. Java has no problem writing to the global preferences when I double click the Jar file or run the program from the command line using "java.exe", and this is true regardless of whether the user is an administrator or not. However, when I use the Advanced Installer EXE launcher, the program can no longer write to the global preferences. I only mentioned that the EXE launcher works when run as administrator in hopes that this would help the diagnosis - I don't think that requiring administrative privileges is a viable option for the environments where my main program will be deployed, and there is nothing in the program that should require it.

Thanks,
- Tim Macinta
gigi
Posts: 2103
Joined: Tue Apr 11, 2006 9:55 am
Contact: Website

Hi Tim,

You don't get that behavior when you double click on the jar file because java.exe is virtualized.

Virtualization is implemented to improve application compatibility problems for applications running as a standard user on Windows Vista. Developers must NOT rely on virtualization being present in subsequent versions of Windows.

Prior to Windows Vista, many applications were typically run by administrators. As a result, applications could freely read and write system files and registry keys. If standard users ran these applications, they would fail due to insufficient access. Windows Vista improves application compatibility for standard users by redirecting writes (and subsequent file or registry operations) to a per-user location within the user’s profile.

For example, if an application attempts to write to "C:\Program Files\Contoso\Settings.ini", and the user does not have permissions to write to that directory, the write will get redirected to "C:\Users\Username\AppData\Local\VirtualStore\Program Files\contoso\settings.ini". Same for registry.

While developing Windows Vista programs, to reduce the complexity of virtualized files and registry keys, be sure to embed an application manifest with an appropriate *requestedExecutionLevel* in order to turn off file and registry virtualization.

Virtualization is intended only to assist in application compatibility with existing programs. Applications designed for Windows Vista should NOT perform writes to sensitive system areas, nor should they rely on virtualization to provide redress for incorrect application behavior.

If you really want your product to be virtualized then set the "Execution Level" for you java product to "No Execution Level Information". In this case no manifest will be added.

Hope this helps,
Gigi
______________
Gheorghe Rada
Advanced Installer Team
http://www.advancedinstaller.com
macinta
Posts: 9
Joined: Thu Jun 07, 2007 8:59 pm
Contact: Website

Hi Gigi,

Ah, you're right - this seems to be a problem with Java, then. It shouldn't be trying to write to a "sensitive" area for what I'm trying to do. I'm simply trying to save some settings for my applications which all users will share, and the Java Preferences API is responsible for determining how and where to actually store the data. It appears broken because 1) it doesn't have permission to write where it tries when not virtualized (e.g., with the EXE launcher) and 2) when it is virtualized, the registry location that it writes to is actually user-specific, thanks to the virtualization. So even though it looks like it works when I double click the Jar, it actually doesn't do what I want in that case either. Maybe I'll file a bug report with Sun.

Thanks again for all your help.

- Tim Macinta
macinta
Posts: 9
Joined: Thu Jun 07, 2007 8:59 pm
Contact: Website

In case anybody runs into this same problem, I submitted a bug report to Sun and they have accepted it:

http://bugs.sun.com/bugdatabase/view_bu ... id=6572807

- Tim Macinta

Return to “Common Problems”