当前位置: 移动技术网 > IT编程>开发语言>Java > Java调用Shell命令的方法

Java调用Shell命令的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java调用shell命令的方法。分享给大家供大家参考。具体如下:

近日项目中有这样一个需求:系统中的外币资金调度完成以后,要将调度信息生成一个txt文件,然后将这个txt文件发送到另外一个系统(kondor)中。生成文件自然使用outputstreamwirter了,发送文件有两种方式,一种是用写个一个类似于ftp功能的程序,另外一种就是使用java来调用shell,在shell中完成文件的发送操作。我们选择后一种,即当完成外币资金的调度工作后,用java的outputstreamwriter来生成一个txt文件,然后用java来调用shell脚本,在shell脚本中完成ftp文件到kondor系统的工作。

以下为java程序javashellutil.java:

import java.io.bufferedreader;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.io.outputstreamwriter;
import java.text.dateformat;
import java.text.simpledateformat;
import java.util.date;
public class javashellutil {
//基本路径
private static final string basepath = "/tmp/";
//记录shell执行状况的日志文件的位置(绝对路径)
private static final string executeshelllogfile = basepath + "executeshell.log";
//发送文件到kondor系统的shell的文件名(绝对路径)
private static final string sendkondorshellname = basepath + "sendkondorfile.sh";
public int executeshell(string shellcommand) throws ioexception {
int success = 0;
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("准备执行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("进程号:").append(pid.tostring()).append("/r/n");
//bufferedreader用于读取shell的输出内容 bufferedreader = new bufferedreader(new inputstreamreader(pid.getinputstream()), 1024);
pid.waitfor();
} else {
stringbuffer.append("没有pid/r/n");
}
stringbuffer.append(dateformat.format(new date())).append("shell命令执行完毕/r/n执行结果为:/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命令时发生异常:/r/n").append(ioe.getmessage()).append("/r/n");
} finally {
if (bufferedreader != null) {
outputstreamwriter outputstreamwriter = null;
try {
bufferedreader.close();
//将shell的执行情况输出到日志文件中
outputstream outputstream = new fileoutputstream(executeshelllogfile);
outputstreamwriter = new outputstreamwriter(outputstream, "utf-8");
outputstreamwriter.write(stringbuffer.tostring());
} catch (exception e) {
e.printstacktrace();
} finally {
outputstreamwriter.close();
}
}
success = 1;
}
return success;
}
}

以下是shell脚本sendkondorfile.sh,该shell脚本的作用是ftp文件到指定的位置:

#!/bin/sh
#日志文件的位置
logfile="/opt/fms2_kondor/sendkondorfile.log"
#kondor系统的ip地址,会将生成的文件发送到这个地址
kondor_ip=192.168.1.200
#ftp用户名
ftp_username=kondor
#ftp密码
ftp_password=kondor
#要发送的文件的绝对路径
filepath=""
#要发送的文件的文件名
filename=""
#如果shell命令带有参数,则将第一个参数赋给filepath,将第二个参数赋给filename
if [ $# -ge "1" ]
then
filepath=$1
else
echo "没有文件路径"
echo "没有文件路径/n" >
>
$logfile
return
fi
if [ $# -ge "2" ]
then
filename=$2
else
echo "没有文件名"
echo "没有文件名/n" >
>
$logfile
return
fi
echo "要发送的文件是 ${filepath}/${filename}"
cd ${filepath}
ls $filename
if (test $? -eq 0)
then
echo "准备发送文件:${filepath}/${filename}"
else
echo "文件 ${filepath}/${filename} 不存在"
echo "文件 ${filepath}/${filename} 不存在/n" >
>
$logfile
return
fi
ftp -n ${kondor_ip} <
<
_end
user ${ftp_username} ${ftp_password}
asc
prompt
put $filename
bye
_end
echo "`date +%y-%m-%d' '%h:%m:%s` 发送了文件 ${filepath}/${filename}"
echo "`date +%y-%m-%d' '%h:%m:%s` 发送了文件 ${filepath}/${filename}/n" >
>
$logfile

调用方法为:

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");

希望本文所述对大家的java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网