In this section of example we are going to describe you how to fetch the class directory's path by the class name.
Java get class directory
In this section of example we are going to describe you how to fetch the class directory's path by the class name. To get the class directory's name and its path we have used two methods getClass() and getResource() over an object instance.
We have created a class GetClassDirectory and to get the class of an object we have created a new instance of Object class and then we have called the getClass() method and to get the path of that class directory we have used getResource() method.
URL url=new Object().getClass().getResource("Object.class");
new Object().getClass().getResource("Object.class") returns an object of type java.net.URL.Here is the example code of GetClassDirectory.java as follows:
GetClassDirectory.java
import java.util.*;
|
Output:
C:\javaexamples>javac GetClassDirectory.java C:\javaexamples>java GetClassDirectory jar:file:/C:/Java/jdk1.6.0_03/jre/lib/rt.jar!/java/lang/Object.class |