-
java运行外部程序
2018-05-18 19:01:54如何用java代码运行外部程序?package com.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; ...如何用java代码运行外部程序?
package com.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; public class EXERunner { public void runExe(String exePath) { try { Process process = Runtime.getRuntime().exec(exePath); // 注:process进程的输出流 相对于本进程就是输入流 Thread errorHandler = new Thread(new ErrorIOHandler(process.getErrorStream())); Thread normalHandler = new Thread(new NormalIOHandler(process.getInputStream())); Thread cmdHandler = new Thread(new CmdIOHandler(process.getOutputStream())); errorHandler.start(); normalHandler.start(); cmdHandler.start(); process.waitFor(); // 等待process进程的输出流都被处理 int exitValue = process.exitValue(); if (exitValue == 0) { System.out.println("进程正常退出"); } else { System.out.println("进程异常退出"); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new EXERunner().runExe("C:\\Windows\\System32\\ipconfig.exe"); } } // 处理外部程序的错误输出流 class ErrorIOHandler implements Runnable { private InputStream error; public ErrorIOHandler(InputStream error) { this.error = error; } @Override public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(error)); String line = ""; try { while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } // 处理外部程序的正常输出流 class NormalIOHandler implements Runnable { private InputStream normal; public NormalIOHandler(InputStream normal) { this.normal = normal; } @Override public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(normal)); String line = ""; try { while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } // 给外部程序发送命令 class CmdIOHandler implements Runnable { private OutputStream out; public CmdIOHandler(OutputStream out) { this.out = out; } @Override public void run() { try { String line = ""; BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//控制台读取流 while (true) { line = br.readLine();//从控制台读取一行 if (line.equals("")) { break; } else { out.write((line + "\r\n").getBytes());//发送命令给外部程序 } out.flush(); } br.close(); } catch (IOException e) { e.printStackTrace(); } } }
-
java运行外部程序_java运行外部程序
2021-03-06 15:17:41如何用java代码运行外部程序?package com.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;public ...如何用java代码运行外部程序?package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class EXERunner {
public void runExe(String exePath) {
try {
Process process = Runtime.getRuntime().exec(exePath);
// 注:process进程的输出流 相对于本进程就是输入流
Thread errorHandler = new Thread(new ErrorIOHandler(process.getErrorStream()));
Thread normalHandler = new Thread(new NormalIOHandler(process.getInputStream()));
Thread cmdHandler = new Thread(new CmdIOHandler(process.getOutputStream()));
errorHandler.start();
normalHandler.start();
cmdHandler.start();
process.waitFor(); // 等待process进程的输出流都被处理
int exitValue = process.exitValue();
if (exitValue == 0) {
System.out.println("进程正常退出");
} else {
System.out.println("进程异常退出");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new EXERunner().runExe("C:\\Windows\\System32\\ipconfig.exe");
}
}
// 处理外部程序的错误输出流
class ErrorIOHandler implements Runnable {
private InputStream error;
public ErrorIOHandler(InputStream error) {
this.error = error;
}
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(error));
String line = "";
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 处理外部程序的正常输出流
class NormalIOHandler implements Runnable {
private InputStream normal;
public NormalIOHandler(InputStream normal) {
this.normal = normal;
}
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(normal));
String line = "";
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 给外部程序发送命令
class CmdIOHandler implements Runnable {
private OutputStream out;
public CmdIOHandler(OutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//控制台读取流
while (true) {
line = br.readLine();//从控制台读取一行
if (line.equals("")) {
break;
} else {
out.write((line + "\r\n").getBytes());//发送命令给外部程序
}
out.flush();
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-
Java运行外部程序
2018-03-19 09:46:00可执行类 Runtime.getRuntime().exec("C:\\Users\\xu\\Desktop\\stop.exe"); 链接类 ProcessBuilder pb = new ProcessBuilder("cmd","...C:\\Users\\xu\\Desktop\\stop.cmd.lnk"...可执行类
Runtime.getRuntime().exec("C:\\Users\\xu\\Desktop\\stop.exe");
链接类
ProcessBuilder pb = new ProcessBuilder("cmd","/c","C:\\Users\\xu\\Desktop\\stop.cmd.lnk"); Process process = pb.start();
-
Java运行外部程序问题解决
2012-12-11 14:13:33Java运行外部程序问题解决 1、现象:问题描述 关于java通过Runtime.getRuntime.exec()运行外部命令抛异常的解决方法在java程序中频繁调用Runtime.getRuntime.exec("command")来运行一个外部命令。发现运行一段...Java运行外部程序问题解决
1、现象:问题描述
关于java通过Runtime.getRuntime.exec()运行外部命令抛异常的解决方法在java程序中频繁调用Runtime.getRuntime.exec("command")来运行一个外部命令。发现运行一段时间以后会抛出异常: IOException:too many files open 检查程序,凡是使用的流都关闭了,还是抛异常。
2、关键过程:根本原因分析
后来上网查了一把,发现在Solaris下使用: Process = Runtime.exec("command") 运行一个外部程序后,Process对象的三个流(InputStream,OutputStream,ErrorStream) 即使你不使用,也要人为地get出来后显式的调用close()关闭。否则相关的文件句柄不会释放。我在使用的时候只用到了OutputStream,也就只关闭了OutputStream, 剩下的两个流我没有用到,就没有获得,当然也没有关闭,于是积累下来,过一段时间就打开了过多的文件句柄,抛异常了。
3、结论:解决方案及效果加入以下语句后解决问题:
process.getInputStream().close();
process.getOutputStream().close();
process.getErrorStream().close();
4、经验总结:预防措施和规范建议
Process对象的三个流(InputStream,OutputStream,ErrorStream) 都需要关闭。 -
apache 运行 java_Java运行外部程序(Apache Commons Exec)
2021-03-06 21:20:01后来在网上找到开源的Java调用外部程序类库Apache Commons Exce,这个类库提供非堵塞方法调用外部程序。Commons Exec对调用外部程序进行了封装,仅仅须要少量代码就可以实现外部程序调用。如运行命令"AcroRd32.exe /... -
java apache exec_Java运行外部程序(Apache Commons Exec)
2021-03-15 18:02:27后来在网上找到开源的Java调用外部程序类库Apache Commons Exce,这个类库提供非堵塞方法调用外部程序。Commons Exec对调用外部程序进行了封装,仅仅须要少量代码就可以实现外部程序调用。如运行命令"AcroRd32.exe /... -
Java运行外部程序(Apache Commons Exec)
2019-09-24 19:19:01之前使用Runtime.getRuntime().exec调用外部程序。在Tomcat下会有当前线程一直等待的现象。当时为了解决问题,使用新建线程接收外部程序的输出信息。...后来在网上找到开源的Java调用外部程序类库Apache Commons ... -
java process 中断_从Java运行外部程序,读取输出,允许中断
2021-03-11 16:30:08这怎么样(看看它如何在jcabi-heroku-maven-plugin中运行 ): /** * Wait for the process to stop, logging its output in parallel. * @param process The process to wait for * @return Stdout produced by the ... -
Java 运行外部程序。
2007-01-05 10:35:001、运行外部程序: Runtime.getRuntime().exec("notepad");2、运行系统内部命令: Runtime.getRuntime().exec("command.com /c dir"); -
关于Java运行外部程序时的Process和Runtime类
2016-02-26 01:09:20在写Java应用程序的时候,会碰到在程序中调用另一个可执行程序或系统命令,这时可通过组合使用Java提供的Runtime类(java/lang/Runtime.java)和Process类(java/lang/Process.java)的API来实现。典型的模式如下:... -
Java运行外部程序-_-!...
2011-12-18 15:18:00// 几行代码我就不解释啦!...import java.lang.Runtime; import java.io.IOException; public class runCmd { public static void main(String[] args) { try{ Runtime.getRuntime().exec("cmd.ex... -
java程序里运行外部程序
2020-03-26 21:03:17使用Runtime.getRuntime().exec()方法可以在java程序里运行外部程序。 1. exec(String command) 2. exec(String command, String envp[], File dir) 3. exec(String cmd, String envp[]) 4. exec(String ... -
用JAVA调用外部程序并截出程序运行结果
2011-08-24 14:23:19JAVA调用外部程序 用JAVA调用外部程序并截出程序运行结果 -
java 调用外部exe_java调用外部程序
2021-02-28 13:02:45本文总结了Java启动外部程序的常用方式,包括启动DOS内部命令、打开系统关联文件、执行DOS应用程序等,让java应用更加灵活。首先介绍启动外部程序的基本方法。1、简单的启动外部程序:记事本被打开了是吧。2、带参数... -
运行外部Java程序
2020-04-03 22:39:14如何运行外部Java文件,有三种方式。 首先先写一个java文件到c盘中: public class Demo { public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, ... -
Java编程使用Runtime和Process类运行外部程序的方法
2020-08-29 16:45:47主要介绍了Java编程使用Runtime和Process类运行外部程序的方法,结合实例形式分析了java使用Runtime.getRuntime().exec()方法运行外部程序的常见情况与操作技巧,需要的朋友可以参考下 -
JAVA运行外部exe程序问题
2012-05-25 17:39:58exe程序文件夹:D:/res/report/Release/WordReportPrint.exe exe程序是一个C#写的windows应用程序,Release文件夹下面还有很多程序运行需要的dll。WordReportPrint.exe是一个查询数据库打印word报表的程序,需要传入... -
java 调用其他程序_java调用外部程序的方法
2021-03-06 22:02:03转自:http://gundumw100.iteye.com/blog/4386961 java调用外部程序的方法在一个java应用中,可能会遇到这样的需求,就是需要调用一些外部的应用做一些处理,比如调用excel,然后在继续程序的运行。下面就开始进入... -
Java中使用Runtime和Process类运行外部程序
2019-04-22 14:30:52使用Runtime.getRuntime().exec()方法可以在java程序里运行外部程序。 exec(String command) exec(String command, String envp[], File dir) exec(String cmd, String envp[]) exec(String cmdarray[]) exec... -
java runtime 实现_Java中使用Runtime和Process类运行外部程序
2021-02-28 08:19:29关键字 Java 外部程序 CMD 进程 调用 Process最近接触一个需求,是利用Java调用本地命令行程序,并希望Java程序能与该命令行程序进行交互,Java对该程序的操作如同在终端中对程序的操纵一样。在技术调研的过程中,... -
通过Java运行一个外部程序
2008-10-23 16:33:28通过Java运行一个外部程序 public class hpboot{ public static void main(String[] args) { String command = "C:\\Program Files\\Outlook Express\\msimn.exe"; try{ Runtime.getRuntime().exec(command);... -
java调用外部程序的方法
2019-09-29 11:45:171 java调用外部程序的方法 在一个java应用中,可能会遇到这样的需求,就是需要调用一些外部的应用做一些处理,比如调用excel,然后在继续程序的运行。 下面就开始进入java调用外部程序的一些演示,让... -
java调用外部程序
2012-09-13 11:34:28Java中使用Runtime和Process类运行外部程序 使用Runtime.getRuntime().exec()方法可以在java程序里运行外部程序。 1. exec(String command) 2. exec(String command, String envp[], File dir) 3. exec...