You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xml-commons-dev@xerces.apache.org by Norman Walsh <nd...@nwalsh.com> on 2003/02/17 20:55:20 UTC

What jar file was a class loaded from?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Is it possible for a class to learn the location (URI) of the jar file
from which it was loaded?

                                        Be seeing you,
                                          norm

- -- 
Norman.Walsh@Sun.COM    | Between the ages of twenty and forty we are
XML Standards Architect | engaged in the process of discovering who we
Web Tech. and Standards | are, which involves learning the difference
Sun Microsystems, Inc.  | between accidental limitations which it is
                        | our duty to outgrow and the necessary
                        | limitations of our nature beyond which we
                        | cannot trespass with impunity.--W. H. Auden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.7 <http://mailcrypt.sourceforge.net/>

iD8DBQE+UT4oOyltUcwYWjsRAvj0AKCLUyXcdVCuwgrc/mfeXhsd2Qd8mgCglXfs
wrS9mOBje89elWqep5Zkf9I=
=/oX4
-----END PGP SIGNATURE-----

Re: What jar file was a class loaded from?

Posted by Stefan Bodewig <bo...@apache.org>.
On Tue, 18 Feb 2003, Brian Smith <br...@uiowa.edu> wrote:

> I know that it doesn't work in NetBeans because NetBeans has a
> complex classloader system that uses some URL scheme "nbres://" or
> similar.

That only means that the code will know it cannot deal with the URL.

Norm wants the name of the jar file, the URL alone is not enough for
him.  I don't see how you'd do that for an URL scheme you don't know
anything about.

> So, any code (e.g. the <antlr> Ant task) that uses [if
> (location.startsWith("jar"))] fails.

Patches more than welcome 8-)

Stefan

Re: What jar file was a class loaded from?

Posted by Brian Smith <br...@uiowa.edu>.
Stefan Bodewig wrote:
> On Mon, 17 Feb 2003, Norman Walsh <nd...@nwalsh.com> wrote:
> If URL starts with "jar:file:", the jar's name is after that prefix.
> If it starts with "file:", you are looking at a directory.  In JDK 1.1
> it will start with "zip:" for jars IIRC and you are out of luck when
> it comes to the file name as Java uses some internal counter instead
> of the name in the URL.
> 
> I'm not sure about jars loaded from the network, but this snippet here
> 
>             if (location.startsWith("jar")) {
>                 url = ((java.net.JarURLConnection) url.openConnection()).getJarFileURL();
>                 location = url.toString();
>             }
> 
> stolen from Ant which has stolen it from Axis IIUC seems to be a more
> general solution for JDK >= 1.2.

I do not think that this is a good way to do things because it assumes 
too much about the classloader. In particular, I know that it doesn't 
work in NetBeans because NetBeans has a complex classloader system that 
uses some URL scheme "nbres://" or similar. So, any code (e.g. the 
<antlr> Ant task) that uses [if (location.startsWith("jar"))] fails.

- Brian


Re: What jar file was a class loaded from?

Posted by Stefan Bodewig <bo...@apache.org>.
On Mon, 17 Feb 2003, Norman Walsh <nd...@nwalsh.com> wrote:

> Is it possible for a class to learn the location (URI) of the jar
> file from which it was loaded?

The general way (works with JDK 1.1): try to load the class file as
resource i.e. 
"/" + fully-qualified-classname.replace('.', '/') + ".class"
and go from there via something like

class.getClassLoader().getResource()

With JDK 1.4 you can go via class.getProtectionDomain().getCodeSource().

If URL starts with "jar:file:", the jar's name is after that prefix.
If it starts with "file:", you are looking at a directory.  In JDK 1.1
it will start with "zip:" for jars IIRC and you are out of luck when
it comes to the file name as Java uses some internal counter instead
of the name in the URL.

I'm not sure about jars loaded from the network, but this snippet here

            if (location.startsWith("jar")) {
                url = ((java.net.JarURLConnection) url.openConnection()).getJarFileURL();
                location = url.toString();
            }

stolen from Ant which has stolen it from Axis IIUC seems to be a more
general solution for JDK >= 1.2.

Stefan

Re: What jar file was a class loaded from?

Posted by Norman Walsh <nd...@nwalsh.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

/ Norman Walsh <nd...@nwalsh.com> was heard to say:
| Is it possible for a class to learn the location (URI) of the jar file
| from which it was loaded?

Thanks for the answers. Here's the problem I'm struggling with:

It's been pointed out that the resolver doesn't have any built-in
knowledge about the DTD for catalogs. That's an obvious catch-22 and
I'd like to fix it.

One obvious way is to put a catalog for the catalog document type in
the Jar file and then put the URI for that catalog at the head of the
path.

But that means the code has to be able to figure that out, which
apparently isn't easy.

Suggestions?

                                        Be seeing you,
                                          norm

- -- 
Norman.Walsh@Sun.COM    | Old and young, we are all on our last
XML Standards Architect | cruise.--Robert Louis Stevenson
Web Tech. and Standards |
Sun Microsystems, Inc.  | 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.7 <http://mailcrypt.sourceforge.net/>

iD8DBQE+U9XxOyltUcwYWjsRAj3tAJ0VoeGWiQntYCwzaaOOH+FNmohi0ACeLURs
2WGU41VqPS/CXKnX3hYYz2I=
=wbfS
-----END PGP SIGNATURE-----