java 执行cmd windows_java 执行windows cmd命令

package com.auto.db.excel.export.tools;

import java.io.*;

import org.apache.log4j.Logger;

class StreamGobbler extends Thread {

InputStream is;

String type;

boolean _run = true;

StreamGobbler(InputStream is, String type) {

this.is = is;

this.type = type;

}

public void stopThread(boolean run) {

this._run = !run;

}

public void run() {

try {

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line = null;

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

System.out.println(type + "> " + line);

}

} catch (IOException ioe) {

ioe.printStackTrace();

}

}

}

public class GoodWindowsExec {

private Logger logger = Logger.getLogger(GoodWindowsExec.class);

private String imp_bat;

public GoodWindowsExec(){

this.imp_bat = GoodWindowsExec.class.getResource("export.bat").getPath().substring(1);

}

public void exec() throws Exception{

new BatUtils().creatBat();

String[] cmd = new String[]{"cmd.exe ","/C ",imp_bat};

Runtime rt = Runtime.getRuntime();

System.out.println("Execing " + cmd[0] + " " + cmd[1] + " "+ cmd[2]);

Process proc = rt.exec(cmd);

// any error message?

StreamGobbler errorGobbler = new StreamGobbler(

proc.getErrorStream(), "ERROR ");

// any output?

StreamGobbler outputGobbler = new StreamGobbler(

proc.getInputStream(), "OUTPUT ");

// kick them off

errorGobbler.start();

outputGobbler.start();

// any error???

//

int exitVal = proc.waitFor();

if(exitVal==1){

proc.destroy();

logger.info("-->>成功导出");

}

System.out.println("ExitValue:   " + exitVal);

}

public static void main(String[] args) throws Exception{

new GoodWindowsExec().exec();

}

}

==================

BatUtils.java创建一个临时bat文件供上面的方法调用

new BatUtils().creatBat(); ===============

package com.auto.db.excel.export.tools; import java.io.FileWriter; import java.io.IOException; import java.util.Map; import org.apache.log4j.Logger; import com.auto.db.util.ClassesUtil; import com.auto.db.util.DateUtils; import com.auto.db.util.FileUtil; public class BatUtils { private Logger logger = Logger.getLogger(BatUtils.class); /** * 该方法根据params参数负责生成bat文件 * @param params [0]=监听到的SQL脚本  */ public void creatBat() { FileWriter fw = null; try { Map map = ClassesUtil.readFile("db.properties"); FileUtil.createDirs(map.get("export_path"), true); String command = "@echo off\r\n"+ "echo 正在启动司法部“社区矫正”表导出任务\r\n"+ "exp "+map.get("datacenter_uname")+"/"+map.get("datacenter_pwd")+"@"+map.get("datacenter_host") +" tables=("+ map.get("export_tables")+") file="+map.get("export_path")+"\\sqjz_"+DateUtils.date2String("yyyyMMdd")+".dmp\r\n"+ "echo 数据表导出完毕\r\n"; String bat = BatUtils.class.getResource("").getPath().substring(1)+"export.bat"; logger.info("---->>export_cmd\r\n"+command); logger.info("---->>export_bat\r\n"+bat); fw = new FileWriter(bat); fw.write(command); } catch (IOException e) { e.printStackTrace(); System.exit(0); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new BatUtils().creatBat(); } }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值