Skip to content
Advertisement

Creating a Windows auto restart + a counter that increments each reboot

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. . .");

    }

}

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement