Rungeek's Java Shutdown Program
About:
A quick shutdown program I wrote in Java. Just enter a date/time and click Shutdown. When the time hits then your computer will try and shutdown.
It works cross-platform, Windows, Mac, *nix. If you run it in Mac/*nix then you need to run it as root (sudo).
To run as admin on a Mac for example: sudo java -jar Shutdown.jar
Download
Get it here!
[advertisement]
Code:
Here is teh code. You can download the file here.
package com.rungeek.shutdown;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Shutdown extends JPanel {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date shutdownDate;
private static final long serialVersionUID = 1L;
public Shutdown() {
super(new BorderLayout());
Date date = new Date();
// Create input panel
JPanel inputPanel = new JPanel(new BorderLayout());
JPanel layoutPanel = new JPanel(new BorderLayout());
// Create date label
JLabel lblDate = new JLabel("Date");
// Create date input
final JTextField txtDate = new JTextField(dateFormat.format(date));
// Create Shutdown label
final JLabel lblShutdown = new JLabel("Time Not Set");
// Create Shutdown button
final JButton btnShutdown = new JButton(new AbstractAction("Shutdown") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
try {
shutdownDate = dateFormat.parse(txtDate.getText());
lblShutdown.setText("Shutdown at: " + dateFormat.format(shutdownDate));
Thread thread = new Thread(new Runnable() {
public void run() {
txtDate.setEnabled(false);
while (new Date().before(shutdownDate)) {
txtDate.setText(dateFormat.format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
}
try {
shutdown();
} catch (Exception se) {
se.printStackTrace();
}
}
});
thread.start();
} catch (ParseException e1) {
JOptionPane.showMessageDialog(null, "Error Parsing Date.", "Bad Date Entered", 0);
System.out.println("Sorry Bad Date Entered");
}
}
});
// Add to Panel
inputPanel.add(lblDate, BorderLayout.WEST);
inputPanel.add(txtDate, BorderLayout.CENTER);
layoutPanel.add(inputPanel, BorderLayout.NORTH);
layoutPanel.add(btnShutdown, BorderLayout.SOUTH);
layoutPanel.add(lblShutdown, BorderLayout.CENTER);
// Add the panel to the Main Panel
this.add(layoutPanel, BorderLayout.CENTER);
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Rungeek's Shutdown Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(new Shutdown());
// Display the window.
frame.pack();
frame.setSize(300, 100);
frame.setVisible(true);
}
public static void main(String[] args) throws InterruptedException, RuntimeException, IOException {
if (args.length > 0) {
int mins = Integer.parseInt(args[0]);
int seconds = mins * 60;
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(format.format(date));
System.out.println("Shutting down in :" + seconds + " seconds");
// Sleep for that many seconds
for (int i = 0; i <= seconds; i++) {
Thread.sleep(1000); // 1 second sleep
}
shutdown();
} else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
public static void shutdown() throws RuntimeException, IOException, InterruptedException {
String shutdownCommand;
String operatingSystem = System.getProperty("os.name");
if (operatingSystem.toLowerCase().contains("linux") || operatingSystem.toLowerCase().contains("mac")) {
shutdownCommand = "shutdown -h now";
} else if (operatingSystem.toLowerCase().contains("windows")) {
shutdownCommand = "shutdown.exe -s -t 0";
} else {
throw new RuntimeException("Unsupported operating system.");
}
System.out.println("Shutting down " + operatingSystem);
Process proc = Runtime.getRuntime().exec(shutdownCommand);
proc.waitFor();
System.exit(0);
}
}