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 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java get class directory View All Comments
Post your Comment