package com.test.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.cnunisoft.util.TextLogger;
public class JavaShellUtil {
/**
* 调用方法为: JavaShellUtil javaShellUtil = new JavaShellUtil();
//参数为要执行的Shell命令,即通过调用Shell脚本sendKondorFile.sh将/temp目录下的tmp.pdf文件发送到192.168.1.200上
int success = JavaShellUtil.executeShell("sh /tmp/sendKondorFile.sh /temp tmp.pdf",false);
* @param shellCommand
* @return
* @throws IOException
*/
public static int executeShell(String shellCommand,boolean waitFor) throws IOException {
int success = 1;//默认是成功的
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
try {
stringBuffer.append(dateFormat.format(new Date()))
.append("exec Shell:").append(shellCommand)
.append(" \r\n");
Process pid = null;
//String[] cmd = { "/bin/sh", "-c", shellCommand };
// 执行Shell命令
pid = Runtime.getRuntime().exec(shellCommand);
if (pid != null) {
stringBuffer.append("pid:").append(pid.toString())
.append("\r\n");
if(waitFor){
pid.waitFor();
}
bufferedReader = new BufferedReader(new InputStreamReader(
pid.getInputStream()), 1024);
} else {
stringBuffer.append("no pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append(
"Shell result:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while (bufferedReader != null
&& (line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
}
} catch (Exception ioe) {
stringBuffer.append("Shell Exception:\r\n").append(ioe.getMessage())
.append("\r\n");
success = 0;
} finally {
if(bufferedReader!=null){
bufferedReader.close();
}
TextLogger.getLogger().warning(stringBuffer.toString());
}
return success;
}
/**
* 带返回值的shell调用
* 注意linux并不是所有的都有返回值的,要特殊处理ls是有的 ,但是ps -ef|grep就么有
* @param shellCommand
* @return
* @throws IOException
*/
public static String executeShellWidthReturn(String shellCommand){
//返回值
StringBuffer stringBufferReturn = new StringBuffer();
//记录日志
StringBuffer stringBufferLog = new StringBuffer();
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
InputStreamReader ir = null;
try {
stringBufferLog.append(dateFormat.format(new Date()))
.append("Will Exec Shell:").append(shellCommand)
.append(" \r\n");
Process process = null;
//String[] cmd = { "/bin/sh", "-c", shellCommand };
// 执行Shell命令
process = Runtime.getRuntime().exec(shellCommand);
// ir用于读取Shell的输出内容
ir=new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
if (process == null){
stringBufferLog.append("Exec Error\r\n");
}
stringBufferLog.append(dateFormat.format(new Date())).append(
"Exec Completed \r\nResult:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while ((line = input.readLine ()) != null) {
stringBufferLog.append(line).append("\r\n");
stringBufferReturn.append(line);
}
} catch (Exception ioe) {
stringBufferLog.append("Exec Exception:\r\n").append(ioe.getMessage())
.append("\r\n");
} finally {
if(ir!=null){
try {
ir.close();
} catch (IOException e) {
}
}
TextLogger.getLogger().warning(stringBufferLog.toString());
}
return stringBufferReturn.toString();
}
/**
* 调用方法为: JavaShellUtil javaShellUtil = new JavaShellUtil();
//参数为要执行的Shell命令,即通过调用Shell脚本sendKondorFile.sh将/temp目录下的tmp.pdf文件发送到192.168.1.200上
int success = JavaShellUtil.executeShell("sh /tmp/sendKondorFile.sh /temp tmp.pdf",false);
* @param shellCommand
* @return
* @throws IOException
*/
public static int executeCommand(String shellCommand,boolean waitFor) throws IOException {
int success = 1;//默认是成功的
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");
try {
stringBuffer.append(dateFormat.format(new Date()))
.append("exec Shell:").append(shellCommand)
.append(" \r\n");
Process pid = null;
String[] cmd = { "/bin/sh", "-c", shellCommand };
// 执行Shell命令
pid = Runtime.getRuntime().exec(cmd);
if (pid != null) {
stringBuffer.append("pid:").append(pid.toString())
.append("\r\n");
if(waitFor){
pid.waitFor();
}
bufferedReader = new BufferedReader(new InputStreamReader(
pid.getInputStream()), 1024);
} else {
stringBuffer.append("no pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append(
"Shell result:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while (bufferedReader != null
&& (line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
}
} catch (Exception ioe) {
stringBuffer.append("Shell Exception:\r\n").append(ioe.getMessage())
.append("\r\n");
success = 0;
} finally {
if(bufferedReader!=null){
bufferedReader.close();
}
TextLogger.getLogger().warning(stringBuffer.toString());
}
return success;
}
}
分享到:
相关推荐
主要介绍了Java调用linux shell脚本的方法,需要的朋友可以参考下
sshxcute 就是这样一个框架工具集,它基于 JSCH 构建,允许工程师利用Java 代码通过 SSH 连接远程批量执行 Linux/UNIX 系统上的命令或者脚本,同时加入了判断成功与否,取回输出等多种实用功能。sshxcute 不管是针对...
详细的linux shell脚本启动java代码类。
java连接linux,执行shell命令的工具类 sshxcute.jar
java调用linux系统命令的封装工具类。
因为我在linux环境下执行shell脚本时sqlldr命令和sqlplus命令是正常执行的,没有任何问题,但是在java代码中调用脚本时却报错,所有排除了其他原因,只可能是环境变量的问题, 于是我把oracle的所有环境变量直接复制...
java 执行linux命令源码,java调用shell脚本源码,java web发布war到tomcat,servlet文件上传,ajax文件上传。 java web 上传war包、停止、启动、发布tomcat。 命令修改成自己的目录即可使用。
JAVA操作linux服务器,本文用于启动shell脚本并返回数据,简单易懂,可随时修改,1.连接服务器。2.输入服务器地址。3.输入用户名和密码。4.输入要执行的命令。
该工具包可实现远程调用Shell脚本,可帮助您轻松实现java远程调用Shell脚本
使用runtime, 实现java客户端多次调用linux命令或者shell脚本,并返回对应结果或信息
基于sshConnect的linux服务器的shell命令调用,并获取命令返回结果的实例。亲测有效!
在Java程序中调用Unix/Linux主机上的Shell命令,并返回相应执行结果。
解决有java调用shell脚本需求的童鞋们,包含一些调用的测试方法,有问题可留言回复。
该部分java代码在linux下通过了测试验证,确实可以使用。
本人根据实际项目需求所开发的,SSH2远程执行Linux中Shell脚本Java小工具,亲测有效!里赋有已打好的程序jar包及工具使用说明,简单易用,很方便哟!
java远程登录Linux,并执行shell命令。
linux中java项目需要重启一般使用先找到进程杀掉进程,然后找到项目启动,整个过程不算复杂,但是每次都操作一遍太麻烦,我这里把这个过程整理成shell脚本,大家可以修改里面项目名称,每次直接执行这个命令重启项目...
在开发的过程中,要善于利用JAVA面向对象编程的优势,与Linux/Unix命令或Shell脚本的优势,并将二者相结合。
该工具是基于SSH2协议的实现,在使用它的过程中非常容易,只需要指定合法的用户名口令,或者授权认证文件,就可以创建到远程Linux主机的连接,在建立起来的会话中调用该Linux主机上的脚本文件,执行相关操作。
利用Runtime.execute方法,我们可以在Java程序中运行Linux的Shell脚本,或者执行其他程序