简体   繁体   English

在 Java 中运行 shell 脚本文件时出现 IoException

[英]IoException while running shell script file in Java

I need to run a shell script in java.我需要在 java 中运行 shell 脚本。 The script accepts two parameters as an argument 1st is the name and the second is the directory path.该脚本接受两个参数作为参数,第一个是名称,第二个是目录路径。

I'm using Windows as the operating system on my local machine.我在本地机器上使用 Windows 作为操作系统。

Below is the code I'm trying to run:下面是我试图运行的代码:

ProcessBuilder processBuilder = new ProcessBuilder("D:\\temp\\script\\create_script.sh", name, sourceDir);     
       try {
            Process process = processBuilder.start();
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println("Success!");
                System.out.println(output);
                System.exit(0);
            } else {
                //abnormal...
            }

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

The above code gives below error:上面的代码给出了以下错误:

2021-05-12 22:08:47.383  INFO 10600 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
java.io.IOException: Cannot run program "D:\temp\script\create_script.sh": CreateProcess error=193, %1 is not a valid Win32 application
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)

Can someone please help me with the missing part?有人可以帮我解决丢失的部分吗?

Appreciate all your help!感谢您的所有帮助! Thanks in advance!提前致谢!

If you are using Windows, the problem might be that you are trying to run a Linux shell script ( .sh ), hence Windows doesn't really appreciate that. If you are using Windows, the problem might be that you are trying to run a Linux shell script ( .sh ), hence Windows doesn't really appreciate that. You can either translate Linux shell script into Windows bash script or try to run your program again on a Linux OS, which I would recommend the latter. You can either translate Linux shell script into Windows bash script or try to run your program again on a Linux OS, which I would recommend the latter.

The below code worked for me!下面的代码对我有用!

ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("cmd.exe", "/c", "D:\\temp\\script\\create_script.sh);
        try {
            Process process = processBuilder.start();
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println("Success!");
                System.out.println(output);
                System.exit(0);
            } else {
                //abnormal...
            }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM