Windows Registry Tutorial - Read/Write windows registry

Yes, it is possible now to read/write registry from java programs without native (JNI) code !


Lets write something to registry

    void sample01() throws IOException
        Reg reg = new Reg();
        Reg.Key key = reg.add("HKEY_CURRENT_USER\\hello");

        key.put("world", "Hello World !");

This code will create a "hello" key in HKEY_CURRENT_USER hive, and put "Hello World !" with name "world".


Lets read something from Control Panel settings...

    void sample02() throws IOException
        Reg reg = new Reg("HKEY_CURRENT_USER\\Control Panel");
        Reg.Key cp = reg.get("HKEY_CURRENT_USER\\Control Panel");
        Reg.Key sound = cp.getChild("Sound");
        String beep = sound.get("Beep");

Create environment variable

Lets create a new environment variable under current users environment....

    void sample03() throws IOException
        Reg reg = new Reg();
        Reg.Key env = reg.add("HKEY_CURRENT_USER\\Environment");

        env.put("SAMPLE_HOME", "c:\\sample");

Thats it ! Now your environment contains variable SAMPLE_HOME ! Unfortunetly you have to restart Windows to see this variable.... but hey, we crated new environment variable from java without any native code !