How to use Java Runtime.exec() with Windows REG utility to Read/Update/Delete entries in HKEY_LOCAL_MACHINE\...\CurrentVersion\Run?

I want to use Runtime.exec() to update the registry for HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run using the Windows REG command utility.

Need to be able to add/remove/read an entry from the "Run" key to allow my Swing application to run at startup and check if it is configured to run at startup so I can mark the option as checked or unchecked in the GUI. I had this working with JNI but the library was 32bit only so it doesn't work on 64bit. I'm thinking this will be a better approach. Don't even need to include a library this way and I don't think REG is going away or changing.

Has anyone done this before or know how to do this?

Thanks!

Answers


This approach might not work as expected. See in x64 Windows is there a way to run a Runtime.exec() process avoiding 'Registry redirection'


I added a couple new methods (addValue/deleteValue) to the example found here: read/write to Windows Registry using Java

/**
 * @author Oleg Ryaboy, based on work by Miguel Enriquez
 */
public class WindowsReqistry
{

    /**
     * 
     * @param location
     *          path in the registry
     * @param key
     *          registry key
     * @return registry value or null if not found
     */
    public static final String readRegistry(String location, String key)
    {
        try
        {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec("reg query \"" + location + "\" /v \"" + key + "\"");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();

            // Output has the following format:
            // \n<Version information>\n\n<key>\t<registry type>\t<value>
            if (!output.contains("\t"))
            {
                return null;
            }

            // Parse out the value
            String[] parsed = output.split("\t");
            if(parsed.length > 0)
            {
                String result = parsed[parsed.length - 1].trim();
                result = result.substring(1, result.length() - 1);
                return result;
            }
        }
        catch (Exception e)
        {
        }
        return null;
    }

    static class StreamReader extends Thread
    {
        private InputStream is;
        private StringWriter sw = new StringWriter();;

        public StreamReader(InputStream is)
        {
            this.is = is;
        }

        public void run()
        {
            try
            {
                int c;
                while ((c = is.read()) != -1)
                    sw.write(c);
            }
            catch (IOException e)
            {
            }
            finally
            {
                try
                {
                    is.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        public String getResult()
        {
            return sw.toString();
        }
    }

    public static boolean deleteValue(String key, String valueName)
    {
        try
        {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec("reg delete \"" + key + "\" /v \"" + valueName + "\" /f");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();

            // Output has the following format:
            // \n<Version information>\n\n<key>\t<registry type>\t<value>
            return output.contains("The operation completed successfully");
        }
        catch (Exception e)
        {
        }
        return false;
    }

    public static boolean addValue(String key, String valName, String val)
    {
        try
        {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec(
                    "reg add \"" + key + "\" /v \"" + valName + "\" /d \"\\\"" + val + "\\\"\" /f");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();

            // Output has the following format:
            // \n<Version information>\n\n<key>\t<registry type>\t<value>
            return output.contains("The operation completed successfully");
        }
        catch (Exception e)
        {
        }
        return false;
    }

}

Need Your Help

Organising resources for an Android project into multiple packages

android android-resources eclipse-adt

I'm working on an Android project using Eclipse. The project contains multiple modules connecting to a central core, and it would make sense to organise them into subpackages, e.g.