无法执行子进程“'scriptName.sh”(无此文件或目录)

时间:2018-08-10 09:31:41

标签: java shell terminal process runtime

String command= "/usr/bin/gnome-terminal.wrapper -e 'startDemonstrator.sh; bash'";
File workDir = new File("/home/malju/Desktop");
Process pr = Runtime.getRuntime().exec(command, null, workDir);

执行此行代码后,出现上述错误。我的脚本位于“桌面”文件夹中。我已经尝试添加./startDemonstrator和完整路径。我总是收到上面的错误。原因可能是什么?

我只是想在终端打开后打开一个sh脚本。

1 个答案:

答案 0 :(得分:0)

首先尝试如下:-

String command= "/home/malju/Desktop/startDemonstrator.sh";
Process pr = Runtime.getRuntime().exec(command);
p.waitFor();

如果仍然无法使用,请尝试使用以下方法使用ProcessBuilder。

    String result = "";
    String[] command = {"/home/malju/Desktop/startDemonstrator.sh"};
    ProcessBuilder process = new ProcessBuilder(command); 
    Process p ;
    try {
        p = process.start();
        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader reader =  new BufferedReader(isr);
        StringBuilder builder = new StringBuilder();
        String line = null;
        while ( (line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
        }
        result = builder.toString();
    }
    catch (IOException e)
    {   System.out.print("error");
        e.printStackTrace();
    }