0

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?

3
  • Put a breakpoint at that line and see what shutdownCommand is. I don't think it's being set. Try uncommenting the throw exception. There is so many other things wrong with this code, but lets start there. Commented Nov 19, 2011 at 9:53
  • I modified this line in the above code else if ("Windows".equals(operatingSystem)) to have else if ("Windows XP".equals(operatingSystem)) and windows shut down successfully. Commented Nov 19, 2011 at 10:02
  • Also note that you may not be allowed to run this command. Your code should handle such a situation properly. Commented Nov 19, 2011 at 10:24

6 Answers 6

5

You're looking for the wrong value of os.name -- whatever it is, it's not "Windows" and therefore the code runs through the else block where the variable shudowncommand is left as an empty string, which causes the "Empty command" exception. Do a System.out.println(operatingSystem) to see what value you should check for instead of "Windows".

It's probably something like "Windows NT", so if you replace the .equals() check by a .startsWith() check you should be set.

Sign up to request clarification or add additional context in comments.

Comments

4

According to this page, you seem to be using the wrong value for O/S names. If that is the case, your shutdownCommand string will remain empty, which will cause your program to pass an empty command, which conforms with the exception you are getting.

Comments

3

the program is skipping both the if statements, os.name would not be just Windows, it would be i think Windows XP or more, so replace that with starts with

else if (operatingSystem.startsWith("Windows")){

 }

Comments

3

There is no property "os.name" that is equal to "Windows". Did you mean to check for if the property contains "Windows"?. As far as I know all window os names have a quantifier such as "Windows 95", "Windows NT" etc.

Comments

1

The shutdown command for windows os should be shutdown -s -t 0. try with this.

Comments

1

I mostly use this site to find out the possible values: What os or arch value can I use?

As others said, you should use String.startsWith() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.