May 15, 2012

How To Shutdown PC With Java Code

Java Runtime
In this tutorial I create a program that shuts the computer down after 10 seconds, which gives you chance to abort the shutdown sequence. I used Java Runtime to execute the command line switch, and the second parameter for aborting the first command sequence. Java Runtime is key to making that possible. You can also pass other commands as parameter to start other Windows programs.

package codecypherprojects;

 *
 * @author khamis
 */


import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class shutDown extends JFrame implements ActionListener,Runnable{
    public boolean running;
    public static Object MUTEX;
    public int sec;
    JFrame f;
    public int count;
    Thread t;
  public shutDown() {
        createComponent();
  }
    public final Component createComponent(){
        JButton jb = new JButton("Shut Down Your Computer");
        JButton jb1 = new JButton("Cancel Shut Down Sequence");
        jb.setActionCommand("shut");
        jb1.setActionCommand("cancel");
        jb.addActionListener(this);
        jb1.addActionListener(this);
        JPanel p = new JPanel();
        p.setLayout(new FlowLayout());
        p.add(jb);
        p.add(jb1);
        f = new JFrame();
        f.add(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setIconImage(new ImageIcon((getClass().getResource("images/apple.png")))
           .getImage());
        f.setSize(400,120);
        f.setVisible(true);
        getCenteredWindowPoint(f);
        f.pack();
       return f;
    }
   public  void getCenteredWindowPoint(JFrame win) {
      win.setLocation(400,300);
  }
  
    public void actionPerformed(ActionEvent e){
        String str = e.getActionCommand();
        if(str.equals("shut")){
            System.out.print("System is shutting down");
            System.out.println(timeRemain());
            shutDown();
            }
       else
         if(str.equals("cancel")){
             cancel();
             f.setTitle("Shut Down Cancelled");
             System.out.println("Shutdown canceled");
        }

     }
    public int timeRemain(){
        for(sec=10;sec>=0;sec--){
            System.out.print(" in " + sec + " second ");
            // synchronized(syncMutex.mutex){
                 syncMutex.val = sec;
                 count = syncMutex.val;
                 f.setTitle("Remain " + count + " Second To Shut Down");
           //  }
            try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(shutDown.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
        if(count == 0){
            f.setTitle("System Is Shutting Down....");
        }
      return sec;
    }
    public void shutDown(){
       Runtime r = Runtime.getRuntime();

            try {
                 r.exec("shutdown -s -t 60");
            } catch (IOException ex) {
                Logger.getLogger(shutDown.class.getName()).log(Level.SEVERE, 
               null, ex);
            }

    }
     public void cancel(){
       Runtime r = Runtime.getRuntime();

            try {
                 r.exec("shutdown -a");
            } catch (IOException ex) {
                Logger.getLogger(shutDown.class.getName()).log(Level.SEVERE,
                null, ex);
          }

    }
    
     public void start(){
          t = Thread.currentThread();
         if(!t.isAlive()){
             t.start();
         }
         
     }


    @Override
    public void run(){

            if (running && !t.isAlive()) {
                this.start();
            }
            running = false;
            t = null;
    }

     public static void main(String[] args){
           new shutDown().run();
     }

}

class syncMutex{
    static final Object mutex = new Object();
    static  int val = 0;

}
This is the result of the program

3 comments:

  1. Seems to be perfect... will try it soon...!!! nice work sir..!!!

    ReplyDelete
  2. My brother suggested I would possibly like this website. He was once totally right.
    This post truly made my day. You can not consider just how so much time
    I had spent for this info! Thank you!
    My homepage - delete ex From facebook

    ReplyDelete