Find Java Class Path in the System

Many times one has to know where in the system some Java class is located, usually when one has to perform file operations relative to the class location.

In Java such task is not straight forward to accomplish. Here’s a snippet that will return the full path of the class location:

import java.security.CodeSource;
import java.security.ProtectionDomain;

public class Utilities {
 protected static final String getApplicationLocation() {

    final ProtectionDomain pd = Utilities.class.getProtectionDomain();
    final CodeSource cs = pd.getCodeSource();

    return cs.getLocation().getPath().replaceFirst("/", "");
 }
}