Search This Blog

Showing posts with label operating system. Show all posts
Showing posts with label operating system. 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:

try
 {
 Runtime.getRuntime().exec("shutdown /h");
 } 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 Get Operating System Information in Java

Using java, you can gather some information about the operating system of the computer system on which the code is running. That us java provides some API classes that will help you to know about the operating system of the computer on which your application program is running. You can use System.getProperty() method for this purpose. There are some system property keys (strings) which can be used to get the respective information. The parameter to the System.getProperty() method is a string, the property key. The following values for the parameter string will give operating system related informations.

"os.arch"
Operating system architecture

"os.name"
Name of the operating system.

"os.version"
Version of Operating system.

Usage examples:

The following java program displays the Operating system related informations using getProperty() method.
System.out.println("Operating system architecture:"+System.getProperty("os.arch") ); System.out.println("Operating system Name:"+System.getProperty("os.name") ); System.out.println("Operating system version:"+System.getProperty("os.version") );

How to Use System.getProperty() - Keys for System Properties

There is a method getProperty() in System class in java. As the name indicates, this java method is used to get System specific, operating system specific or user specific properties. There are different keys which are strings passed as parameter to the getProperty() method.

Usage

The following sample java code will show you how to use the getProperty method in System class. The key is a string. The System.getProperty() method takes a string parameter (argument). Based on the string passed, the method System.getProperty() returns different properties as string. In this example,we pass "java.io.tmpdir" as the parameter, which is the key to get the temporary directory (temp directory) for the current user account.

String property = "java.io.tmpdir"; String tempDir = System.getProperty(property); System.out.println("temp directory ="+ tempDir );


Similarly, for each property, there is a key. Below, the keys for different properties are listed.
"file.separator"
Gives the character that separates components of a file path. This is "/" on UNIX and "\" on Windows. This character is used in File paths for cross platform java applications
"java.class.path"
Returns the path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.
"java.home"
Returns the installation directory for Java Runtime Environment (JRE)
"java.vendor"
Returns the name of JRE vendor
"java.vendor.url"
Returns the URL of JRE vendor
"java.version"
Returns the version number of JRE
"line.separator"
Returns the character sequence used by operating system to separate lines in text files. This is operating system specific. It can be '\n', '\r' or "\r\n".
"os.arch"
Returns the operating system architecture
"os.name"
Returns the name of the Operating system
"os.version"
Returns the version of the Opposing system.
"path.separator"
Returns path separator character used in java.class.path
"user.dir"
Returns the path to the working directory of current user in the computer.
"user.home"
Returns the path to the home directory of current user.
"user.name"
Returns the name of current user (user account name)