java调用shell脚本,并获得结果集的例子
2015-01-28 21:09
477 查看
/**
* 运行shell脚本
* @param shell 需要运行的shell脚本
*/
public static void execShell(String shell) {
try {
Runtime rt = Runtime.getRuntime();
rt.exec(shell);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 运行shell
*
* @param shStr
* 需要执行的shell
* @return
* @throws IOException
*/
public static List runShell(String shStr) throws Exception {
List<String> strList = new ArrayList();
Process process;
process = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c",shStr},null,null);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
strList.add(line);
}
return strList;
}
注:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流.
* 运行shell脚本
* @param shell 需要运行的shell脚本
*/
public static void execShell(String shell) {
try {
Runtime rt = Runtime.getRuntime();
rt.exec(shell);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 运行shell
*
* @param shStr
* 需要执行的shell
* @return
* @throws IOException
*/
public static List runShell(String shStr) throws Exception {
List<String> strList = new ArrayList();
Process process;
process = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c",shStr},null,null);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
strList.add(line);
}
return strList;
}
注:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流.
相关文章推荐
- java调用shell脚本,并获得结果集的例子
- java调用shell脚本,并获得结果集的例子
- java调用shell脚本,并获得结果集的例子
- java调用shell脚本,并获得结果集的例子
- Java代码中调用shell脚本和python脚本并获得输出结果(分为小数据量和大数据量)
- Java调用shell脚本并获得结果
- Java调用shell脚本并获得结果
- java调用shell脚本,并获得结果集
- Java调用shell脚本
- Java调用远程Shell脚本
- java直接调用python脚本的例子
- Java调用shell脚本要注意工作目录
- java调用shell脚本并传参
- 在java程序中如何调用linux的命令?如何调用shell脚本呢?
- JAVA 调用shell脚本
- java调用windows系统的批处理(.bat文件) 和 linux系统的shell脚本(.sh文件)
- 使用bat命令或shell脚本调用java程序
- java jsp调用shell(带参数)脚本并返回值
- ZZ Java远程调用shell脚本
- JAVA调用Shell脚本