来自java的shell脚本异常

时间:2014-04-09 10:29:45

标签: java shell

我正在尝试从shell脚本sh文件运行的Spring批处理作业。以下是我的代码。 我想从java代码中捕获shell脚本异常或错误。

String file = "script.sh";
File shellScriptFile= new File(file);
Runtime run = Runtime.getRuntime();
ProcessBuilder processBuilder = new ProcessBuilder(shellScriptFile.getAbsolutePath(), "Argument-ONE" , "Argument-TWO");
Process process = processBuilder.start();

非常感谢。

3 个答案:

答案 0 :(得分:1)

脚本或外部命令通常会设置错误代码。您可以使用Process#waitFor()获取它,并根据您的需要处理返回的值。返回值0通常表示no errors

Process process = processBuilder.start();
int exitCode = process.waitFor();
switch(exitCode) {
    case 0:
        // everything is ok
        break;
    case 1:
        // handle
        break;
    // and so on ...
    default:
        // default
}

答案 1 :(得分:1)

Peharps你可以使用这段代码:

    public static void main(String[] args) {    
    String[] cmd = { "/bin/sh", "-c", "script.sh" };
    BufferedReader bri = null, bre = null;
    int exitC = 0;
    try {
        Process p = Runtime.getRuntime().exec(cmd);
        exitC = p.waitFor();
        bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
        bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String line = "";
        while ((line = bri.readLine()) != null) {
            System.out.println(line);               
        }
        while ((line = bre.readLine()) != null) {
            System.out.println(line);
        }
        bri.close();
        bre.close();
    } catch (Exception e) {
        e.printStackTrace();
    } 
    System.out.println("Exit Code: "+ exitC);
}

或者如果您想使用processBuilder.start()

    String file = "script.sh";
    File shellScriptFile= new File(file);
    Runtime run = Runtime.getRuntime();
    ProcessBuilder processBuilder = new ProcessBuilder(shellScriptFile.getAbsolutePath(), "Argument-ONE" , "Argument-TWO");
    try {
        Process process = processBuilder.start();
        int exitC = process.waitFor();
        BufferedReader bri = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String inputLine = "";
        while ((inputLine = bri.readLine()) != null) {
            System.out.println(inputLine);
        }
        BufferedReader bre = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String errorLine = "";
        while ((errorLine = bre.readLine()) != null) {
            System.out.println(errorLine);
        }
        System.out.println("Exit Code:" + exitC);
    } catch (IOException e){
        //captured Exception Here
        //e.printStackTrace();
    }

不同之处在于ProcessBuilder.start()检查它是否是有效的操作系统命令,这些有效命令是系统相关的

答案 2 :(得分:1)

您可以使用Apache commons exec library

示例:

package testShellScript;

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;

public class TestScript {
    int iExitValue;
    String sCommandString;

    public void runScript(String command){
        sCommandString = command;
        CommandLine oCmdLine = CommandLine.parse(sCommandString);
        DefaultExecutor oDefaultExecutor = new DefaultExecutor();
        oDefaultExecutor.setExitValue(0);
        try {
            iExitValue = oDefaultExecutor.execute(oCmdLine);
        } catch (ExecuteException e) {
            // TODO Auto-generated catch block
            System.err.println("Execution failed.");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.err.println("permission denied.");
            e.printStackTrace();
        }
    }

    public static void main(String args[]){
        TestScript testScript = new TestScript();
        testScript.runScript("sh /root/Desktop/testScript.sh");
    }
}