java程序执行exe脚本文件
一、新建bat脚本文件,并写入执行exe脚本命令:
二、执行bat脚本:
1 String fullPath = "E:\\model-script\\ComSim-master.bat"; 2 File file = new File(fullPath); 3 if (file.exists()) { // 如果已存在,删除旧文件 4 file.delete(); 5 } 6 file.createNewFile(); 7 Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); 8 write.write("cd /d e:\\submit files && ComSim-master.exe \"NewPaths/5HydrodynamicSimulation/sj-InPathConfig_TDMA.ini\" \""+beginTm+"\" \""+endTm+"\""); 9 write.flush(); 10 write.close(); 11 String[] cmd = {fullPath}; 12 Process process = null; 13 BufferedReader br; 14 BufferedReader brError; 15 String line; 16 try { 17 ProcessBuilder pb = new ProcessBuilder(); 18 pb.command(cmd); 19 process=pb.start(); 20 br = new BufferedReader(new InputStreamReader(process.getInputStream())); 21 brError = new BufferedReader(new InputStreamReader(process.getErrorStream())); 22 while ((line = br.readLine()) != null || (line = brError.readLine()) != null) { 23 System.out.println(line); 24 } 25 } catch (Exception e) { 26 e.printStackTrace(); 27 }finally { 28 if (process != null){ 29 process.destroy(); 30 } 31 }