简体   繁体   English

从 java 运行 shell 脚本并捕获返回值

[英]Running shell script from java and capturing return value

I a trying to run a shell script from java and want to capture the exit value from shell script in java. I a trying to run a shell script from java and want to capture the exit value from shell script in java. Below are the codes that I have tried along with their outputs以下是我尝试过的代码及其输出

Java Code 1: Java 代码1:

try {
            

    ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", "dir //pathOfSHFile//Test.sh");
                Process proc = processBuilder.start();
                proc.waitFor();
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line = "";
                String output = "";
                while ((line = in.readLine()) != null)
                {
                 output += line;
                } 
                System.out.println("### " + output);
            } catch (Throwable t) {
                t.printStackTrace();
            }

Test.sh file测试.sh 文件

#!/bin/sh
echo "good"

Output 1: Output 1:

###  Volume in drive C is OSDisk Volume Serial Number is *** Directory of C:\Data\Code07-12-2020  12:10                22 Test.sh 

Was expecting "good" as the output.期待“好”作为 output。

Java code 2: Java代码2:

try {
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec(new String[]{"C:\\Data\\Code", ".\\Test.sh"});

            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }

Output 2: Output 2:

java.io.IOException: Cannot run program "C:\Data\Code": CreateProcess error=5, Access is denied
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:621)
    at java.lang.Runtime.exec(Runtime.java:486)
    at Intersecttion.main(Intersecttion.java:53)
Caused by: java.io.IOException: CreateProcess error=5, Access is denied
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:444)
    at java.lang.ProcessImpl.start(ProcessImpl.java:140)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 3 more

I tried to give permission to all the folders but still same result.我试图授予所有文件夹的权限,但结果仍然相同。 I need to pass one argument as well to the script but not sure how to get that done and run the script as well and get exit value.我还需要将一个参数传递给脚本,但不确定如何完成并运行脚本并获取退出值。 Is there any better way to do this apart from above ones.除了上述方法之外,还有没有更好的方法来做到这一点。

Version 2 tries to execute the folder.版本 2 尝试执行该文件夹。 This should work:这应该有效:

        String path = "C:\\Data\\Code";
        String command = path + "\\Test.bat"; // Test.sh renamed
//      String command = "bash Test.sh"; // bash execution when installed on Windows
        
        Runtime rt = Runtime.getRuntime();
        
        Process pr = rt.exec(command, null, new File(path));

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = "";
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }

However, you may have to change your script to a batch file "Test.bat" instead as Windows will not natively execute shell scripts.但是,您可能必须将脚本更改为批处理文件“Test.bat”,因为 Windows 不会本机执行 shell 脚本。 You can also install bash and execute the bash command with the script as a parameter.您也可以安装 bash 并使用脚本作为参数执行bash命令。 I added that as a commented line.我将其添加为注释行。

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

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