Connecting Tech Pros Worldwide Forums | Help | Site Map

Get Path Where A Class Is Located

Hal Vaughan
Guest
 
Posts: n/a
#1: Jul 18 '05
I have a program that consists of only ONE Java class.

Is there any way, from within that class, that I can find where the class is
on disk? In other words, can I find the path for a loaded class?

Thanks!

Hal

A J Gage
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Get Path Where A Class Is Located


>[color=blue]
> Is there any way, from within that class, that I can find where the class is
> on disk? In other words, can I find the path for a loaded class?[/color]


YEP!

OK, first you need to get the String value of your classpath. Can be
done using System.getProperty(String key), pretty sure the key you
need is "java.class.path".
Then tokenize the String (containing classpath) using
File.pathSeparator as delimeter.
foreach token (path element)
File f = new File( token );
String[] list = f.list();
search list array for ClassName.class

I needed to do something similar for a jEdit plugin, I don't have code
on this machine right now, but that is pretty mush how it was done.

If you like, I could email you the class that I used. I split
different jobs between methods.

This can be used to get arbitrary class (not in a jar).

But if you only have 1 class then it is probably locating in the
working directory, so you might want to try:
File f = new File(".");
String path = f.getAbsolutePath();
Joel.Kamentz@sas.com
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Get Path Where A Class Is Located


First of all, there is no guaranteed way of doing this, as the class
might be loaded over the network and not exist anywhere on the local
disk.

However, if your code is privileged,
getClass().getProtectionDomain().getCodeSource().g etLocation() returns
a URL which is the equivalent of a classpath entry. That URL might
refer to a directory, a jar file or something else and could be on the
local machine or remote.

I don't see the original message with the original question. If all
you want to do is load a .properties file or an image or something
relative to the class' location, then Class.getResource and
ClassLoader.getResource are better approaches.

Good luck.

Note that I don't check this group often, and so see or respond to
follow up posts.

Joel


On 15 Sep 2004 22:03:02 -0700, gagey71@hotmail.com (A J Gage) wrote:
[color=blue][color=green]
>>
>> Is there any way, from within that class, that I can find where the class is
>> on disk? In other words, can I find the path for a loaded class?[/color]
>
>
>YEP!
>
>OK, first you need to get the String value of your classpath. Can be
>done using System.getProperty(String key), pretty sure the key you
>need is "java.class.path".
>Then tokenize the String (containing classpath) using
>File.pathSeparator as delimeter.
>foreach token (path element)
> File f = new File( token );
> String[] list = f.list();
> search list array for ClassName.class
>
>I needed to do something similar for a jEdit plugin, I don't have code
>on this machine right now, but that is pretty mush how it was done.
>
>If you like, I could email you the class that I used. I split
>different jobs between methods.
>
>This can be used to get arbitrary class (not in a jar).
>
>But if you only have 1 class then it is probably locating in the
>working directory, so you might want to try:
> File f = new File(".");
> String path = f.getAbsolutePath();[/color]

Closed Thread