需要示例 Java 代码来运行 shellscript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/609762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need sample Java code to run a shellscript
提问by
I tried with many different examples, but it is not working.
我尝试了许多不同的例子,但它不起作用。
I would really appreciate some sample Java code to run a shell script.
我非常感谢一些示例 Java 代码来运行 shell 脚本。
回答by extraneon
You need Runtime.getRuntime().exec(...). See a very extensive example(don't forget to read the first three pages).
你需要 Runtime.getRuntime().exec(...)。查看一个非常广泛的示例(不要忘记阅读前三页)。
Keep in mind that Runtime.exec is not a shell; if you wish to execute a shell script your command line would look like
请记住, Runtime.exec不是 shell;如果你想执行一个 shell 脚本,你的命令行看起来像
/bin/bash scriptname
That is, the shell binary you need is fully qualified (although I suspect that /bin is always in the path). You can not assume that if
也就是说,您需要的 shell 二进制文件是完全限定的(尽管我怀疑 /bin 总是在路径中)。你不能假设如果
myshell> foo.sh
runs,
跑,
Runtime.getRuntime.exec("foo.sh");
also runs as you are already in a running shell in the first example, but not in the Runtime.exec.
在第一个示例中,也像您已经在正在运行的 shell 中一样运行,但不在 Runtime.exec 中。
A tested example (Works on My Linux Machine(TM)), mosly cut-and-past from the previously mentioned article:
一个经过测试的示例(适用于我的 Linux 机器(TM)),来自前面提到的文章的大部分内容:
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();
}
}
}
回答by Suniel
Shell Script test.sh code
Shell脚本test.sh代码
#!/bin/sh
echo "good"
Java Code to execute shell script test.sh
执行shell脚本test.sh的Java代码
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();
}