How do you force a computer shut-down in Java? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do you force a computer shut-down in Java?

ggledggled Posts: 2Member
I was just wondering if there was a way to force a computer to shut down using a command line in javascript, and if you could tell me it, so I can add it onto the end of a doccument (for a prank).

Thanks in advance,
George

Comments

  • zibadianzibadian Posts: 6,349Member
    : I was just wondering if there was a way to force a computer to shut
    : down using a command line in javascript, and if you could tell me
    : it, so I can add it onto the end of a doccument (for a prank).
    :
    : Thanks in advance,
    : George
    :
    The following class has a number of shutdown methods for the various windows versions and linux:
    [code]
    public class Shutdown {

    private Shutdown() { }

    private static void runCommand(String file, String params) {
    ParamArray pa = params.split(" ");
    Runtime.exec(file, pa);
    }

    public static void shutdownWinpreXP() {
    runCommand("RUNDLL32.EXE", "user32,ExitWindowsEx");
    }

    public static void shutdownWinXP() {
    runCommand("shutdown.exe", "-s -t 01");
    }

    public static void shutdownWindows() {
    try {
    shutdownWinXP();
    catch (IOException e) {
    shutdownWinpreXP();
    }
    }

    public static void shutdownLinux() {
    runCommand("shutdown", "now");
    }

    }
    [/code]
    This code hasn't been tested. I don't know the command to shutdown a Mac or other platforms.
    The usage is quite straight forward:
    [code]
    Shutdown.shutdown[OSName]();
    [/code]
    where [OSName] is the OS to shutdown.

    And always remember: (Java != Javascript);
  • ggledggled Posts: 2Member
    : : I was just wondering if there was a way to force a computer to shut
    : : down using a command line in javascript, and if you could tell me
    : : it, so I can add it onto the end of a doccument (for a prank).
    : :
    : : Thanks in advance,
    : : George
    : :
    : The following class has a number of shutdown methods for the various
    : windows versions and linux:
    : [code]:
    : public class Shutdown {
    :
    : private Shutdown() { }
    :
    : private static void runCommand(String file, String params) {
    : ParamArray pa = params.split(" ");
    : Runtime.exec(file, pa);
    : }
    :
    : public static void shutdownWinpreXP() {
    : runCommand("RUNDLL32.EXE", "user32,ExitWindowsEx");
    : }
    :
    : public static void shutdownWinXP() {
    : runCommand("shutdown.exe", "-s -t 01");
    : }
    :
    : public static void shutdownWindows() {
    : try {
    : shutdownWinXP();
    : catch (IOException e) {
    : shutdownWinpreXP();
    : }
    : }
    :
    : public static void shutdownLinux() {
    : runCommand("shutdown", "now");
    : }
    :
    : }
    : [/code]:
    : This code hasn't been tested. I don't know the command to shutdown a
    : Mac or other platforms.
    : The usage is quite straight forward:
    : [code]:
    : Shutdown.shutdown[OSName]();
    : [/code]:
    : where [OSName] is the OS to shutdown.
    :
    : And always remember: (Java != Javascript);


    Thanks a lot, but I'm a bit unsure as to which bit to copy and paste into my webpage to get an automatic shutdown for Windows XP.

    Thanks again,
    George.
  • zibadianzibadian Posts: 6,349Member
    : : : I was just wondering if there was a way to force a computer to shut
    : : : down using a command line in javascript, and if you could tell me
    : : : it, so I can add it onto the end of a doccument (for a prank).
    : : :
    : : : Thanks in advance,
    : : : George
    : : :
    : : The following class has a number of shutdown methods for the various
    : : windows versions and linux:
    : : [code]: :
    : : public class Shutdown {
    : :
    : : private Shutdown() { }
    : :
    : : private static void runCommand(String file, String params) {
    : : ParamArray pa = params.split(" ");
    : : Runtime.exec(file, pa);
    : : }
    : :
    : : public static void shutdownWinpreXP() {
    : : runCommand("RUNDLL32.EXE", "user32,ExitWindowsEx");
    : : }
    : :
    : : public static void shutdownWinXP() {
    : : runCommand("shutdown.exe", "-s -t 01");
    : : }
    : :
    : : public static void shutdownWindows() {
    : : try {
    : : shutdownWinXP();
    : : catch (IOException e) {
    : : shutdownWinpreXP();
    : : }
    : : }
    : :
    : : public static void shutdownLinux() {
    : : runCommand("shutdown", "now");
    : : }
    : :
    : : }
    : : [/code]: :
    : : This code hasn't been tested. I don't know the command to shutdown a
    : : Mac or other platforms.
    : : The usage is quite straight forward:
    : : [code]: :
    : : Shutdown.shutdown[OSName]();
    : : [/code]: :
    : : where [OSName] is the OS to shutdown.
    : :
    : : And always remember: (Java != Javascript);
    :
    :
    : Thanks a lot, but I'm a bit unsure as to which bit to copy and paste
    : into my webpage to get an automatic shutdown for Windows XP.
    :
    : Thanks again,
    : George.
    :
    You can use the entire class and then call either the shutdownWinXP() or the shutdownWindows() method.
Sign In or Register to comment.