user3733078
user3733078

Reputation: 249

Confused about reading data from Registry

I am trying to get some data from registry .The problem is while loop ,because when the application is running in debug mode I can view the value of a line variable .But end of the loop line variable is assigned null

  private final String DESKTOP_PATH="\"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\"
            + "CurrentVersion\\Explorer\\Shell Folders\" /v Desktop";

    private final String REG="REG_SZ";

    private final String EXACUTE_STR="reg query "+DESKTOP_PATH;
        private String getDesktopPath() throws IOException   {
        Process p = null;
        String line = null;
        try {
            p = Runtime.getRuntime().exec(EXACUTE_STR);
            p.waitFor();
            InputStream stream=p.getInputStream();


            BufferedReader reader=new BufferedReader(new InputStreamReader(stream));


            while((line=reader.readLine())!=null){
                line+=reader.readLine();

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return line;

Upvotes: 1

Views: 51

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70909

Consider using jna the Java Native Access library which provides Registry handling utilities.

The reason that I suggest a different toolkit instead of simply solving your problem has to do with a few items. When launching an external executable, you have to handle input and output in a buffered manner, and there are two streams of output (normal and error).

In short, typically all of these input and output streams might need to be available, but you won't typically notice failures when they are not present in the typical development environment. Later, in production environments (headless, console-less, etc) these problems become apparent.

To solve these problems with a CLI call, you typically then set up Buffer collectors to capture the output in independent threads, and sometimes you need to stand up a fake buffer provider (some programs check that input is readable, even if they don't read any input!). The JNA library uses JNI, which greatly reduces the issues by side-stepping the CMD shell that wraps your executable call.

However, if you only wanted to know about the logic error in your code, JimW did a good job explaining it.

Upvotes: 2

JimW
JimW

Reputation: 186

You are replacing the contents of line with what you read from reader.readLine(), when that finally returns null you return line which is of course null.

Instead create a StringBuilder before starting the loop and append to that.

StringBuilder buffer = new StringBuilder();
while ((line = reader.readLine())!=null) {
    buffer.append(line);
}
// buffer.toString() is the String you are looking for

You could also .trim() inside the append if you want to remove end lines.

Upvotes: 1

Related Questions