java 操作linux_java程序里调用linux命令

展开全部

以下62616964757a686964616fe78988e69d8331333335336532方法支持Linux和windows两个系统的命令行调用。还用到了apache的lang工具包commons-lang3-3.1.jar来判断操作系统类型、也用到了和log4j-1.2.16.jar来打印日志。至于rm -rf 是否能成功删除文件,可以手动去调用命令行试试。private String callCmd(String cmd) throws InterruptedException, UnHandledOSException, ExecuteException {

if(SystemUtils.IS_OS_LINUX){

try {

// 使用Runtime来执行command,生成Process对象

Process process = Runtime.getRuntime().exec(

new String[] { "/bin/sh", "-c", cmd });

int exitCode = process.waitFor();

// 取得命令结果的输出流

InputStream is = process.getInputStream();

// 用一个读输出流类去读

InputStreamReader isr = new InputStreamReader(is);

// 用缓冲器读行

BufferedReader br = new BufferedReader(isr);

String line = null;

StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {

System.out.println(line);

sb.append(line);

}

is.close();

isr.close();

br.close();

return sb.toString();

} catch (java.lang.NullPointerException e) {

System.err.println("NullPointerException " + e.getMessage());

logger.error(cmd);

} catch (java.io.IOException e) {

System.err.println("IOException " + e.getMessage());

}

throw new ExecuteException(cmd + "执行出错!");

}

if(SystemUtils.IS_OS_WINDOWS){

Process process;

try {

//process = new ProcessBuilder(cmd).start();

String[] param_array = cmd.split("[\\s]+");

ProcessBuilder pb = new ProcessBuilder(param_array);

process = pb.start();

/*process=Runtime.getRuntime().exec(cmd);*/

int exitCode = process.waitFor();

InputStream is = process.getInputStream();

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line;

StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {

System.out.println(line);

sb.append(line);

}

is.close();

isr.close();

br.close();

return sb.toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

throw new ExecuteException(cmd + "执行出错!");

}

throw new UnHandledOSException("不支持本操作系统");

}

以下是测试代码public class ExecuteCmdCommand {

static Logger logger = Logger.getLogger(ExecuteCmdCommand.class.getName());

public static void main(String[] args) throws InterruptedException, UnHandledOSException, ExecuteException {

// TODO Auto-generated method stub

ExecuteCmdCommand executeCmdCommand = new ExecuteCmdCommand();

if(SystemUtils.IS_OS_LINUX){

executeCmdCommand.callCmd("ls -al");

}else if(SystemUtils.IS_OS_WINDOWS){

executeCmdCommand.callCmd("dir");

}else{

System.out.println("不支持的OS");

}

}

public class ExecuteException extends Exception {

public ExecuteException() {

super();

// TODO Auto-generated constructor stub

}

public ExecuteException(String arg0) {

super(arg0);

// TODO Auto-generated constructor stub

}

}

public class UnHandledOSException extends Exception {

public UnHandledOSException() {

super();

// TODO Auto-generated constructor stub

}

public UnHandledOSException(String arg0) {

super(arg0);

// TODO Auto-generated constructor stub

}

}

private String callCmd(String cmd) throws InterruptedException, UnHandledOSException, ExecuteException {

if(SystemUtils.IS_OS_LINUX){

try {

// 使用Runtime来执行command,生成Process对象

Process process = Runtime.getRuntime().exec(

new String[] { "/bin/sh", "-c", cmd });

int exitCode = process.waitFor();

// 取得命令结果的输出流

InputStream is = process.getInputStream();

// 用一个读输出流类去读

InputStreamReader isr = new InputStreamReader(is);

// 用缓冲器读行

BufferedReader br = new BufferedReader(isr);

String line = null;

StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {

System.out.println(line);

sb.append(line);

}

is.close();

isr.close();

br.close();

return sb.toString();

} catch (java.lang.NullPointerException e) {

System.err.println("NullPointerException " + e.getMessage());

logger.error(cmd);

} catch (java.io.IOException e) {

System.err.println("IOException " + e.getMessage());

}

throw new ExecuteException(cmd + "执行出错!");

}

if(SystemUtils.IS_OS_WINDOWS){

Process process;

try {

//process = new ProcessBuilder(cmd).start();

String[] param_array = cmd.split("[\\s]+");

ProcessBuilder pb = new ProcessBuilder(param_array);

process = pb.start();

/*process=Runtime.getRuntime().exec(cmd);*/

int exitCode = process.waitFor();

InputStream is = process.getInputStream();

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line;

StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {

System.out.println(line);

sb.append(line);

}

is.close();

isr.close();

br.close();

return sb.toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

throw new ExecuteException(cmd + "执行出错!");

}

throw new UnHandledOSException("不支持本操作系统");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值