[an error occurred while processing this directive]

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:


import java.util.*;
import java.lang.*;
import java.net.*;

public class GetClassDirectory
  public static void main(String args[]) {
    URL url=new Object().getClass().getResource("Object.class");


C:\javaexamples>javac GetClassDirectory.java

C:\javaexamples>java GetClassDirectory

Download Source Code



[an error occurred while processing this directive]