Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] finding out your classes base directory

Started by Preacher Mar 3, 2004 at 3:27 AM 7 replies 400+ views
Original Post
Preacher
Preacher
Hi just wondering how to find out what directory a class resides in programaticaly as i''m using a class loader and this sort of screws up relative addressing. i have tried to find methods for doing so in the ClassLoader itself and other classes such as File URL etc but to no avail a little help would me much appreciated
CaptainJester
CaptainJester

System.getProperty("user.dir");


Try this one out. It will return a String with a directory path. It should be the path the program was launched from.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

[edited by - CaptainJester on March 3, 2004 7:32:43 AM]
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
aidan_walsh
aidan_walsh
quote:
Original post by CaptainJester

System.getProperty("user.dir");



Uh, AFAIK that returns the users home directory...




SketchSoft OFFLINE | SketchNews OFFLINE | NewKlear Studios

www.aidanwalsh(.net)(.info)
Woody FX
Woody FX
Irish guys check out gamedevelopers.ie if your not already aware!

Brian






[edited by - Woody FX on March 3, 2004 1:28:38 PM]
Lucidquiet
Lucidquiet
I''ve been meaning to get around to doing the same thing, and this locating classes if in the working directory is fine, but what if you want to search a library for a specific class, or even just a specific method -- that would handy.

But the way I was going to do that was to use the zip and jar classes to search a jar/zip, then find the class that way, of course this brings up the question of how to get environmental variables like PATH, CLASSPATH, ANT_HOME, XERCES_HOME, CATALINA_HOME, etc...

Anybody have some ideas?

L-

" ''No one has control -- control is just a fantasy. And being human is difficult.'' "
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
CaptainJester
CaptainJester
quote:
Original post by doodle_sketch
quote:
Original post by CaptainJester

System.getProperty("user.dir");



Uh, AFAIK that returns the users home directory...




SketchSoft OFFLINE | SketchNews OFFLINE | NewKlear Studios




That one is user.home



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Lucidquiet
Lucidquiet
How do you determine where a .class file resides?

For a class your application uses

Class Object.getClass()
ProtectionDomain Class.getProtectionDomain()
CodeSource ProtectionDomain.getCodeSource()
URL CodeSource.getLocation()

You mentioned File URL -- but I think you were trying to say that you looked there for methods that would give you the location of the class file. So if this is the procedure you attempted then I''m sorry for the duplication. But if its not, then give this a try -- its very pseudocode-ish, but the classes/packages reside in the API.

L-

ps. I haven''t got a chance to test it myself, planned to do it the weekend of the 03/06/04, but it looks like I won''t get a chance. But I still wanted to pass it along. So please accept this with a grain of salt I haven''t, myself, done anything with it yet.

Hope this helps.


" ''No one has control -- control is just a fantasy. And being human is difficult.'' "
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.