Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Java Shut down the PC after specified amount of time

0 Replies, 1829 Views

In The Name OF Allah
Al-Salam Alekum

Hi guys, today we will make a simple app for shutting down the PC after specified amount of time in Java Wink
Add two Text Field and a button to your JFrame, then Double Click on the Button, our work Starts here :Tongue
The First textField is for the hours, the second is for minutes. Okay we will get the text and convert it to int:

    
Code:
try{
            int hours = Integer.parseInt(jTextField1.getText()); //get the text and convert it to integer
            int minutes = Integer.parseInt(jTextField2.getText()); //get the text and convert it to integer
            Thread.sleep((hours * 60 * 60 * 1000) + (minutes * 60 * 1000)); //Here the thread will sleep until the time to shutdown the PC.
            Runtime.getRuntime().exec("shutdown -s -t 30"); //shutting down the PC after 30 sec of executing the code.
         }
    catch(NumberFormatException | InterruptedException | IOException e){
            JOptionPane.showMessageDialog(this, "Pls fill the TextFields", "Erro", JOptionPane.ERROR_MESSAGE);
         }

Big Grin
Wa Salam Alekum
Rs
* Thankful to Allah *
Kurdy

Messages In This Thread
Java Shut down the PC after specified amount of time - by Mr.Kurd - 09-14-2017, 06:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling? Mr.Kurd 0 5,211 03-20-2020, 08:49 PM
Last Post: Mr.Kurd
Question Why Java Strings are immutable in nature? Mr.Kurd 0 2,647 03-20-2020, 08:47 PM
Last Post: Mr.Kurd
Question What is constructor chaining in Java? Mr.Kurd 0 2,602 03-20-2020, 08:43 PM
Last Post: Mr.Kurd
Question What is the difference between equals() and == in Java? Mr.Kurd 0 2,638 03-20-2020, 08:40 PM
Last Post: Mr.Kurd
  What public static void main(String args[]) stand for in Java? Mr.Kurd 0 2,676 03-20-2020, 08:38 PM
Last Post: Mr.Kurd
Question Why multiple inheritance is not supported in java? Mr.Kurd 0 2,616 03-20-2020, 08:34 PM
Last Post: Mr.Kurd
  Tutorial Taking Screenshot in Java Mr.Kurd 0 2,707 03-09-2020, 05:38 PM
Last Post: Mr.Kurd
  10 java skills HeavensReject 1 3,415 01-12-2020, 05:31 AM
Last Post: Mr.Kurd
Wink Tutorial Downloading video from Youtube using JAVA Mr.Kurd 0 5,409 02-06-2019, 06:33 PM
Last Post: Mr.Kurd
  Tutorial Java and SSH Mr.Kurd 0 3,425 12-16-2018, 08:05 PM
Last Post: Mr.Kurd



Users browsing this thread: 1 Guest(s)