• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Locating a class file path from other class

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all,
I have to write a program to get the original path of the class file.That is, my program will get the location of the given file not in the current directory or the same class executing the program.Bcoz, i read many pages returning the codings of getting the location of the running class.Many suggested to use



Both the things will return the file path of myClass.class. But i want to get any class file in random location and should return its path.
Like my1.class is the class grasping the file location in some C:\MyDir\my1.class

Some file called my2.class in D:\ or C:\MyDir\MyClass\ or some other location

Pls help me to get it in a rite way.

Thanks in Advance,
Mythily.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

For every loaded class, there's an instance of 'java.lang.Class' which represents it. The method getClass() returns the java.lang.Class object for an instance of that class. You're calling getClass() on the current instance, so you're getting the location of the class where this code appears. To get the location of my.class, you need to use one of the following:

new my().getClass().getProtectionDomain()...

Class.forName("my").getProtectionDomain()...

my.class.getProtectionDomain()...

Any of these three will call getProtectionDomain() on the java.lang.Class instance for the class "my". The second one is handy if you won't know the name of the class you're interested in until runtime, because there the class appears only as a String -- you could use a runtime variable there.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the Java Almanac on this, too.
 
mythily prakash
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I got it at last.This will find any file irrsepective of its location. Initially my Program starts it search for C drive only.



my special thanks to Mr.Santhosh who helped me all the way to get the coding.

Thanks,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic