Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create and write a value in a special key.
Thank you in advance.

What I have tried:

I test 'WinRegistry Class' but I could not create and write a value in key also try some other class which I found them by searching on the net. unfortunately, none of them solve my problem.
Posted
Updated 15-Apr-18 0:07am
v3
Comments
Richard MacCutchan 5-Apr-18 3:56am    
Java does not provide a default implementation for this. Try using JNI and writing the code in C.
M.reza Vasebi 7-Apr-18 3:25am    
Thank you.
You propose to use JNI library for writing this program. it means that I write this program in C or C++ and by using JNI or JNA libraries execute the written program in Java. Did I understand correct?
Richard MacCutchan 7-Apr-18 3:41am    
Yes, you need C/C++ routines to access the registry. Your Java program can then access those routines via JNI/JNA.
M.reza Vasebi 7-Apr-18 4:42am    
Thanks so much.

If you want to access the windows registry with Java you can use the Java Runtime class
Java
Runtime.getRuntime().exec("REG ADD HKCU\\SOFTWARE\\TEST")



Here is a little sample application i've wrote to show some possibilities of the windows commandline registry tool in combination with the Java Runtime class.

This code uses the m$ reg.exe tool.

If you want to create, modify or delete special registry keys like HKEY_LOCAL_MACHINE or HKEY_CURRENT_CONFIG you may need elevated privileges on your system.


Java
import java.io.IOException;
import java.util.Scanner;


/**
 * Sample Java Application to access to windows registry through 
 * the windows commandline application reg.exe
 * 
 * Just a sample application: 
 * 
 * Open your command prompt and enter REG /? to add missing features
 * 
 * 
 */
public class WinReg {
	
	/**
	 * Success status code
	 */
	public static final int REG_SUCCESS = 0;
	
	/**
	 * Failure status code
	 */
	public static final int REG_FAILURE = 1;

	
	
	/**
	 * Implemented root-keys<br>
	 * HKLM HKEY_LOCAL_MACHINE elevated privileges needed<br>
	 * HKCU HKEY_CURRENT_USER <br>
	 * HKCR HKEY_CLASSES_ROOT elevated privileges needed<br>
	 * HKU HKEY_USER  <br>
	 * HKCC HKEY_CURRENT_CONFIG elevated privileges needed<br>
	 *
	 */
	private enum WRKey {
		HKLM,  HKCU , HKCR , HKU , HKCC
	}
	
	
	/**
	 * Registry data-types
	 */
	private enum WRType {
        REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ,
        REG_DWORD, REG_QWORD, REG_BINARY, REG_NONE
	}
	
	/**
	 * Creates a new string for the registry cli.
	 * 
	 * @param hkey the  root key [ HKLM , HKCU ] 
	 * @param key the name of the key  SOFTWARE\WINDOWS
	 * @param valueName the name of the value
	 * @param data 
	 * @param type 
	 * @param force override an existing key ?
	 * 
	 * @see WRKey
	 * @see WRType
	 * @return the string value to the registry cli
	 */
	
	public String createRegString(WRKey hkey, String key, String valueName, byte[] data,  WRType type, boolean force) {
		String keyString = " "+hkey+"\\" + key; 
		String valueString = valueName!=null 	? " /v "+ valueName : "" ;
		String dataString =  data != null 		? (" " + ( data.length>0 ? " /d " + new String(data) : "")):"";
		String typeString = type != null 		? " /t " + type : "";
		
		return keyString + valueString + dataString + typeString +  (force ? " /f" : "");
	}
	
	/**
	 * Adds a new key to the windows registry
	 * 
	 * @param hkey The root-key [ HKLM , HKCU ] 
	 * @param key the key name to create eg. SOFTWARE\TEST\ABCD
	 * @return true on success
	 * @throws IOException
	 * @throws InterruptedException
	 * 
	 * @see WRKEY
	 * 	
	 */
	public boolean addKey(WRKey hkey, String key) throws IOException, InterruptedException{
		Process proc = Runtime.getRuntime().exec("REG ADD "+hkey+"\\" + key + " /f");
		proc.waitFor();
		
		return proc.exitValue() == REG_SUCCESS;
	}
	
	/**
	 * Adds a value to an existing registry key
	 * @param hkey The root-key [ HKLM , HKCU ] 
	 * @param key the key name to create eg. SOFTWARE\TEST\ABCD
	 * @param valueName the name of the value to add the data
	 * @param data the data as a byte array
	 * @param type the type of data [ REG_STRING ,REG_DWORD ]
	 * @return true on success
	 * @throws IOException
	 * @throws InterruptedException
	 * 
	 * @see WRKey
	 * @see WRType
	 */

	public boolean addValue(WRKey hkey, String key, String valueName, byte[] data,  WRType type) throws IOException, InterruptedException {
		String regString = createRegString(hkey,key,valueName,data,type,true);
		Process proc = Runtime.getRuntime().exec("REG ADD " + regString);
		proc.waitFor();
		
		return proc.exitValue() == REG_SUCCESS;
		
	}

	/**
	 * Shows a registry value
	 * @param hkey The root-key [ HKLM , HKCU ] 
	 * @param key the key name to open eg. SOFTWARE\TEST\ABCD
	 * @param valueName the name of the value 
	 * @return true on success
	 * @throws IOException
	 * @throws InterruptedException
	 * 
	 * @see WRKey
	 */
	public boolean showValue(WRKey hkey, String key, String valueName)  throws IOException, InterruptedException{
		String regString = createRegString(hkey,key,valueName,null,null,true);
		Process proc = Runtime.getRuntime().exec("REG QUERY " + regString);
		proc.waitFor();
		
		if(proc.exitValue()==REG_SUCCESS) {
		
			Scanner sc = new Scanner(proc.getInputStream());
			String str = "";
			do {
				str = sc.nextLine();
				System.out.println(str);
			}while(str != null);
		}
		else {
			System.err.println("Query failure..");
		}
		
		return proc.exitValue()==REG_SUCCESS;
	}
	
	
	/**
	 * 
	 * @param hkey The root-key [ HKLM , HKCU ] 
	 * @param key the key name to open eg. SOFTWARE\TEST\ABCD
	 * @param valueName the name of the value 
	 * @param withChildren view all subdirectories
	 * @return true on success
	 * @throws IOException
	 * @throws InterruptedException
	 * 
	 * @see WRKey
	 */
	public boolean showAllValues(WRKey hkey, String key, String valueName, boolean withChildren) throws IOException, InterruptedException {
		String regString = createRegString(hkey,key,null,null,null,false);
		Process proc = Runtime.getRuntime().exec("REG QUERY " + regString + "\\" + valueName + " " + (withChildren? " /s" :" "));
		proc.waitFor();
		
		
		if(proc.exitValue()==REG_SUCCESS) {
		
			Scanner sc = new Scanner(proc.getInputStream());
			
			String str = "";
			do {
				str = sc.nextLine();
				System.out.println(str);
				
			}while(sc.hasNext() && str != null);
			if(sc!=null) {
				sc.close();
			}
		}
		else {
			System.err.println("Query failure..\n" + regString);
		}
		
		
		return proc.exitValue()==REG_SUCCESS;
	}
	
	
	/**
	 * Sample program to access the windows registry
	 * 
	 * <br>
	 * NOTE: To create, modify or delete entries on HKEY_LOCAL_MACHINE you'll need elevated privileges
	 * 
	 * @param args
	 */
	public static void main(String ... args) {
		
		WinReg reg = new WinReg();
		
		try {
			
			// create a new registry key on HKEY_CURRENT_USER\\SOFTWARE\AAAA
			if( reg.addKey(WRKey.HKCU, "SOFTWARE\\AAAA") ) {
				System.out.println("Key created");
			}
			else {
				System.err.println("Error: could not create the key");
			}
			
			// create a new registry value on HKEY_CURRENT_USER\\SOFTWARE\AAAA  Name: sampleValue Type REG_SZ
			if( reg.addValue(WRKey.HKCU, "SOFTWARE\\AAAA", "sampleValue", "Tesalue".getBytes(), WRType.REG_SZ) ) {
				System.out.println("value created");
			}
			else {
				System.err.println("Error: could not create the value");
			}
			
			
			
			// list all registry values at HKEY_CURRENT_USER\\SOFTWARE\AAAA  with subdirectories
			if(! reg.showAllValues(WRKey.HKCU, "SOFTWARE\\AAAA", "", true) ) {
				System.err.println("Error: could not show the values");
			}
			
		}
		catch( InterruptedException | IOException e) {
			System.err.println("An error occurred. " + e.getMessage() );
		}
	}
	
}
 
Share this answer
 
v2
Comments
M.reza Vasebi 24-May-18 16:52pm    
Thank you so much because of your help.
hey bro but can't work "HKCR" wrgKey
 
Share this answer
 
Comments
Richard Deeming 16-Nov-20 4:24am    
If you want to ask for more information or clarification about a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900