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 / September 2007

Tip: Looking for answers? Try searching our database.

How to find the JAR file from which a ava class and all successors had been loaded.

Thread view: 
Peter - 23 Sep 2007 15:33 GMT
Hi,

I am looking for some piece of java code which allows me to get the name
of the JAR file from which a java class (and all subsequent classes
which are used in this java class ) had been loaded by the classloader.

Any hint or idea or - even better - code is appreciated.

Thanks,

Peter
Andrew Thompson - 23 Sep 2007 16:17 GMT
...
>I am looking for some piece of java code which allows me to get the name
>of the JAR file from which a java class (and all subsequent classes
>which are used in this java class ) had been loaded ..

Class files might not be in a Jar file.  They might
be loose class files, or created on the fly.

Why do you think you need to know this information?
(Try and answer that in terms of "I want to offer the
end user 'X'", where X is something an end-user might
care about.)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Stefan Ram - 23 Sep 2007 16:23 GMT
>I am looking for some piece of java code which allows me to get the name
>of the JAR file from which a java class (and all subsequent classes
>which are used in this java class ) had been loaded by the classloader.

 For the standard class loader, the system resource
 corresponding to a class path (such as
 »java/lang/Object.class«) can be obtained via

http://download.java.net/jdk7/docs/api/java/lang/ClassLoader.html#getSystemResou
rce(java.lang.String
)
Lew - 23 Sep 2007 17:31 GMT
>> I am looking for some piece of java code which allows me to get the name
>> of the JAR file from which a java class (and all subsequent classes
[quoted text clipped - 5 lines]
>
> http://download.java.net/jdk7/docs/api/java/lang/ClassLoader.html#getSystemResou
rce(java.lang.String
)

That gets the system resource given the name.  The OP wants to get the name of
the JAR file from which a resource came, not the name of the resource,
assuming the resource came from a JAR, given the resource.

Signature

Lew

Real Gagnon - 23 Sep 2007 16:38 GMT
> I am looking for some piece of java code which allows me to get the name
> of the JAR file from which a java class (and all subsequent classes
> which are used in this java class ) had been loaded by the classloader.

"Determine if running from JAR"
http://www.rgagnon.com/javadetails/java-0391.html

Signature

Real Gagnon  from  Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html

Arne Vajhøj - 23 Sep 2007 18:32 GMT
> I am looking for some piece of java code which allows me to get the name
> of the JAR file from which a java class (and all subsequent classes
> which are used in this java class ) had been loaded by the classloader.
>
> Any hint or idea or - even better - code is appreciated.

    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


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



©2009 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.