如何在java

时间:2018-03-22 16:24:13

标签: java bash shell

您好我正在尝试在java中运行shell脚本,其中脚本包含sudo apt-get update和echo line,如下所示

#!/bin/bash

sudo apt-get update
sudo apt-get install nautilus
echo "hello"

这是我的java代码,

public class Main {

    public static void main(String[] args) {

        String command = "/home/anon/Script.sh ";

        try {
                Process process = Runtime.getRuntime().exec(command);

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line + "\n");
                }

                reader.close();

            }
        catch (IOException e) {
            e.printStackTrace();
        }

    }
}

但是这只是打印echo语句而不是apt-get install甚至是sudo的密码。无论如何要解决它吗?请帮忙

0 个答案:

没有答案