package org.leanwo.management.util; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * 程序启动通用类 * * @author YangZhiJie * */ public class ProgramAutoStartUtil { private static Log logger = LogFactory.getLog(ProgramAutoStartUtil.class); /** * 生成停止程序的BAT文件 */ public static void generateStartBat() { File file = new File("autoStart.bat"); if (file.exists() == false) { try { file.createNewFile(); } catch (IOException e) { logger.error("autoStart.bat文件创建失败."); e.printStackTrace(); } } logger.info("autoStart.bat文件的路径如下:" + file.getAbsolutePath()); try { BufferedWriter out = new BufferedWriter(new FileWriter(file, false)); String path = file.getAbsolutePath().replace(File.separatorChar + file.getName(), ""); String command = "CD " + path + "\r\n"; String diskName = path.substring(0, path.indexOf(":") + 1); command += (diskName + "\r\n"); File pathFolder = new File(path); File[] files = pathFolder.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { if (pathname.isFile() && pathname.getName().endsWith(".jar")) { return true; } return false; } }); if(files != null && files.length > 0) { command += ("start javaw -jar " + files[0].getName() + " autoRun"); } out.write(command); out.close(); } catch (IOException e) { logger.error("autoStart.bat文件写入失败。"); } } }