【问题标题】:How to run sudo command from java program using eclipse IDE?如何使用 Eclipse IDE 从 Java 程序运行 sudo 命令?
【发布时间】:2012-05-24 08:44:34
【问题描述】:

我想使用 Eclipse IDE 从 java prog 运行 sudo 命令,但它没有运行,不要求输入密码,也没有在控制台中显示任何输出消息,请告诉我 我的编是

public class JavaCommand {
    public static void main(String args[]) throws IOException, InterruptedException {
        String command="sudo cat /etc/sudoers";
        Process myProcess =null;
        try {
            Runtime runtime=Runtime.getRuntime();
            File file=new File("/home/users/admin/temp");
            myProcess = runtime.exec(command,null,file);
            myProcess.waitFor();
            InputStreamReader myIStreamReader = new InputStreamReader(myProcess.getInputStream());
            BufferedReader br=new BufferedReader(myIStreamReader);
            String line;
            while((line=br.readLine())!=null){
                System.out.println(line);
            }
        } catch (IOException anIOException) {
            System.out.println(anIOException);
        }
    }
}

【问题讨论】:

标签: java linux eclipse ubuntu


【解决方案1】:

Process.waitFor 是一个阻塞调用。

在你做myProcess.waitFor之前,你需要创建线程来处理程序i/o。

【讨论】:

    猜你喜欢
    • 2011-04-03
    • 2015-11-14
    • 2017-03-29
    • 1970-01-01
    • 2017-02-08
    • 2020-11-19
    • 1970-01-01
    • 2015-08-07
    • 2012-08-09
    相关资源
    最近更新 更多