Home > Back-end >  Creating a Windows auto restart a counter that increments each reboot
Creating a Windows auto restart a counter that increments each reboot

Time:11-03

I'm trying to set up a program to auto restart windows after boot as well as incrementing the amount of restarts that it has completed.

I've written a small amount of code but it's not even restarting the system itself when placed in the startup folder. You can see the command prompt very briefly, then nothing happens.

import java.awt.event.WindowEvent;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.io.*;
import javax.swing.*;


public class AutoRestart {

    public static void main(String[] args) throws IOException {
        int numRestarts = 0;

        Runtime r = Runtime.getRuntime();
        numRestarts  ;


        File outFile = new File("C:\\reboots\\numberOfReboots.txt");
        if (outFile.exists()) {
            System.exit(0);
        }

        PrintWriter writer = new PrintWriter(outFile);
        writer.println("Number of times rebooted: "   numRestarts);
        writer.close();

        r.exec("shutdown -r -t 0");
        System.out.println("Restarting. . .");

    }

}

CodePudding user response:

The int variable does not survive the reboot, you could try to read from the file you just created and increase the number in there.

  • Related