| Using Windows shell folders with java |
|
|
There's a really annoying weakness in the Java virtual machine on Windows: the Java user.home system property is based on the location of the Windows Desktop folder not the Windows USERPROFILE variable. The user.home system property is useful for iX-Systems but worthless for Windows. In reality you want to know the location of " My Documents". What is required to locate this folders ? Yepp, just a short lookup in the Windows Registry. It is time to close this gap without JNI. Sun disinvest the Java Platform from the Windows Registry. The registry is bound to Windows and worthless for platform independent programing. Nevertheless, it may not be overlooked that more than 90% all PC windows use. SUN´s recommendationthe is to use the native WIN32 API by the means of the JAVA JNI interface. The programmer can either use a homebrew DLL or a commercial solutions. Example:
While debugging this Example with Eclipse you´ll discopver the WindowsPreferences Class, wich is internal and not visible in the JAVA API. The souce code is public available, here vor example. A little research with google brought the fundamental idea in lenkite´s WebLog. To extend the boilerplate was easy, see the following codefragment from a JUnit Test on howto use the windows shell folders. windowsPreferencesWrapper = WindowsPreferencesWrapper.getInstance() String test = windowsPreferencesWrapper.getHKCUKeyValue("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", "Local Settings"); assertNotNull(test); System.out.println(test); String test = windowsPreferencesWrapper.getHKLMKeyValue("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName"); assertNotNull(test); System.out.println(test); test = windowsPreferencesWrapper.getHKLMKeyValue("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentVersion"); System.out.println(test); assertNotNull(test); You can download the complete class from the download area .
|
