evilarceus evilarceus - 11 months ago 82
Java Question

Why does this registry reader for Java return nothing?

I'm creating a part of an API for Java which gathers information about your OS (specifically Windows right now). To do this without any external JARs/APIs, I want it to read the registry.

Code:

package main;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class OperatingSystem
{
String edition;
String buildBranch;
String CurrentBuild;
String productName;
String release;
String productID;
String owner;

public static void main(String[] args)
{
OperatingSystem os = new OperatingSystem();
os.getWindowsInfo();
os.returnWindowsBuild();
os.returnWindowsEdition();
os.returnWindowsBranch();
os.returnWindowsRelease();
os.returnProductID();
os.returnWindowsOwner();
}

public String returnWindowsBuild()
{
return CurrentBuild;
}
public String returnWindowsEdition()
{
return edition;
}
public String returnWindowsBranch()
{
return buildBranch;
}
public String returnWindowsRelease()
{
return release;
}
public String returnProductID()
{
return productID;
}
public String returnWindowsOwner()
{
return owner;
}

public void getWindowsInfo()
{
edition = getRegistryValue("\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"", "EditionID", "Windows version not detected");
buildBranch = getRegistryValue("\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"", "BuildBranch", "Windows build branch not detected");
CurrentBuild = getRegistryValue("\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"", "CurrentBuildNumber", "Windows build number not detected");
productName = getRegistryValue("\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"", "ProductName", "Windows product name not detected");
release = getRegistryValue("\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"", "ReleaseID", "Windows release not detected");
productID = getRegistryValue("\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"", "ProductID", "Windows product ID not detected");
owner = getRegistryValue("\"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\"", "RegisteredOwner", "Windows owner not detected");
}

public String getRegistryValue(String query, String value, String Fail)
{
try {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe",
"/c",
"REG QUERY " + query + " /v " + value
);
Process p = builder.start(); // Query registry
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
for(int i = 0; i < 2; ++i) // Reads line 2 for output
r.readLine();
String result = r.readLine();
return result;
} catch(Exception e) {
return Fail; // Returns the third argument in getRegistryValue, in case the operation fails
}
}
}


However, when I run it, it outputs nothing. How would I get it to output the value of the specified registry key?
(This code did work at one point, not sure why it's not working now)

Answer Source

Try to wrap os.returnWindowsBuild(); with System.out.print() if you are looking for some output in console.