【问题标题】:Java execute shell command - error [Cannot run program "sh": CreateProcess error=2, The system cannot find the file specified]Java 执行 shell 命令 - 错误 [Cannot run program "sh": CreateProcess error=2, The system cannot find the file specified]
【发布时间】:2020-04-22 15:40:16
【问题描述】:

我想使用 shell 命令从 java 执行 python 脚本 文件位置:d:/python/test.py

下面是我的java程序

def callstme(){
        Process p
        try {

            List<String> cmdList = new ArrayList<String>();
            cmdList.add("sh");
            cmdList.add("d:/python/test.py");
            ProcessBuilder pb = new ProcessBuilder(cmdList);
            p = pb.start();

            p.waitFor();
            BufferedReader reader=new BufferedReader(new InputStreamReader(
             p.getInputStream()));
            String line;
            while((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

执行时出现错误

"java.io.IOException: Cannot run program "sh": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)

【问题讨论】:

    标签: java python shell


    【解决方案1】:

    您的文件名打印错误?

    d:/python/test.pyy

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 2022-01-24
      • 2020-01-24
      • 1970-01-01
      • 2022-12-22
      相关资源
      最近更新 更多