I've been writing a class to find the real userID, using the linux binary /usr/bin/logname that returns the current user associated with my TTY. The command works fine in shell.
/usr/bin/logname
scott
But I cannot get the same as a String in java with the following code that I wrote.
private String currentUser;
public void getRealUser() throws Exception{
String cmd = "/usr/bin/logname";
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
this.currentUser = stdInput.readLine();
System.out.println(currentUser);
}
When I create the object, I am seeing null for the value currentUser, which means stdInput.readLine(); is not outputting anything.
Please let me know what am I doing wrong.
p.waitFor();Have you tried puttingp.waitFor();before theSystem.out.println?nullwithp.waitFor()afterSystem.out.println()this.currentUser =statement, but before theSystem.out.statement. On another note, there's the potential that the first line you get is a null line... I use a while loop to make sure I read all lines from theInputStream.