How do I get the path from where a class is loaded?

This examples shows how to get a path name or location from where our class is loaded.

package org.kodejava.example.lang;

public class CodeSourceLocation {
    public static void main(String[] args) {
        CodeSourceLocation csl = new CodeSourceLocation();
        csl.getCodeSourceLocation();
    }

    private void getCodeSourceLocation() {
        // The the location from where the class is loaded.
        System.out.println("Code source location: " +
            getClass().getProtectionDomain().getCodeSource().getLocation());
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA. Support me by donating >> here <<.

Leave a Reply