`

java调用命令行

    博客分类:
  • JAVA
阅读更多
public class MyExe {
public void execEXE(String[] exeName , String[] env)
{
try{
if(null == env || env.length==0){
this.executeExe(exeName) ;
}else{
this.executeExe(exeName, env) ;
}
}catch(Exception e ){
e.printStackTrace() ;
}
}
/**
* 执行EXE程序
* @param exeName   需要执行EXE的程序名称以及命令行参数
* @param env       需要执行EXE程序的环境变量
* @throws InterruptedException
*/
public void executeExe(String [] exeName,String [] env) throws InterruptedException{
Runtime runTime=Runtime.getRuntime();
try {
Process process=runTime.exec(exeName, env);
String text=null;
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
while ((text = in.readLine())!= null) { 
  System.out.println(text); //输出测试 

in.close();

System.out.println("finally  Exe");
process.waitFor();
process.destroy();
} catch (IOException e) {
System.out.println("调用Exe出错!!!");
e.printStackTrace();
}
}
/**
* 执行EXE程序 ,不加环境变量
* @param exeName   需要执行EXE的程序名称以及命令行参数
* @param env       需要执行EXE程序的环境变量
* @throws InterruptedException
*/
public void executeExe(String [] exeName) throws InterruptedException{
Runtime runTime=Runtime.getRuntime();
try {
Process process=runTime.exec(exeName);
String text=null;
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
while ((text = in.readLine())!= null) { 
  System.out.println(text); //输出测试 

in.close();

System.out.println("finally  Exe");
process.waitFor();
process.destroy();
} catch (IOException e) {
System.out.println("调用Exe出错!!!");
e.printStackTrace();
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics