System Class in Java - Properties, System.out.println() With Examples

System class in java is one of the core classes and I have never seen any java developer who doesn’t use it. One of the easiest way to log information for debugging is System.out.print() function. System class is final and all of it’s members and methods are static so that we can’t subclass and override it’s behavior through inheritance.

System class in java doesn’t provide any public constructors. So we can’t instantiate this class (for argument sake, we can instantiate it using Java Reflection) and that’s why all of it’s methods are static.

Here we will look into the different features provided by java.lang.System class.

  • Java System Array Copy

    Java System class provides a native method for copying data from one array to another. This is a native implementation and supposed to be faster than other ways to copy array data.

    System array copy method throws IndexOutOfBoundsException if copying would cause access of data outside array bounds. It also throws ArrayStoreException if an element in the source array could not be stored into the destination array because of a type mismatch and NullPointerException if either source or destination array is null.

    Below example program shows how to use this method.

    int [] array1 = {1,2,3,4,5};
    int[] array2 = {10,20,30,40,50};
    //copying first two elements from array1 to array2 starting from index 2 of array2
    System.arraycopy(array1, 0, array2, 2, 2);
    System.out.println(Arrays.toString(array2)); //prints "[10, 20, 1, 2, 50]"


  • Java System Properties

    System class contains useful method to get the list of System properties, get specific property, set system property and clear any existing property. The sample program below shows different methods and it’s usage.

    //Get System Defined Properties
    Properties systemProps = System.getProperties();
    Set<Object> keySet = systemProps.keySet();
    for(Object obj : keySet){
    	String key = (String) obj;
    //Get Specific Property
    //Clear property example
    System.out.println(System.getProperty("")); //print null
    //Set System property
    System.setProperty("", "IN");
    System.out.println(System.getProperty("")); //prints "IN"

    If we run above code in a java program, we get following output. Notice that output will vary based on your system configurations.

    { SE Runtime Environment}
    {java.vm.vendor=Oracle Corporation}
    { HotSpot(TM) 64-Bit Server VM}
    { Virtual Machine Specification}
    {java.vm.specification.vendor=Oracle Corporation}
    { OS X}
    { Platform API Specification}
    { 64-Bit Tiered Compilers}
    {java.specification.vendor=Oracle Corporation}
    { mode}
    {java.vendor=Oracle Corporation}


    Check out Java Properties example tutorial.

  • Reading and Writing to Console

    Java System Class provide a method to get the unique Console object associated with the running JVM.

    Console class was introduced in Java IO in 1.6 and provides useful method to print formatted data and read password securely.

    If no console is associated with the current JVM, for example running through Eclipse or running as background program, then it returns null.

    Below sample program show get Console object from System class and use it.

    Console console = System.console();
    if(console != null){
        Calendar c = new GregorianCalendar();
        console.printf("Welcome %1$s%n", "Pankaj"); //prints "Welcome Pankaj"
        console.printf("Current time is: %1$tm %1$te,%1$tY%n", c); //prints "Current time is: 08 5,2013"
    } else{
    	//No console is attached when run through Eclipse, background process
    	System.out.println("No Console attached");



  • java System Get Current Time

    System class in java provide two methods to get the current time in milliseconds and nano time.

    We can use time in milliseconds to create Date object, nano time is used mostly in scientific experiments or in benchmark testing.

    Below code snippet show the usage of System class methods to get time related information.

    long currentTimeMillis = System.currentTimeMillis();
    Date date = new Date(currentTimeMillis);
    System.out.println("Current time in millis="+currentTimeMillis);
    System.out.println(date); //prints 2013-08-05
    long nanoTime = System.nanoTime();
    System.out.println("Current nano time="+nanoTime);


  • Java System Environment Variables

    Java System class provides a method to get the environment variables data in Map form, the returned Map is unmodifiable and it contains key-value pairs in String object.

    //get unmodifiable environment variables map
    Map<String, String> envMap = System.getenv();
    Set<String> keySet = envMap.keySet();
    for(String key : keySet){
    //Get Specific environment variable
    String pathValue = System.getenv("PATH");

    java system environment variables, system in java

  • Security Manager

    SecurityManager class is used to implement security policy for applications, System class provide useful methods to get SecurityManager for the currently running JVM and to set the SecurityManager for the application.

    SecurityManager secManager = System.getSecurityManager();
    if(secManager == null){
    	System.out.println("SecurityManager is not configured");
    SecurityManager mySecManager = new SecurityManager();
    secManager = System.getSecurityManager();
    if(secManager != null){
    	System.out.println("SecurityManager is configured");


  • File IO Operations

    System class contains three fields – in, out and err. They are used to read data from InputStream and to write data to OutputStream.

    System class provide methods to set different types of InputStream and OutputStream to be used for logging purposes.

    For example, we can set FileOutputStream to out and err fields so that console output is written to file.

    Below code snippet shows the usage of these fields and how can we set them using setter methods.

    try(FileInputStream fis = new FileInputStream("input.txt");
    	FileOutputStream fos = new FileOutputStream("server.log");) {
    //set input stream
    char c = (char);
    System.out.print(c); //prints the first character from input stream
    //set output stream
    System.setOut(new PrintStream(fos));
    System.out.write("Hi Pankajn".getBytes());
    //set error stream
    System.setErr(new PrintStream(fos));
    System.err.write("Exception messagen".getBytes());
    } catch (IOException e) {

    Notice the use of Java 7 try with resources feature in above try-block.

  • Miscellaneous Tasks

    System class provides some other methods for miscellaneous tasks. For example, to run Garbage Collector, load external libraries, map the library name to OS specific String, run the finalize method for any object waiting for finalization and to terminate the JVM.

    //run the garbage collector
    System.out.println("Garbage collector executed.");
    //map library name
    String libName = System.mapLibraryName("");
    System.out.println(" library="+libName);
    //load external libraries
    //run finalization
    //terminates the currently running JVM
    // this line will never print because JVM is terminated
    System.out.println("JVM is terminated");

That’s all about System class in java, I hope it will help you in getting most out of java System class.

By admin

Leave a Reply

%d bloggers like this: