有没有办法使用Java在Linux机器上获取用户的UID? [英] Is there a way to get user's UID on Linux machine using java?

查看:353
本文介绍了有没有办法使用Java在Linux机器上获取用户的UID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用Java在Linux机器上获取用户的UID?我知道System.getProperty("user.name");方法,但是它返回的用户名,并且我正在寻找UID.

Is there a way to get user's UID on Linux machine using java? I'm aware of System.getProperty("user.name"); method, but it return's user name and I'm looking for UID.

推荐答案

您可以执行id命令并读取结果.

you can execute id command and read result.

例如:

$ id -u jigar

输出:

1000

1000

您可以通过以下方式执行命令

you can execute command by

try {
    String userName = System.getProperty("user.name");
    String command = "id -u "+userName;
    Process child = Runtime.getRuntime().exec(command);

    // Get the input stream and read from it
    InputStream in = child.getInputStream();
    int c;
    while ((c = in.read()) != -1) {
        process((char)c);
    }
    in.close();
} catch (IOException e) {
}

这篇关于有没有办法使用Java在Linux机器上获取用户的UID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆