Home Java Java-get-example Java get class directory

Related Tutorials


 
 

Share on Google+Share on Google+

Java get class directory

Advertisement
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.*;
import java.lang.*;
import java.net.*;

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

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

Download Source Code

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2008

Related Tutorials

Discuss: Java get class directory   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com