Java provides several system-level operations that can be accessed through the System
class, which is part of the java.lang
package. These operations include system exit, time measurement, garbage collection, and retrieving environment variables and properties. Let’s explore some of the most commonly used system-level operations:
1. Exiting the JVM: System.exit()Â
The System.exit()
method terminates the currently running Java Virtual Machine (JVM). It takes an integer status code as a parameter. A status of 0
indicates normal termination, while a non-zero status indicates abnormal termination.
public class ExitExample { public static void main(String[] args) { System.out.println("Program is running..."); System.exit(0); System.out.println("This will not be printed."); } } /* Program is running... */
2. Measuring Time: System.currentTimeMillis()
The System.currentTimeMillis()
method returns the current time in milliseconds since the epoch (January 1, 1970, 00:00:00 GMT). This is useful for measuring elapsed time or tracking application performance.
public class TimeMeasurement { public static void main(String[] args) { long startTime = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { // Some time-consuming operation } long endTime = System.currentTimeMillis(); System.out.println("Elapsed Time: " + (endTime - startTime) + " milliseconds"); } }
3. Triggering Garbage Collection: System.gc()
The System.gc()
method is a suggestion to the JVM to perform garbage collection. It is not guaranteed that the garbage collector will actually run immediately.
Example:
public class GarbageCollection { public static void main(String[] args) { String str = new String("Garbage Collection Example"); str = null; System.gc(); System.out.println("Garbage collection requested."); } }
4. Retrieving System Properties: System.getProperty()
The System.getProperty()
method is used to fetch system properties such as the Java version, OS name, user directory, and more.
public class SystemProperties { public static void main(String[] args) { String javaVersion = System.getProperty("java.version"); String osName = System.getProperty("os.name"); String userDir = System.getProperty("user.dir"); System.out.println("Java Version: " + javaVersion); System.out.println("OS Name: " + osName); System.out.println("User Directory: " + userDir); } }
5. Retrieving Environment Variables: System.getenv()
The System.getenv()
method retrieves the value of environment variables. It returns a map of all environment variables or a specific variable when a key is specified.
Example:
public class EnvironmentVariables { public static void main(String[] args) { String path = System.getenv("PATH"); System.out.println("PATH: " + path); // Print all environment variables System.getenv().forEach((key, value) -> System.out.println(key + " = " + value)); } }
6. Terminating the Program with Error Status: System.exit() with Non-Zero Status
A non-zero status code indicates abnormal termination. This can be useful for indicating specific exit conditions to external systems.
public class ExitWithError { public static void main(String[] args) { try { int number = Integer.parseInt("ABC"); // Will cause NumberFormatException } catch (NumberFormatException e) { System.err.println("Invalid number format. Exiting..."); System.exit(1); } } } /* Invalid number format. Exiting... */
System-level operations in Java provide essential methods for interacting with the operating system, managing the JVM, and retrieving system information. The System
class includes methods for time measurement, program termination, garbage collection, and accessing environment variables and properties. Understanding these operations is crucial for effective resource management and application monitoring in Java.