From which Jar a Class was loaded?

Posted by Paolo Dona' Sun, 13 Nov 2005 15:11:00 GMT

Sometimes in production environments I face problems never encountered during development… It’s a general thing.. could happen with jdbc drivers or xml parsers.

I just feel classes are loaded from a different jar than expected.

This of course could happen if you’re deploying to a very different application server or if you’ve no control over the production server classpath.

I found in javaalmanac.com a code snippet that can help you identify which is the jar containing a specific Class at runtime:

Class cls = MyFoo.class;
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation(); 
System.out.println(loc); 
// prints something like "c:/jars/MyFoo.jar"

This way you can check if your class is loaded right from the expected jar, not elsewhere :-).

This has shown to be really useful during my sad production debug sessions.

Hope it can help you as well.

Tags , , ,  | 13 comments

Comments

  1. Avatar jug-33 said 30 days later:

    Perchè ci sono 4 pagine in italiano ed una inglese.

    I commenti li devo scrivere in Inglese o in Italiano. Per me sarebbe più semplice scrivere in italiano ma se mi ritrovo in un forum inglese scrivo in inglese , ma se il forum è italiano e si spaccia per inglese scrivo in italiano .

    Insomma inglese o tutto o niente (compresi i commenti)

  2. Avatar Lucio said 32 days later:

    The forum doesn’t pretend to be english. Simply we would like all people could read contents, even english people.

    Write as you want, italian or english, but if the post is i english, it would be polite to comment in english.

  3. Avatar jug-33 said 34 days later:

    hi there,

    I’m not a jug’s member but this is my thought.

    The developer must be to know the english, because all the documentation in the world is in english language and if we want to make grow the community and to go around the world write in english , yeh yeh of course sometimes we can mistake the grammer and we could to make a shit scenarious … but only the man without fear can to speak with god.

  4. Avatar Bert said 331 days later:

    Thx, this helped.

  5. Avatar Benoit Moussaud said 782 days later:

    Very Useful, thanks !

  6. Avatar Egor said 1155 days later:

    You are the man! Saved my day.

  7. Avatar 5 incredible sites to see in Prague said 1996 days later:

    Prague Castle is the world’s largest ancient castle and is actually a complex comprised of sixty-plus buildings. Plan to spend an entire day exploring the cathedrals, towers, palaces, statues, gates and more.5 incredible sites to see in Prague

  8. Avatar watches mens said 2003 days later:

    The developer must be to know the english, because all the documentation in the world is in english language and if we want to make grow the community and to go around the world write in english , yeh yeh of course sometimes we can mistake the grammer and we could to make a shit scenarious … but only the man without fear can to speak with god.

  9. Avatar seo said 2003 days later:

    Here is a novice to learn seo tips seo record from scratch, from the initial construction to the site seo, sem, step by step of the way, the majority of seoer common and common learning progress, this is my seo blog.

  10. Avatar seo said 2003 days later:

    Here is a novice to learn seo tips seo record from scratch, from the initial construction to the site seo, sem, step by step of the way, the majority of seoer common and common learning progress, this is my seo blog.

  11. Avatar Photographers Northern Ireland said 2060 days later:

    Wow, excellent data regarding this issue. You might want to let me know how long will it take? Because I like to write the thesis or may be it would be nice to detect the thesis writing service. Thank you very much.

  12. Avatar bijoux homme said 2070 days later:

    Prague Castle is the world’s largest ancient castle and is actually a complex comprised of sixty-plus buildings.

  13. Avatar Emergency Sandbags said 2113 days later:

    Hey Paolo Dona, I am Master in Computer Application.I am learning many computer languages,specially Ajax Jquery,Jsp. Here in this blog I knew different thing about java.

    - Emergency Sandbags Selling

(leave url/email »)

   Comment Markup Help Preview comment