ProgramAutoStartUtil.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package org.leanwo.management.util;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileFilter;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. /**
  10. * 程序启动通用类
  11. *
  12. * @author YangZhiJie
  13. *
  14. */
  15. public class ProgramAutoStartUtil {
  16. private static Log logger = LogFactory.getLog(ProgramAutoStartUtil.class);
  17. /**
  18. * 生成停止程序的BAT文件
  19. */
  20. public static void generateStartBat() {
  21. File file = new File("autoStart.bat");
  22. if (file.exists() == false) {
  23. try {
  24. file.createNewFile();
  25. } catch (IOException e) {
  26. logger.error("autoStart.bat文件创建失败.");
  27. e.printStackTrace();
  28. }
  29. }
  30. logger.info("autoStart.bat文件的路径如下:" + file.getAbsolutePath());
  31. try {
  32. BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
  33. String path = file.getAbsolutePath().replace(File.separatorChar + file.getName(), "");
  34. String command = "CD " + path + "\r\n";
  35. String diskName = path.substring(0, path.indexOf(":") + 1);
  36. command += (diskName + "\r\n");
  37. File pathFolder = new File(path);
  38. File[] files = pathFolder.listFiles(new FileFilter() {
  39. @Override
  40. public boolean accept(File pathname) {
  41. if (pathname.isFile() && pathname.getName().endsWith(".jar")) {
  42. return true;
  43. }
  44. return false;
  45. }
  46. });
  47. if(files != null && files.length > 0) {
  48. command += ("start javaw -jar " + files[0].getName() + " autoRun");
  49. }
  50. out.write(command);
  51. out.close();
  52. } catch (IOException e) {
  53. logger.error("autoStart.bat文件写入失败。");
  54. }
  55. }
  56. }