需要Java代码示例,运行shell脚本[关闭]需要Java代码示例,运行shell脚本[关闭](N

2019-05-12 07:45发布

我试着用很多不同的例子,但它不工作。

我真的很感激一些示例Java代码来运行一个shell脚本。

Answer 1:

你需要调用Runtime.getRuntime()。EXEC(...)。 见一个非常广泛的例子 (不要忘了阅读前三页)。

记住的Runtime.exec 不是一个壳 ; 如果你想执行一个shell脚本命令行会看起来像

/bin/bash scriptname

也就是说,你需要的外壳二进制是完全合格的(虽然我怀疑/ bin中始终处于路径)。 你不能假设,如果

myshell> foo.sh

运行时,

Runtime.getRuntime.exec("foo.sh");

也跑因为你已经在第一个例子中正在运行的外壳,但不是在的Runtime.exec。

经过测试的例子(可在我的Linux机器(TM)),mosly剪切和过去从前面提到的文章 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ShellScriptExecutor {

    static class StreamGobbler extends Thread {
        InputStream is;

        String type;

        StreamGobbler(InputStream is, String type) {
            this.is = is;
            this.type = type;
        }

        public void run() {
            try {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null)
                    System.out.println(type + ">" + line);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("USAGE: java ShellScriptExecutor script");
            System.exit(1);
        }

        try {
            String osName = System.getProperty("os.name");
            String[] cmd = new String[2];
            cmd[0] = "/bin/sh"; // should exist on all POSIX systems
            cmd[1] = args[0];

            Runtime rt = Runtime.getRuntime();
            System.out.println("Execing " + cmd[0] + " " + cmd[1] );
            Process proc = rt.exec(cmd);
            // any error message?
            StreamGobbler errorGobbler = new StreamGobbler(proc
                    .getErrorStream(), "ERROR");

            // any output?
            StreamGobbler outputGobbler = new StreamGobbler(proc
                    .getInputStream(), "OUTPUT");

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}


Answer 2:

Shell脚本test.sh代码

#!/bin/sh
echo "good"

Java代码来执行shell脚本test.sh

      try {
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec(new String[]{"/bin/sh", "./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();
        }


文章来源: Need sample Java code to run a shellscript [closed]
标签: java shell