How do I get Java Home directory?

To get Java Home directory we can obtain it from system properties using the java.home key.

package org.kodejava.example.lang;

public class JavaHomeDirectory {
    public static void main(String[] args) {
        String javaHome = System.getProperty("java.home");
        System.out.println("JAVA HOME = " + javaHome);
    }
}

On my computer this code give me the following output:

JAVA HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me by donating.

Leave a Reply