Application Server Solutions for Microsoft IIS and ASP.NET
       solutions   products   partners   company   support   downloads         store
ServletExec Self-Help: FAQ
Back to Search >  Back to Search Results

Faq ID 395
Product ServletExec
Category Class Loading/Reloading
Question How can my code find out where classpath items reside?
Answer

.class files are looked for and loaded from various locations
as determined by the classloader. Here is an example of
ways you can see these locations for a particular item or package:


<%@ page import="java.net.URL, java.util.Enumeration" %>

<html>
<head><title>showResourceLocation</title></head>

<p>Finding the first location for a given class or resource (to see which one is being found and used by the classloader):</p>
<%
 String itemToFind = "java/lang/String.class"; // setting itemToFind="" will tell you the first place the classloader looks for things.
 out.print(this.getClass().getClassLoader().getResource(itemToFind).getPath());
 //example output: file:/C:/Program%20Files%20(x86)/Java/jdk1.6.0_16/jre/lib/rt.jar!/java/lang/String.class 
%>

<hr>
<p>Finding all locations for a given class or resource:</p>

<%
  itemToFind = "META-INF/vfs-providers.xml";
  Enumeration<URL> locations = this.getClass().getClassLoader().getResources(itemToFind);
  out.println("<ol>");
  while (locations.hasMoreElements())
    out.println("<li>" +((URL)locations.nextElement()).getPath()+ "</li>");

  out.println("</ol>");
%>

<p>
Note: These will be output in a different order than how the
classloader would actually find and load them.
See the javadocs for ClassLoader.getResources() which say
that the parent loader is searched first.
</p>

</html>


This technique can also be used to locate the JAR file that contains a particular package. For example:
itemToFind = "javax/servlet";



   
company media information terms of use privacy policy contact us