Java System Class in the java.lang Package
The System class has properties and methods related to the runtime environment. It provides class variables for standard input and output using out and in, error output using err, methods for copying objects, and useful methods that can be used when writing programs.
System Variables
| Variable | Description |
|---|---|
| final static InputStream in | Used for standard input. |
| final static PrintStream out | Used for standard output. It receives print() and println() parameters and outputs them to the monitor. |
| final static PrintStream err | Used for error output. It receives print() and println() parameters and outputs errors to the monitor. |
Main System Methods
| Method | Description |
|---|---|
| static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) | Copies an array. src and dest are the array variables to copy from and to, srcPos and destPos are the positions where copying starts, and length is the size of the array to copy. |
| static long currentTimeMillis() | Returns the time from midnight on January 1, 1970 to the present in milliseconds. |
| static void exit(int status) | Terminates the currently running application. Returns the termination status as status. Usually, a negative value means abnormal termination. |
| static void gc() | Runs garbage collection. |
| static Properties getProperties() | Gets the list of system environment variables. |
| static String getProperty(String key) | Gets a system environment variable. |
| static String getProperty(String key, String def) | Gets a system environment variable with a default value if it does not exist. |
System Examples
Example) System.currentTimeMillis()
The following is an example of the System.currentTimeMillis() method.
package com.devkuma.tutorial.java.lang;
public class SystemCurrentTimeMillis {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
System.out.println("start time : " + System.currentTimeMillis());
Thread.sleep(1000); // Stop for 1 second
long end = System.currentTimeMillis();
System.out.println("end time : " + System.currentTimeMillis());
System.out.println("duration : " + (end - start));
}
}
The execution result is as follows.
start time : 1497970891769
end time : 1497970892771
duration : 1002
Example) System.arraycopy()
The following is an example of the System.arraycopy() method.
package com.devkuma.tutorial.java.lang;
public class SystemArraycopy {
public static void main(String[] args) {
char[] src = { 'a', 'b', 'c', 'd', 'e' };
char[] dest = { 'x', 'x', 'x', 'x', 'x' };
System.out.println("src=" + new String(src));
System.out.println("dest=" + new String(dest));
System.arraycopy(src, 0, dest, 0, src.length);
System.out.println();
System.out.println("src=" + new String(src));
System.out.println("dest=" + new String(dest));
}
}
The execution result is as follows.
src=abcde
dest=xxxxx
src=abcde
dest=abcde
Example) System.getProperties(), System.getProperty()
The following is an example of the System.getProperties() and System.getProperty() methods related to system environment variables.
package com.devkuma.tutorial.java.lang;
import java.util.Enumeration;
import java.util.Properties;
public class SystemProperties {
public static void main(String[] args) {
// Java Runtime Environment version
System.out.println("java.version=" + System.getProperty("java.version"));
// Java Runtime Environment vendor
System.out.println("java.vendor=" + System.getProperty("java.vendor"));
// Java Runtime Environment vendor URL
System.out.println("java.vendor.url=" + System.getProperty("java.vendor.url"));
// Java Runtime Environment installation directory
System.out.println("java.home=" + System.getProperty("java.home"));
// Java virtual machine specification
System.out.println("java.vm.specification.version=" + System.getProperty("java.vm.specification.version"));
// Java virtual machine specification vendor
System.out.println("java.vm.specification.vendor=" + System.getProperty("java.vm.specification.vendor"));
// Java virtual machine specification name
System.out.println("java.vm.specification.name=" + System.getProperty("java.vm.specification.name"));
// Java virtual machine implementation version
System.out.println("java.vm.version=" + System.getProperty("java.vm.version")); //
// Java virtual machine implementation vendor
System.out.println("java.vm.vendor=" + System.getProperty("java.vm.vendor")); //
// Java virtual machine implementation name
System.out.println("java.vm.name=" + System.getProperty("java.vm.name"));
// Java Runtime Environment specification version
System.out.println("java.specification.version=" + System.getProperty("java.specification.version"));
// Java Runtime Environment specification vendor
System.out.println("java.specification.vendor=" + System.getProperty("java.specification.vendor"));
// Java Runtime Environment specification name
System.out.println("java.specification.name=" + System.getProperty("java.specification.name"));
// Java class format version number
System.out.println("java.class.version=" + System.getProperty("java.class.version"));
// Java class path
System.out.println("java.class.path=" + System.getProperty("java.class.path"));
// List of paths searched when loading libraries
System.out.println("java.library.path=" + System.getProperty("java.library.path"));
// Default temporary file path
System.out.println("java.io.tmpdir=" + System.getProperty("java.io.tmpdir"));
// Name of the JIT compiler in use
System.out.println("java.compiler=" + System.getProperty("java.compiler"));
// Extension directory path
System.out.println("java.ext.dirs=" + System.getProperty("java.ext.dirs"));
// operating system name
System.out.println("os.name=" + System.getProperty("os.name"));
// operating system architecture
System.out.println("os.arch=" + System.getProperty("os.arch"));
// operating system version
System.out.println("os.version=" + System.getProperty("os.version"));
// file separator ("/" on UNIX)
System.out.println("file.separator=" + System.getProperty("file.separator"));
// path separator (":" on UNIX)
System.out.println("path.separator=" + System.getProperty("path.separator"));
// line separator ("\n" on UNIX)
System.out.println("line.separator=" + System.getProperty("line.separator"));
// user name
System.out.println("user.name=" + System.getProperty("user.name"));
// user home directory
System.out.println("user.home=" + System.getProperty("user.home"));
// user's current working directory
System.out.println("user.dir=" + System.getProperty("user.dir"));
System.out.println();
// Query all information
Properties prop = System.getProperties();
Enumeration<?> enu = prop.keys();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
String value = (String) prop.get(key);
System.out.println(key + "=" + value);
}
}
}
The execution result is as follows.
java.version=1.8.0_161
java.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
java.vm.specification.version=1.8
java.vm.specification.vendor=Oracle Corporation
java.vm.specification.name=Java Virtual Machine Specification
java.vm.version=25.161-b12
java.vm.vendor=Oracle Corporation
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.specification.version=1.8
java.specification.vendor=Oracle Corporation
java.specification.name=Java Platform API Specification
java.class.version=52.0
java.class.path=/Library/Java/JavaVirtualMachines...(omitted)...
java.library.path=/Users/kimkc/Library/Java/Extensions:...(omitted)...
java.io.tmpdir=/var/folders/f2/s8f8bt7j2vdf4538y4lydp480000gn/T/
java.compiler=null
java.ext.dirs=/Users/kimkc/Library/Java/Extensions:...(omitted)...
os.name=Mac OS X
os.arch=x86_64
os.version=10.13.6
file.separator=/
path.separator=:
line.separator=
user.name=kimkc
user.home=/Users/kimkc
user.dir=/Users/kimkc/dev/workspace/java-tutorial
java.runtime.name=Java(TM) SE Runtime Environment
...
sun.cpu.isalist=