ProgramStartUtil.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 ProgramStartUtil {
  16. private static Log logger = LogFactory.getLog(ProgramStartUtil.class);
  17. /**
  18. * 生成停止程序的BAT文件
  19. */
  20. public static void generateStartBat() {
  21. File file = new File("start.bat");
  22. if (file.exists() == false) {
  23. try {
  24. file.createNewFile();
  25. } catch (IOException e) {
  26. logger.error("start.bat文件创建失败.");
  27. e.printStackTrace();
  28. }
  29. }
  30. logger.info("start.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 = "title Server Management\r\n";
  35. command += ("CD " + path + "\r\n");
  36. String diskName = path.substring(0, path.indexOf(":") + 1);
  37. command += (diskName + "\r\n");
  38. File pathFolder = new File(path);
  39. File[] files = pathFolder.listFiles(new FileFilter() {
  40. @Override
  41. public boolean accept(File pathname) {
  42. if (pathname.isFile() && pathname.getName().endsWith(".jar")) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. });
  48. if(files != null && files.length > 0) {
  49. command += ("javaw -jar " + files[0].getName());
  50. }
  51. out.write(command);
  52. out.close();
  53. } catch (IOException e) {
  54. logger.error("start.bat文件写入失败。");
  55. }
  56. }
  57. }