try { Runtime.getRuntime().exec("shutdown /h"); } catch (IOException ex) {}
Tutorials, tips and tricks about java programming language. Answers for many doubts about java programming language. Answering many howtos. Sample java projects and source code, java games, java programs for java practical labs.
Search This Blog
Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts
Java Program to Hibernate Windows Operating System
Do you want to hibernate your Windows computer using java program? This is a java program to hibernate Windows operating System. If you execute this java program, it will hibernate your Windows computer instantly. The following is the java code to hibernate Windows:
Java Code to Restart Windows
Are you thinking how to restart Windows from Java. You can restart Windows operating system by executing a system command. You can restart windows by executing the system command shutdown /r /t 1. The following Java code will restart the windows computer when executed.
try { Runtime.getRuntime().exec("shutdown /r /t 1"); } catch (IOException ex) {}
How to Schedule a Shutdown in Java - Java Code to Schedule a Shutdown
In this post, we will see how to schedule a shutdown in a computer using java program. For this, we execute the appropriate shutdown command after identifying the operating system. The following java code will schedule a shutdown after a 10 minute from execution of the code.
String os=System.getProperty("os.name").toLowerCase(); String command; try { if(os.contains("windows")) { command="shutdown /s /t 600"; } else if(os.contains("linux")||os.contains("mac os x")) { command="shutdown -h +600"; } else throw new Exception("Unsupported OS"); Runtime.getRuntime().exec(command); }catch(Exception ex){ JOptionPane.showMessageDialog(null,ex.getMessage()); }
How to Shutdown Computer from Java - Java Code to Shutdown Computer
Java program can cause a computer to shutdown. In order to shutdown a computer using java code, you should execute the shutdown command in that operating system. The shutdown command is different in Windows and Linux. So, we first check for the operating system. Then we execute appropriate shutdown command.
For Linux and Mac OS X, the command for immediate shutdown is shutdown -h now. But in Windows, it is shutdown /p. The above java code checks the operating system. Then it assumes appropriate shutdown command and then executes it. This java code can shutdown a computer.
try { String command; String os = System.getProperty("os.name").toLowerCase(); if (os.contains("linux") || os.contains("mac os x")) command = "shutdown -h now"; else if (os.contains("windows")) command = "shutdown /p"; else throw new Exception("Unsupported operating system."); Runtime.getRuntime().exec(command); System.exit(0); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); }
For Linux and Mac OS X, the command for immediate shutdown is shutdown -h now. But in Windows, it is shutdown /p. The above java code checks the operating system. Then it assumes appropriate shutdown command and then executes it. This java code can shutdown a computer.
How to Logoff or Sign Out Windows Using Java Code
You can sign out the current user account from windows using java code. Sign out, log out or logoff button can be created using simple lines of code. To logout from Windows operating system using java code, use the following code:
As the above code shows, we are simply executing a cmd command shutdown /l. It is actually running shutdown.exe with parameter /l to force logoff. If you add above code in the actionPerformed() function of a button, the button becomes a logoff button. The following video will show an example in which a simple button is made into a logout button.
try { Runtime.getRuntime().exec("shutdown /l"); } catch (IOException ex) {}
As the above code shows, we are simply executing a cmd command shutdown /l. It is actually running shutdown.exe with parameter /l to force logoff. If you add above code in the actionPerformed() function of a button, the button becomes a logoff button. The following video will show an example in which a simple button is made into a logout button.
Plane Shooter Game for PC -Java Game Project with Source code
Plane Shooter is a single player lightweight Java game for Computers. It is a 2D game that works well even on low end computers. The game size is only a few Kilobytes. You play as a tank and shoot down planes and what they drop. The game is in 7 or more levels. This is my first complete Greenfoot project. The source code of this game is also uploaded for those who want to see.
Subscribe to:
Posts (Atom)