I'm trying the following Java code to shut down Windows operating system. I'm using Microsoft Windows XP Professional version 2002, Service pack 3 with Intel(R) Core(TM) i3 CPU using Netbeans IDE 6.9.1.
package shutdownos;
import java.io.IOException;
final public class Main
{
public static void main(String... args) throws IOException
{
String shutdownCommand="";
String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem))
{
shutdownCommand = "shutdown -h now";
}
else if ("Windows".equals(operatingSystem))
{
shutdownCommand = "shutdown.exe -s -t 0";
}
else
{
//throw new RuntimeException("Unsupported operating system.");
}
Runtime.getRuntime().exec(shutdownCommand);
System.exit(0);
}
}
It throws the following exception in Java.
Exception in thread "main" java.lang.IllegalArgumentException: Empty command
The exception occurs on the following line in the above code.
Runtime.getRuntime().exec(shutdownCommand);
The second-last in the main() method. What is wrong with this code?
else if ("Windows".equals(operatingSystem))to haveelse if ("Windows XP".equals(operatingSystem))and windows shut down successfully.