System Property cookbook

By Maurizio Farina | Posted on September 2017

The following cookbook show java example to get common system property.

The java statement to retrieve system folder is the following:

1
System.getProperty("java.io.tmpdir");
System Property Brief Description
java.io.tmpdir On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp";
on Microsoft systems is TMP system variable typically USER_FOLDER\AppData\Local\Temp\
file.separator "/" on UNIX and "\" on Windows
line.separator line separator in text files
os.arch Operating system architecture
os.name Operating system name
os.version Operating system version
java.class.path classpath folder
java.home Java Runtime Environment (JRE) folder
user.dir User working directory
user.home User home directory
user.name User account name

The following code example might be useful to show all the system properties used when application is running:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.listfeeds;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SystemPropertiesTest {

    //define new log4j report via slf4j logger factory
    private static final Logger log = LoggerFactory.getLogger(SystemPropertiesTest.class);

    @Test
    public void TestAll() {

        log.info("java.io.tmpdir   [{}]", System.getProperty("java.io.tmpdir"));
        log.info("file.separator   [{}]", System.getProperty("file.separator"));
        log.info("line.separator   [{}]", System.getProperty("line.separator"));
        log.info("os.arch          [{}]", System.getProperty("os.arch"));
        log.info("os.name          [{}]", System.getProperty("os.name"));
        log.info("os.version       [{}]", System.getProperty("os.version"));
        log.info("java.class.path  [{}]", System.getProperty("java.class.path"));
        log.info("java.home        [{}]", System.getProperty("java.home"));
        log.info("user.dir         [{}]", System.getProperty("user.dir"));
        log.info("user.home        [{}]", System.getProperty("user.home"));
        log.info("user.name        [{}]", System.getProperty("user.name"));

    }
}