03 | 09 | 2010
Main Menu
Affiliates
Who's Online
We have 33 guests online
Alexa
Java Registry Wrapper
Java Concepts
Written by Vimal   
Saturday, 14 June 2008 11:42

Rating 3.9/5 (29 votes)

Java Registry Wrapper is a free pure java library for performing common windows registry functions. It provides the following functionalities.
  1. Opening Registry Keys
  2. Create Registry Keys
  3. Delete Registry Keys
  4. Enumerate Sub Keys
  5. Enumerate Registry Values
  6. Write/Update Registry Values.

Java Registry Wrapper was inspired by link posted on DZONE(http://lenkite.blogspot.com/2008/05/access-windows-registry-using-java.html) about accessing windows registry from pure java code. The trick was to use reflection to access private methods defined in WindowsPreference class. The trick was very good but adding reflection code everywhere in your applications where you needed to access registry was not practical. So I decided to create a Opensource Project for a Java Registry Wrapper so that accessing and manipulating windows registry values could be simple method calls. This wrapper class just uses reflection to delegate all calls to WindowsPreference class and also provides methods that directly accept string keys and values.


You can download Java Registry Wrapper from Google Code.
View the source code for Java Registry Wrapper here.

 


Below is some sample code that I wrote for demonstrating Java Registry Wrapper.

package org.snipecode.reg.test;

import org.snipecode.reg.RegUtil;

import junit.framework.TestCase;

public class RegUtilTestCase extends TestCase
{
    private static void testWriteRead()
    {
        // Create a key
        int handle = RegUtil.RegCreateKeyEx(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Java\\regutil")[RegUtil.NATIVE_HANDLE];
        
        //close the handle
        RegUtil.RegCloseKey(handle);

        //open a new handle with all access
        handle = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Java\\regutil",RegUtil.KEY_ALL_ACCESS)[RegUtil.NATIVE_HANDLE];
        
        //Write a value
        RegUtil.RegSetValueEx(handle, "TestName", "TestValue");
        //Read the value
        byte[] val = RegUtil.RegQueryValueEx(handle, "TestName");
        //Check the value
        System.out.println(new String(val).toString().trim());
        // Close the handle
        //delete the value
        RegUtil.RegDeleteKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\SnipCode\\regutil");
        RegUtil.RegDeleteKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\SnipCode");
    }
    
    private static void testReadEnum()
    {


        // Open a Handle
        int[] ret = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", RegUtil.KEY_QUERY_VALUE);
        int handle = ret[RegUtil.NATIVE_HANDLE];
        
        // get the Number of Values in the key
        int[] info = RegUtil.RegQueryInfoKey(handle);
        int count = info[RegUtil.VALUES_NUMBER];
        int maxlen = info[RegUtil.MAX_VALUE_NAME_LENGTH];
        
        for(int index =0 ;index< count;index++)
        {
            // get the Name of a key
            // Note to use 1 greater than the length returned by query
            byte[] name = RegUtil.RegEnumValue(handle,index, maxlen+1);
            System.out.print(new String(name).trim() +" = ");
            
            // Get its Value
            byte[] values = RegUtil.RegQueryValueEx(handle, name);
            if(null!=values)
            System.out.print(new String(values).trim());
            System.out.println();
        }
        
        // Finally Close the handle
        RegUtil.RegCloseKey(handle);

    
    }
    public static void main(String[] args)
    {
        testWriteRead();
        testReadEnum();
        
    }

}



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Comments
Search
Anu   |59.163.89.xxx |2008-06-18 09:36:02
Can to describe what all registry operations come handy with the java req
wrapper
Charon   |213.128.110.xxx |2008-06-19 08:34:31
Wow thats great!

Thats exactly the thing i need right now. When are you gonna
publish the code on GoogleCode? I don't like precompiled jar's that much
:-)

Thanks ind advance
Markus
Vimal  - Code for JavaRegistryWrapper     |122.167.128.xxx |2008-06-19 09:58:37
You can find the source code here
http://www.snipcode.org/java/1-java/23-java-cla...
Charon   |213.128.110.xxx |2008-06-19 10:52:11
Seems like the site has some problems, i only get a white page.

Thanks for
uploading it though, i will just try to download it again
later.

Regards
Markus
mhp   |59.183.27.xxx |2008-06-20 00:20:43
Hi Vimal,

Could you upload the source somewhere? The link you posted doesn't
work. Thanks!
Vimal  - The Links Works Now   |122.167.154.xxx |2008-06-20 15:04:27
Hi the link is working now. I will upload it to google code also.
David Jiang  - Java Registry Reader   |203.162.3.xxx |2008-12-20 04:22:10
Hi Vimal,
Thanks for your code, but it can't read DWORD value, it only get
String value from registry, do you have have solutions for reading Window
Registry DWORD, BINARY value? Thanks
Marc  - Request for jdoc   |83.30.56.xxx |2008-06-23 06:25:49
Thanks for that great piece of food work!
Can I only request for jDoc?
Marc   |83.30.56.xxx |2008-06-23 06:26:13
s/food/good/
Marc  - Limited to HKEY_LOCAL_MACHINE and HKEY_CURRENT_USE   |83.30.56.xxx |2008-06-23 10:51:09
Please have in mind that this allows you only to operate on: HKEY_LOCAL_MACHINE
and HKEY_CURRENT_USER. The rest of registry is unavailable by this. That's
serious limitation, big enought that this should be mentioned. Please correct me
if I'm wrong.
gujaru   |202.32.8.xxx |2008-06-24 03:23:51
Can't Write to DWORD . Does anyone know how to do it?
Marco  - Limited to value type REG_SZ   |155.56.68.xxx |2008-07-15 03:12:16
Unfortunately I cannot access other types than REG_SZ, i.e. REG_EXPAND_SZ,
DWORD. Does anyone know if it is possible? If yes how?
Anonymous   |58.186.144.xxx |2008-07-21 23:55:49
good
Anonymous   |64.46.248.xxx |2008-08-15 12:12:25
This is good work. I have a question...

How can I get a list of subkeys under a
key? For example in under HKLM\Software\JavaSoft, I would like to get
the list of subkeys (Java Development Kit, Java Runtime Environment etc). Can
this be done using the library?
Thanks
Tony Clulow   |62.190.6.xxx |2008-09-05 09:46:38
I too would like some JavaDoc.

However I've written some code using this
library, but can't enumerate the Sub-Keys of a Key.

I can get
the SubKey count and MaxLength, but RegEnumKeyEx() always returns
null.
Code:

int[] ret
= RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\
2;Classes", RegUtil.KEY_QUERY_VALUE);
int handle
= ret[RegUtil.NATIVE_HANDLE];

 int[] info =
RegUtil.RegQueryInfoKey(handle);
  int count =
info[RegUtil.SUBKEYS_NUMBER];
  int maxlen =
info[RegUtil.MAX_KEY_LENGTH];  
byte[] name
= RegUtil.RegEnumKeyEx(handle, 0, maxlen);

 System.out.println("Count " + count + ", maxlen " +
maxlen + ", Name " + name);

produces:
Code:

Count 5370, maxlen 70, Name null
the_29  - Nice - but old     |88.116.60.xxx |2008-09-15 05:58:35
Hohi!

My project (which do the same) was created on 30 November
2006!
http://sourceforge.net/projects/java-registr y/

Maybe i will add my
project into google too!

And since version 3.0 it can read/write any values
(using the regedit.exe)

Best regards, joerg
vbplayr2000   |130.76.32.xxx |2009-02-26 16:40:00
Do you have a sample for the WinRegistry 3.4? Tutorial? I'm new to java and am
just having difficulty in getting started.

What I want to do is develop a very
simple java application that is a toggle switch to set a DWORD entry (Enabled)
from on to off (change value from 0 to 1).

Any help would be appreciated.
Ranjan   |211.237.20.xxx |2008-09-24 23:27:26
hi All,

i am unable to do the Registry add and delete.......... i am doing a
project in SWT. if i click on a check box it should disable my application on
window startup............. please help me.


Regards,
Ranjan
D   |85.230.169.xxx |2008-11-10 04:49:15
Java does not found the import files. package org.snipecode.reg does not exist Does anyone have any idea?
Ratnesh  - Read Registry by jdk1.1 or 1.2   |61.247.238.xxx |2008-12-22 04:40:55
how i can read registry by jdk1.1,1.2
i have read by jdk 1.4 and later but i req
on jdk 1.1 or 1.2plz reply
Diego  - sub keys   |217.57.13.xxx |2009-08-24 10:48:11
To enumerate the subkeys of the specified open registry key the key must have
been opened with the KEY_ENUMERATE_SUB_KEYS access right (see
http://msdn.microsoft.com/en-us/library/ms724862(V S.85).aspx).

I was not able
to make it work with KEY_ENUMERATE_SUB_KEYS access right, but using
KEY_ALL_ACCESS access right it works.
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 14 June 2008 13:37 )
 
Bottom Ad
Your Ad Here