Usage of system class in Java

Though System class cannot be instantiated, it contains lot of useful fields, methods and properties. Using this class we can get lot of system properties which can give information about Operating system like OS name, OS architecture and OS version, Java Specification information such as Java Version, Java Name, Java Vendor etc..
System class also gives information about Separator scheme and User such as User Name, User's Home Dir etc. If there is no current set of properties then a set of such properties is created and initialized.

Lets see the program below which use System class to print the information about OS, java specifications and User.



public class SystemUsage {


public static void main(String[] args){

//Few Important Usage of System class

System.out.println("\n\nOS INFORMATION : ");


String strOSName = System.getProperty("os.name");

System.out.println("OS Name " +strOSName);

String strOSArch = System.getProperty("os.arch");

System.out.println("OS Architecture " +strOSArch);

String strOSVersion = System.getProperty("os.version");

System.out.println("OS Version " +strOSVersion);


System.out.println("\n\nJAVA INFORMATION : ");

String strJavaVersion = System.getProperty("java.specification.version");

System.out.println("Java VERSION : " + strJavaVersion);

String strJavaVendor = System.getProperty("java.specification.vendor");

System.out.println("Java VENDOR : " + strJavaVendor);

String strJavaName = System.getProperty("java.specification.name");

System.out.println("Java Name : " + strJavaName);


System.out.println("\n\nSEPARATOR INFORMATION : ");

String strFileSeparator = System.getProperty("file.separator");

System.out.println("File Separator : " + strFileSeparator);

String strLineSeparator = System.getProperty("line.separator");

System.out.println("Line Separator : " + strLineSeparator);

String strPathSeparator = System.getProperty("path.separator");

System.out.println("Path Separator : " + strPathSeparator);



System.out.println("\n\nUSER INFORMATION : ");

String userNameStr = System.getProperty("user.name");

System.out.println("User Name : " + userNameStr);

String userHomeStr = System.getProperty("user.home");

System.out.println("User Home : " + userHomeStr);

String userDirStr = System.getProperty("user.dir");

System.out.println("User Directory : " + userDirStr);






}

}



Output on windows :

OS INFORMATION :
OS Name Windows XP
OS Architecture x86
OS Version 5.1


JAVA INFORMATION :
Java VERSION : 1.6
Java VENDOR : Sun Microsystems Inc.
Java Name : Java Platform API Specification


SEPARATOR INFORMATION :
File Separator : \
Line Separator :

Path Separator : ;


USER INFORMATION :
User Name : Admin
User Home : C:\Documents and Settings\Admin
User Directory : D:\


Output on Linux :

OS INFORMATION :
OS Name Linux
OS Architecture i386
OS Version 2.6.18-164.11.1.el5PAE


JAVA INFORMATION :
Java VERSION : 1.4
Java VENDOR : Sun Microsystems Inc.
Java Name : Java Platform API Specification


SEPARATOR INFORMATION :
File Separator : /
Line Separator :

Path Separator : :

USER INFORMATION :
User Name : root
User Home : /root
User Directory : /usr/java/j2sdk1.4.2_19/bin
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment