views:

68

answers:

1

How can I write system preferences with Java, using Preferences.systemRoot()?

I tried with:

Preferences preferences = Preferences.systemRoot();
preferences.put("/myapplication/databasepath", pathToDatabase);

But I got this error message:

2010-maj-29 19:02:50 java.util.prefs.WindowsPreferences openKey
VARNING: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002. Windows RegOpenKey(...) returned error code 5.
Exception in thread "AWT-EventQueue-0" java.lang.SecurityException: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002: Access denied
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.putSpi(Unknown Source)
    at java.util.prefs.AbstractPreferences.put(Unknown Source)
    at org.example.install.Setup$2.actionPerformed(Setup.java:43)

I would like to do this, because I want to install an embedded JavaDB database, and let multiple users on the computer to use the same database with the application.

How to solve this? Can I invoke UAC and do this as Administrator from Java? And if I log in as Administrator when writing, can I read the values with my Java application if I'm logged in as a User?

+2  A: 

You cannot write to any arbitrary registry location from java preferences - all preferences are stored under a subkey Software\Javasoft\Prefs. With user preferences mapping to the HKEY_CURRENT_USER hive, and system mapping to the HKEY_LOCAL_MACHINE hive.

To write to the registry, you could use the windows "REG" command line tool. This page details other ways of modifying the registry. including use of .reg files.

I had the same need - to write to the registry from java - I solved it by writing a small .NET command line utility to do it.

The Sun Windows JDK does ship with generic code to write to arbitrary portions of the registry (WindowsPreferences), but it's not public. This article describes how to access this class using reflection.

mdma
So I cannot write on `Software\Javasoft\Prefs` with `Preferences.systemRoot()`, is it only for `Preferences.userRoot()`? It doesn't matter for me where I write it, I just want it for every user, or is there no use for `Preferences.systemRoot()`?
Jonas
all roots write under Syftware\JavaSoft\Prefs. The user root writes to the user portion of the registry, the system root writes to the system (i.e. admin privilleges required.) either way, you cannot write above Software\javasoft\prefs. To do that, see the second part of my answer.
mdma
Ah, thanks. So the question is: How can I write system root writes to `Software\Javasoft\Prefs` (admin privileges requeired) when not logged in as admin? is it possible to use UAC or similar technique (i.e java technique)? **Or**, if I write to system root as an admin, can I read the values as a user?
Jonas
You can always read system roots as user, so login as admin and write, and your users can read. To write as admin you of course need admin privileges. Windows can grant MSI installers elevated admin privileges, so you could install your app as a MSI which can write to the system preferences (HKLM hive.) I've used AdvancedInstaller in the past, there's a free edition available.
mdma