• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know which class is loaded from which jar file alongwith location?  RSS feed

 
Danish Sualeh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my code I am using function escapeHtml() belonging to class org.apache.commong.lang.StringEScapeUtils.
I want the JVM to output in verbose mode , that StringUtils class is being loaded from org-apache-commons.jar alongwith the path of the jar file.

Further , I tried out the java -verbose option, but required info is printed
only for the classes belonging to the J2SE package.
When it comes it to external classes supplied by jar files, no such info is printed .

Can anyone help ?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use ClassLoaders to find files, as well as loading Classes. The trick here is that if you turn a fully-qualified Class into it's file representation, you can use the ClassLoader to get the URL location for this 'file', which includes the name of the JAR containing the Class it loaded. I won't give you all the code, it isn't that hard and I don't want to spoil the fun:

1) Turn the class name into a file name eg java.lang.String into java/lang/String/class
2) GET the file as a RESOURCE afetr you GET the ClassLoader
3) Print the URL.
4) Parse the URL for the actual data you want.

Hope this helps.

Dave
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, spot the deliberate error
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Received via PM:
(Please keep conversations on the site, we don't always check emails and most people will ignore code requests via PM, we just don't have time. Also, if we keep it here others can help and it will help other people too)

But there are two issues:

*Can the solution be implemented without changing the original code?
*Can you provide some hints how to implement it ?


I won't have chance to look at this for a while. Maybe someone else can help.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
Oops, spot the deliberate error


You didn't state your assumptions.
Namely, that the class was indeed loaded from a .class file and that the system class loader was used to do so.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I was referring to String/class, but that will do too

You can 'enable' this style of information by adding -verbose:class to the command line. It will produce output like this:



and eventually you will get to your classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!