您的位置:首页 > 编程语言 > Java开发

JAVA如何调用exe或者批处理文件

2010-02-22 13:56 676 查看
/**
* JavaExec.java version 1.0    Feb 22, 2010
*/

import java.lang.Runtime;
import java.lang.Process;
import java.io.InputStreamReader;
import java.lang.Exception;
import java.io.*;
public class JavaExec {
/**
* @param args
* @author Eric Yang
* @
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

JavaExec javaExec = new  JavaExec();
javaExec.testProcess();
}

public void testProcess()
{
try
{

String home="c:/process";
String command = "bfimport.bat -U yq -P qq -x bfimport_71.xml  -p";
String[] cmd = new String[] { "cmd.exe", " ", "bfimport.bat" };
File dir = new File(home);
//Process process = Runtime.getRuntime().exec(command,null, dir);
Process process = Runtime.getRuntime().exec(command);

/*TestInputStream errorStream = new TestInputStream(process
.getErrorStream());
errorStream.start();*/
InputStream  inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
//int exitValue = process.waitFor();
//System.out.println("Return Value:" + exitValue);
process.getOutputStream().close();
}
catch(IOException e)
{
System.out.println("IOException ");


 

 

 

修改其中的command就可以了

上面的例子确实可以获得一些输出,但是有很多情况也无法获取,再参考很多实例之后,下面的例子最强大,几乎可以捕获所有类型的命令行输出。

 

/**
* JavaExec.java version 1.0    Feb 22, 2010
*/

import java.lang.Runtime;
import java.lang.Process;
import java.io.InputStreamReader;
import java.lang.Exception;
import java.io.*;

class StreamDrainer implements Runnable {
private InputStream ins;

public StreamDrainer(InputStream ins) {
this.ins = ins;
}

public void run() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}

}

}

public class JavaExec {
/**
* @param args
*
*
* @author Eric Yang
* @
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("/n Usage:java JavaExec command or  java JavaExec");

JavaExec javaExec = new  JavaExec();
if (args.length >= 1) {
javaExec.testProcess(args[0]);
}
else{
javaExec.testProcess(null);
}

}

public void testProcess(String userInputCmd)
{
try
{

String home="C:/jas_utilities";
String command = "bfimport.bat -U root -P root -x C:/etc/bfimport_71.xml  -p";
String[] cmd = new String[] { "bfimport.bat",
"-U ","root ",  "-P"," root ", "-x ", "C:/fvt/7120modify/src/bfimport_71.xml", " -p" };
File dir = new File(home);
//Process process = Runtime.getRuntime().exec(command,null, dir);
Process process = null;
if(userInputCmd == null){
process = Runtime.getRuntime().exec(command,null, dir);
System.out.println("default command");
}
else{
process = Runtime.getRuntime().exec(userInputCmd);
System.out.println("user input command");
}

System.out.println("-----------------output informaiton------");
new Thread(new StreamDrainer(process.getInputStream())).start();
System.out.println("-----------------output informaiton------/n");

System.out.println("-----------------error informaiton------");
new Thread(new StreamDrainer(process.getErrorStream())).start();
System.out.println("-----------------error informaiton------");
process.getOutputStream().close();

int exitValue = process.waitFor();
System.out.println("Return Value:" + exitValue);
process.destroy();

}
catch(Exception e)
{
System.out.println("Exception:"+e.getMessage() );
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息