java代码执行 bat 脚本
java代码执行 bat 脚本
·
java代码执行 bat 脚本
package com.example.springbootycw.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
@Component
public class NoSleepTestt {
@Scheduled(cron = "0/2 * * * * ?")
public void testSleep() {
System.out.println("进入");
Function1 f = new Function1();
try {
f.controlPowerState();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
class Function1 {
public void controlPowerState() throws IOException, InterruptedException {
// 使用PowerShell命令控制电源状态
String command = "explorer http://www.baidu.com";
Process powerShellProcess = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));
// 输出命令执行结果(可选)
String s;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.err.println(s);
}
powerShellProcess.waitFor();
}
}
}
更多推荐
已为社区贡献8条内容
所有评论(0)