How to write Windows Registry using Java code/program?

UPDATED: 12 July 2011
I searched so many site to write Windows Registry using java code/program but there is not any good information available on internet. I came with new API javaQueryAPI that will help you to write windows Registry using java code.

Important notice for existing user ( javaQueryAPI 7.0 or less ) : 
Thanks for using javaQueryAPI bundle. I've made major changes in javaQueryAPI 8.0 so don't import this bundle if you don't want to change the code of your code. Many classes exist in javaQueryAPI 7.0 will no longer available in javaQueryAPI 8.0. I'm going to give you code snippet for existing and new javaQueryAPI.

/*
* javaQueryAPI 7.0 or less
* method : (RKey,path,keyType,Name,Value);
* RKey : Root key (folder) to create the registry. (i.e : HKEY_CURRENT_USER )
* path : Folder path of your software or existing folder that must starts with (\\). (i.e : \\software\\microsoft)
* keyType : Its a keyType that you want to create DWORD,String,Binary.
* Name : Name of key.
* Value: Value of key.
*/
import javaQuery.core.Rkey;
import javaQuery.core.keyType;
public class jqRegDemo {
    public static void main(String[] args) {
        jqReg _jr = new jqReg();
        String message = _jr.jqReg(RKey.HKEY_CURRENT_USER, "\\jqReg",keyType.Dword, "test","00000320");
        System.out.println("Response: "+message);
    }
}

The above code works for javaQueryAPI 7.0 or less. For new javaQueryAPI 8.0 check out the below code snippet.

/*
* javaQueryAPI 8.0 or above
* method : (RegistryKey,path,keyType,Name,Value);
* RegistryKey : Root key (folder) to create the registry. (i.e : HKEY_CURRENT_USER )
* path : Folder path of your software or existing folder that must starts with (\\). (i.e : \\software\\microsoft)
* keyType : Its a keyType that you want to create DWORD,String,Binary.
* Name : Name of key.
* Value: Value of key.
* javaQueryBundle.createRegistry() is new class created for better use. That allows you to overcome from creating object of class. 
* I prefer you to use IDE so it'll give you better idea of class covered in javaQueryBundle.
*/
import javaQuery.core.Registry;
import javaQuery.importClass.javaQueryBundle;

public class Demo {
    public static void main(String[] args) {
        String response = javaQueryBundle.createRegistry().createKey(Registry.HKEY_CURRENT_USER, "\\jqreg", Registry.key_String_Value, "Software", "javaQueryAPI");
        System.out.println(response);
    }
}

You must follow rules to create new - for existing Registry key:
  1. When you want to change (Default) value give key name as (Default). It'll change default value.
  2. String value = "\\\"javaw.exe\\\" \\\"-jar\\\" \\\"c:\\\\jqEditor\\\\jqEditor.jar\\\" \\\"%1\\\"" this will set value as "javaw.exe" "-jar" "c:\jqEditor\jqEditor.jar" "%1"
  3. If Registry value contains double quote ( " ) then you have to use ( \ ). for example check rule 2.
  4. If Registry value contains forward slash ( \ ) then also you have to use ( \ ). for example check rule 2.
  5. You must have admin rights of system to create Registry.

Check out the http://www.javaquery.com/p/downloads.html for download.

4 comments :

  1. Can't help getting the same error over and over again:

    Response: java.io.FileNotFoundException: C:\jqReg.bat (Access is denied)

    Whole code would look like:
    package Main;

    import javaQuery.core.RKey;
    import javaQuery.core.jqReg;
    import javaQuery.core.keyType;

    public class Main
    {

    public static void main(String[] args)
    {
    jqReg _jr = new jqReg();
    String message = _jr.jqReg(RKey.HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Explorer\\Main\\",keyType.Dword, "test","00000320");
    System.out.println("Response: "+message);
    }

    }

    ReplyDelete
  2. Because you don't have admin access rights...thats why its giving error...

    ReplyDelete
  3. public static void main(String[] args) {
    jqReg _jr = new jqReg();
    String message = _jr.jqReg(RKey.HKEY_CURRENT_USER, "\\jqReg",keyType.Dword, "test","00000320");
    System.out.println("Response: "+message);
    }
    i build succesful Response: Created Thank you for using jqReg Visit http://www.javaquery.com

    but when i open regedit i dont see any jqReg Key in HKEY_CURRENT_USER
    please help me

    ReplyDelete
  4. Its working fine on my machine. I think you don't have rights(user rights) to write registry key. You must need Admin access of machine.

    ReplyDelete