开发者

How do I get the username in Java (ie, who -m in Java) (or Jython 2.1)

Here's the situation. I have a jython 2.1 script 开发者_JS百科in a shared account that needs to know who is calling it. In bash, I can simply use $(who -m) and it will give me the correct username.

By "shared account", I mean I log in as myself, then $(sudo su - shared_account) to get to the shared account.

I haven't been able to find anything in java (or jython) that would give me a similar result. Even trying to call Runtime.getRuntime().exec("who -m") doesn't do anything. When I try to read the InputStream from the process returned by exec, the stream is empty.


To get the process owner do this:

System.getProperty("user.name");

The syntax of getRunTime().exec() is tricky.

Runtime.getRuntime().exec(new String[] {"/path/to/who", "-m"});


I've come up with an option, even though I don't really love it:

Add this flag to the java call:

-Duser.name="$(who -m | awk '{print $1}')"

And then access the user name with:

System.getProperty('user.name')


You can just use this.

System.out.println( System.getProperties().getProperty("user.name"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜