How to Restart or Shutdown Windows 10 PC using Processing 3

I am making an arcade station with a Latte Panda v1.0 as the main computer which plays fun games like “The Game of Life” an others.

In the middle there is a “World Reset” button and pressing it is supposed to reset the game and load back to the desktop.

I found a way to do this was to have the button trigger a relay with a 5 second delay. As if you were simply unplugging the computer, waiting 5 seconds, and plugging it back in.

However, this leads to more problems as even though “Auto Power On” is enabled in the BIOS the computer starts back up without error maybe 50% of the time. Usually saying something like Windows did not shutdown correctly, etc. See image 1.

So, this brings me to the latest solution and question!!!

I found that by opening the cmd command line running the line “shutdown /r /f /t 0” would more properly restart the computer in a timely fashion without error.

But, I am stuck with how to run that through Processing so it can be automated with a button press. As in use an Arduino to sense the “World Reset” button, send a command through USB to Processing, use the “exec()” function to then type the command line and run it.

I feel like I am close and this is what I have so far. I am getting the error “Exception while attempting shutdown /r /f /t 0”. See Image 2.

Any ideas how I could make this work?

I am open to totally other ideas I just know the only constraints are using the “World Reset” button right in front of the screen to restart the computer.

import java.io.*;

void setup()  {
  size(100, 100);
}

void draw()  {
  background(20);
  fill(200);
  ellipse(mouseX, mouseY, 25, 25);
}

void mousePressed()  {
  
  // run command to restart computer...
  
  exec("shutdown /r /f /t 0");
  
  //exit();
  //exec()
}


Hi @CaseyJ. My guess is you will need to use a non-Processing Java class and access some of the built-in Java functionality for the Windows OS.
If you can properly run a command prompt script or a .bat file that restarts the machine successfully then you can execute the file from Java’s Process Builder.
There is an example here: https://www.geeksforgeeks.org/how-to-execute-native-shell-commands-from-java-program/
I don’t know how version specific these examples are to the jdk Processing uses, but you can always use pure Java in P3 sketches.
Other than that there may be a P3 library that handles OS specific subprocesses but in my experience it’s just as easy to use Java’s built-in classes.
If I have time later I’ll try to run a simple cmd.exe .bat file and see how it works in P3.

1 Like

Just an update. I was able to get my machine to restart using the same command you use in your sketch, but instead I copied it to a .bat file and used the code below from the previous example via GeeksForGeeks to execute it. It seemed to work fine for me.
However, just a caveat, it may behave differently on other machines as different processes may be active during shutdown and cause the OS to be left in an unstable state. You may want to investigate what the /f flag for the shutdown command really does, and perhaps there is a “safer” way to reboot. Or you may need to kill certain processes before rebooting, all of which could be added to the .bat file. Good luck!

// Run a simple .bat program in the Java console
import java.io.File;
import java.io.IOException;

void setup()
  {
    try {
      // File location for the bat script
      File dir = new File("C:\\Users\\xxx\\Desktop");
      // Command to run the bat file in the same
      // console
      ProcessBuilder pb = new ProcessBuilder(
        "cmd.exe", "/C", "hello.bat");
      pb.directory(dir);
      Process process = pb.start();

      int exitVal = process.waitFor();
      if (exitVal == 0) {
        
        System.exit(0);
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
1 Like

Hello,

Example:

import java.io.*;

void setup()
  {
  Runtime runtime = Runtime.getRuntime();
  
try
    {
    System.out.println("Shutdown and restarting the PC after 50 seconds.");
    runtime.exec("shutdown -r -t 50");
     }
  catch(IOException e)
    {
    System.out.println("Exception: " +e);
    }
  }

Reference:
Java Program to Shutdown and Restart Computer.

I set a longer time so I could abort it !

:)

1 Like

Okay I found out a work around though it would be nicer to be able to use the “exec()” line directly.

What I did was I found out you can make a command line an executable by typing it into a .txt file and then saving with the extension “.bat”.

I was able to the use “launch()” and it worked great.

I can now use a physical button to trigger an Arduino to send a message to Processing which then launches this script.

I think this is kind of a weird work around but it has not booted incorrectly yet and I’ve done it 5 times in a row now.

Good call! Putting the line to execute in a Batch file worked well and has been performing flawlessly now.

Thank you for the great tip!

Again nice call on the .bat file. I even found that this behaves better than just the command line.

Also, I have found the “/f” to be the “force” command which in my experience helps quit everything when other programs are running.

Great work it looks like I was missing a few lines of code to make it all happen! I am going to try this out and see if it works better / different. Thank you for your time and help!

1 Like