Java执行shell脚本文件
2014-08-26 16:36
302 查看
Java调用shell脚本的问题,曾在之前的开发中遇到过,虽然调用方法很简单(貌似当时花了我蛮长时间),还是做个记录吧(年纪大了,易忘哈)~
配置文件中配置shell脚本文件执行命令,如:
java中获取配置项:
java执行脚本:
public static void executeScript(String command){
Process pcs = null ;
InputStreamReader is = null;
LineNumberReader in = null;
try{
Runtime rt = Runtime.getRuntime();
pcs = rt.exec(command);
is = new InputStreamReader(pcs.getInputStream());
in = new LineNumberReader(is);
String line;
pcs.waitFor();
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch(InterruptedException e){
System.err.println("processes was interrupted");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(pcs != null){
pcs.destroy();
}
}
}
ok,搞定收工!
配置文件中配置shell脚本文件执行命令,如:
sg_sam_command_model=sh ./sam_model.sh
java中获取配置项:
public boolean model(){ try { String command = SGConfig.getInstance().getItem("sg_sam_command_model"); ScriptUtil.executeScript(command); Thread.sleep(50000); } catch (InterruptedException e) { e.printStackTrace(); } return false; }
java执行脚本:
public static void executeScript(String command){
Process pcs = null ;
InputStreamReader is = null;
LineNumberReader in = null;
try{
Runtime rt = Runtime.getRuntime();
pcs = rt.exec(command);
is = new InputStreamReader(pcs.getInputStream());
in = new LineNumberReader(is);
String line;
pcs.waitFor();
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch(InterruptedException e){
System.err.println("processes was interrupted");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(pcs != null){
pcs.destroy();
}
}
}
ok,搞定收工!
相关文章推荐
- Java实现远程ssh执行shell脚本对文件打包下载
- JAVA执行bat文件和shell脚本文件
- JAVA执行bat文件和shell脚本文件
- JAVA执行bat文件和shell脚本文件
- 一个用shell写的执行java程序的脚本
- 如何在java环境下执行sql脚本文件
- Java调用Windows批处理或Linux脚本或可执行文件
- Java通过Telnet连接执行shell脚本 【转】
- 如何用Java后台执行SHELL脚本?
- 解决Linux下shell脚本文件执行失败问题
- shc gzexe 将shell脚本转换为二进制的可执行文件
- Java执行Shell脚本“No such file or directory”异常的可能原因
- 利用 Ant 的 SQL Task 来实现自己的 Java 执行 SQL 脚本文件的功能
- Java通过SSH2协议执行远程Shell脚本(ganymed-ssh2-build210.jar)
- linux0.11是如何加载并执行一个二进制可执行文件和shell脚本文件的?
- Java SSH远程执行Shell脚本实现
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值(如果有的话)
- Java通过SSH2协议执行远程Shell脚本
- shell 为jar文件,编写可执行启动脚本
- Java SSH远程执行Shell脚本实现