SystemUtil.java 1.43 KB
package cn.csbr.app.util;

import cn.hutool.core.io.FileUtil;
import lombok.AllArgsConstructor;

import java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

public class SystemUtil {
    public static void restartMain()  {
        File[] files = FileUtil.ls(System.getProperty("user.dir"));
        HashMap<Long, String> fileMap = new HashMap<>();
        for (File file : files) {
            if (file.getName().endsWith(".jar")) {
                fileMap.put(file.lastModified(), file.getName());
            }
        }
        Set<Long> longs = fileMap.keySet();
        Long max = Collections.max(longs);
        System.out.println(fileMap.get(max));
        ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "start", "java", "-jar", fileMap.get(max));
        new start(pb).start();
        new exit().start();
    }
    public static void ReHcg(){
       // String path = "D:\\public.bat";
        Runtime run = Runtime.getRuntime();
        try {
            run.exec("cmd /k shutdown -r -t 2");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
@AllArgsConstructor
class start extends Thread  {

    ProcessBuilder pb;

    @Override
    public void run() {
        try {
            pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class exit extends Thread  {

    @Override
    public void run() {
        System.exit(0);
    }
}