【Java】Java调用shell脚本

在这里插入图片描述

1.概述

java 执行shell

2.utils类

package com.shell.java;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class JaveShellUtil {

    public static int ExecCommand(String command) {
        int retCode = 0;
        try {
            Process process = Runtime.getRuntime().exec(  command,null,null);
            retCode = process.waitFor();
            ExecOutput(process);
        } catch (Exception e) {
            e.printStackTrace();
            retCode = -1;
        }
        return retCode;
    }

    public static boolean ExecOutput(Process process) throws Exception {
        if (process == null) {
            return false;
        } else {
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line;
            String output = "";
            while ((line = input.readLine()) != null) {
                output += line + "\n";
            }
            input.close();
            ir.close();
            if (output.length() > 0) {
                System.out.println(output);
            }
        }
        return true;
    }


    public static int execuCommonWithParam(String command) {
        int retCode = 0;
        try {
            Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", command }, null, null);
            retCode = process.waitFor();
            ExecOutput(process);
        } catch (Exception e) {
            retCode = -1;
        }
        return retCode;
    }
}

测试类如下

package com.shell.java;

import org.junit.Test;

/**
 * 参考:https://blog.csdn.net/zhoudado921/article/details/80847182
 */
public class JaveShellUtilTest {

    /**
     * 不传参测试
     */
    @Test
    public void execCommand() {
        String command= "chmod 777 /opt/workspace/demo.sh ";
        //需要传递的就是shell脚本的位置
        int retCode=JaveShellUtil.ExecCommand(command);
        System.out.println(retCode+" retCode");
        if(retCode==0){
            System.out.println("success.....");
        }else{
            System.out.println("error.....");
        }
    }

    /**
     * 传参测试
     */
    @Test
    public void execCommand1() {
        String command= "/bin/sh /opt/workspace/demo.sh lcc xjj ";
        //需要传递的就是shell脚本的位置
        int retCode=JaveShellUtil.ExecCommand(command);
        System.out.println(retCode+" retCode");
        if(retCode==0){
            System.out.println("success.....");
        }else{
            System.out.println("error.....");
        }
    }

    /**
     * 传参测试
     */
    @Test
    public void execuCommonWithParamTest() {
        String command= "/opt/workspace/demo.sh lcc xjj ";
        //需要传递的就是shell脚本的位置
        int retCode=JaveShellUtil.execuCommonWithParam(command);
        System.out.println(retCode+" retCode");
        if(retCode==0){
            System.out.println("success.....");
        }else{
            System.out.println("error.....");
        }
    }



}
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页