If all you want is the Computer's User Name then by all means using the System.getProperty("user.name");
is the way to go. However, if there are other items you want to throw across the Windows Command Prompt then you might want to utilize something like this with a runCMD() method:
List<String> list = runCMD("/C echo %USERNAME%");
if (!list.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
Console will display the current User Name.
The method might look something like this:
public List<String> runCMD(String commandString) {
// Remove CMD from the supplied Command String
// if it exists.
if (commandString.toLowerCase().startsWith("cmd ")) {
commandString = commandString.substring(4);
}
List<String> result = new ArrayList<>();
try {
// Fire up the Command Prompt and process the
// supplied Command String.
Process p = Runtime.getRuntime().exec("cmd " + commandString);
// Read the process input stream of the command prompt.
try (BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()))) {
String line = null;
// Store what is in the stream into our ArrayList.
while ((line = in.readLine()) != null) {
result.add(line);
}
}
p.destroy(); // Kill the process
return result;
}
catch (IOException e) {
System.err.println("runCMD() Method Error! - IO Error during processing "
+ "of the supplied command string!\n" + e.getMessage());
return null;
}
}