I am trying to find the current logged in username from Java.
Process p;
try
{
p = Runtime.getRuntime().exec("who -m");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null)
System.out.println(line);
}
catch (Exception e)
{
}
The above code does not print any result. However if i remove -m option, it prints user name and other details. And also i tested with other options like -s, -u. It all works except -m. Does anyone has an idea why is it so?
Note: I am aware of
System.getProperty("user.name");
But that's not a solution in my case. I am calling a shell script in which "who -m" is used. So, I cannot use java classes.