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.example.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: /Users/wsaryada/Projects/kodejava

How do I get user home directory name?

package org.kodejava.example.lang;

public class UserHomeExample {
    public static void main(String[] args) {
        // This is the key that we used to obtain user home directory
        // in the operating system
        String userHome = "user.home";

        // We get the path by getting the system property with the 
        // defined key above.
        String path = System.getProperty(userHome);

        // We print your home path here
        System.out.println("Your Home Path: " + path);
    }
}

On my machine it print something like:

Your Home Path: /Users/wsaryada

How do I get operating system name and version?

package org.kodejava.example.lang;

public class OperatingSystemInfo {
    public static void main(String[] args) {
        // The key for getting operating system name
        String name = "os.name";
        // The key for getting operating system version
        String version = "os.version";
        // The key for getting operating system architecture
        String architecture = "os.arch";

        System.out.println("Name: " + System.getProperty(name));
        System.out.println("Version: " + System.getProperty(version));
        System.out.println("Arch: " + System.getProperty(architecture));
    }
}

Below is the example result of our program, of course it could be different from what you’ll see in your machine because it depends on the operating system that you use.

Name: Windows XP
Version: 5.1
Architecture: x86

How do I get operating system temporary directory / folder?

package org.kodejava.example.lang;

public class TempDirExample {
    public static void main(String[] args) {
        // This is the property name for accessing OS temporary directory
        // or folder.
        String property = "java.io.tmpdir";

        // Get the temporary directory and print it.
        String tempDir = System.getProperty(property);
        System.out.println("OS temporary directory is " + tempDir);
    }
}

How do I get Java Runtime Environment (JRE) version?

This code snippet show you how to get JRE version by reading the java.version from the system properties. We use the System.getProperty(String key) method to get the value of the property and it will be returned as a String value.

package org.kodejava.example.lang;

public class JavaVersion {
    public static void main(String[] args) {
        // You can get the version of Java Runtime Environment
        // running on your machine by reading the "java.version"
        // property value from the system.
        String version = System.getProperty("java.version");

        // Just show to the world your JRE version :)
        System.out.println("JRE Version = " + version);
    }
}

In my computer it print the following output:

JRE Version = 1.8.0_20