java自定义时间启动双击bat文件
直接上代码
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class Run {
@Scheduled(cron = "0 18 15 * * ?")
public void doTask(){
try {
Thread thread = new Thread();
String command = "cmd /c start stop.bat"; // stop.bat 是要启动的批处理文件名
Process process = Runtime.getRuntime().exec(command);
int exitCode = process.waitFor();
System.out.println("批处理文件已经执行,退出码:" + exitCode);
process.destroy();
thread.sleep(1000);
String commands = "cmd /c start start.bat"; // 第二个start.bat 是要启动的批处理文件名
Process processs = Runtime.getRuntime().exec(commands);
int exitCodes = processs.waitFor();
System.out.println("批处理文件已经执行,退出码:" + exitCodes);
process.destroy();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
这里的runtime.jar 打包出来运行控制start.bat 和 stop.bat
要注意的是脚本最后要加入exit 不然到时候会很多cmd框框 不关闭