Shutdown/Restart Windows With Simple Java Program
We can just make use of the Runtime class in java to execute the shutdown command using the simple java code.
We can shutdown from the command prompt itself.
To know the various ways to shutdown windows take a look at the below video.
Program : (Shut Down)
public class ShutDownMySystem
{
public static void main(String[] args)
{
Runtime runtime=Runtime.getRuntime();
try
{
System.out.println("shutting down ....");
runtime.exec("shutdown /s /t 0");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Program : (Restart)
public class RestartMySystem
{
public static void main(String[] args)
{
Runtime runtime=Runtime.getRuntime();
try
{
System.out.println("shutting down ....");
runtime.exec("shutdown /r");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Explanation:
/s - to shutdown
/t - to specify the time in seconds after to shutdown
/r - to restart
Output :
Take a look at the below video to see the the real time output.