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();
        }
    }
}
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐