How do I get the username of operating system active user?

This example show you how to get operating system active user’s login name. We can obtain the username of current user by reading system properties using the user.name key.

package org.kodejava.lang;

public class GettingUserName {
    public static void main(String[] args) {
        String username = System.getProperty("user.name");
        System.out.println("username = " + username);
    }
}

How do I clear system property?

System.clearProperty(String key) method enables you to remove a system property. The key must not be an empty string or a null value because it will cause the method to throw an IllegalArgumentException or a NullPointerException.

It will also check if a SecurityManager exists and if you don’t have a write permission to the system property a SecurityException is going to be thrown.

package org.kodejava.lang;

public class ClearProperty {
    public static void main(String[] args) {
        String key = "user.dir";
        System.out.println(key + " = " + System.getProperty(key));

        // The System.clearProperty() method available since Java 1.5
        System.clearProperty(key);
        System.out.println(key + " = " + System.getProperty(key));
    }
}

The code snippet above give us the following output:

user.dir = F:\Wayan\Kodejava\kodejava-example
user.dir = null

How do I get file separator symbol?

Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use "\" (backslash) while Linux use "/" (forward slash).

To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.

package org.kodejava.lang;

public class FileSeparatorExample {
    public static void main(String[] args) {
        // file.separator system property return the correct file 
        // separator for each different platform (Windows = \), 
        // (Linux = /)
        String dataFolder = System.getProperty("user.dir") +
                System.getProperty("file.separator") + "data";

        System.out.println("Data Folder = " + dataFolder);
    }
}

How do I read system property as an integer?

The Integer.getInteger() methods allows us to easily read system property and convert it directly to Integer object. This method call the System.getProperty() method and then convert it to integer by calling the Integer.decode() method.

package org.kodejava.lang;

public class IntegerProperty {
    public static void main(String[] args) {
        // Add properties to the system. In this example we create a
        // dummy major and minor version for our application.
        System.setProperty("app.major.version", "2021");
        System.setProperty("app.minor.version", "9");

        // In the code below we use the Integer.getInteger() method to
        // read our application version from the value specified in the
        // system properties.
        Integer major = Integer.getInteger("app.major.version");
        Integer minor = Integer.getInteger("app.minor.version");
        System.out.println("App version = " + major + "." + minor);
    }
}

The output of the code snippet:

App version = 2021.9

How can I get current working directory?

A system properties named user.dir can be used if you want to find the current working directory of your Java program.

package org.kodejava.io;

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        // System property key to get current working directory.
        String USER_DIR_KEY = "user.dir";
        String currentDir = System.getProperty(USER_DIR_KEY);

        System.out.println("Working Directory: " + currentDir);
    }
}

Result example:

Working Directory: F:\Wayan\Kodejava\kodejava-example