java与exe通信,Java 调用windows .exe

public static void runExe() throws IOException {

Process process = null;

try {

process = Runtime.getRuntime().exec(exePath);

//防止程序阻塞,需要把缓存中的内容消费掉

taskExecutor.execute(new InputStreamRunnable(process.getErrorStream(), filePath, 1));

taskExecutor.execute(new InputStreamRunnable(process.getInputStream(), null, 1));

process.waitFor();

LOGGER.info(url);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (process != null) {

process.destroy();

}

}

}

public class InputStreamRunnable implements Runnable {

private static final Logger LOGGER = LoggerFactory.getLogger(InputStreamRunnable.class);

private BufferedReader bReader = null;

private String fileUrl = null;

private Integer logOut = null;

public InputStreamRunnable(InputStream is, String url, Integer logOut) {

try {

bReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(is), "GBK"));

fileUrl = url;

this.logOut = logOut;

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

@Override

public void run() {

StringBuffer line = new StringBuffer();

try {

String str = "";

while ((str = bReader.readLine()) != null) {

line.append(str).append(System.lineSeparator());

}

if (logOut != null && logOut == 1) {

LOGGER.info(line.toString());

if (!StringUtils.isEmpty(fileUrl)) {

LOGGER.info( line.toString());

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (bReader != null) {

bReader.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值