Use of system class java

Use of system class java

System class is final class in java which extends Object class. This class contains many useful fields and methods which is very helpful when you need system specific information. Many facilities provided by this class i.e. standard output, input, error output streams, loading files and libraries, utility method to copy array, access to external defined properties and environment variables. Below are demo java class which shows some of the uses:

package com.javahonk.system;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SystemClassDemo {

    public static void main(String[] args) {
    
    //Find operating system name:
    System.out.println(System.getProperty("os.name"));
    
    //Find java version name
    System.out.println(System.getProperty("java.specification.version"));
    
    //Find user work directory
    System.out.println(System.getProperty("user.dir"));
    
    //Find user name
    System.out.println(System.getProperty("user.home"));
    
    //Find user home
    System.out.println(System.getProperty("user.home"));
    
    //Find file separator
    System.out.println("Path separator: "+System.
        getProperty("file.separator"));
    
    //Find time
    long timeInMilli = System.currentTimeMillis();
    Date date = new Date(timeInMilli);
    SimpleDateFormat simpleDateFormat = 
        new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
    System.out.println("Time in millisecond: "+timeInMilli);
    System.out.println("Formatted time: "+simpleDateFormat.format(date));
    
    //Terminates currently running JVM
    //This method calls the exit method in class Runtime. 
    //This method never returns normally. 

    System.exit(0);
    // is equivalent to below
    Runtime.getRuntime().exit(0);

    }

}

Output:
operatingsystem2

Leave a Reply

Your email address will not be published. Required fields are marked *