简体   繁体   中英

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. Below are the codes that I have tried along with their outputs

Java Code 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

#!/bin/sh
echo "good"

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.

Java code 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:

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. 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. You can also install bash and execute the bash command with the script as a parameter. I added that as a commented line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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