Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

getting the url of a class

Thread view: 
Aryeh M. Friedman - 22 Nov 2007 23:56 GMT
If I have a class that is already loaded how do I figure out from what
URL it was loaded from (actually local filename is all I care about)?
Arne Vajhøj - 23 Nov 2007 03:37 GMT
> If I have a class that is already loaded how do I figure out from what
> URL it was loaded from (actually local filename is all I care about)?

    private String getPath(Class cls) {
        String cn = cls.getName();
        String rn = cn.replace('.', '/') + ".class";
        String path =
getClass().getClassLoader().getResource(rn).getPath();
        int ix = path.indexOf("!");
        if(ix >= 0) {
            return path.substring(0, ix);
        } else {
            return path;
        }
    }

Arne
Owen Jacobson - 23 Nov 2007 03:43 GMT
> If I have a class that is already loaded how do I figure out from what
> URL it was loaded from (actually local filename is all I care about)?

Assuming that the class was loaded from a file at all, you can get that
information from the class's ProtectionDomain.  Have a look at
<http://java.sun.com/javase/6/docs/api/java/security/ProtectionDomain.html>
particularly the getCodeSource() method.

-O
Roedy Green - 23 Nov 2007 08:24 GMT
On Thu, 22 Nov 2007 15:56:46 -0800 (PST), "Aryeh M. Friedman"
<Aryeh.Friedman@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>If I have a class that is already loaded how do I figure out from what
>URL it was loaded from (actually local filename is all I care about)?

see http://mindprod.com/jgloss/protectiondomain.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.