Yep. There is a Hierarchy of Classloaders (you can play with this via: Classloader cl = getClass().getClassloader(); while(cl!=null) { // print out CL info cl = cl.getParent() }
In "bigger" applications like WLS, it gets quite complicated and it is useful to be able to find out "where" in the class loader hierarchy yuor class is loaded from. But basically in tomcat you have 2 (I think), Tomcats one and the one for each webapp. BTW, you will often have one for the Servlets and one for the JSPs (helps with reloading) not sure about tomcat and this... Alan. -----Original Message----- From: Roland Berger [mailto:[EMAIL PROTECTED]] Sent: 19 November 2001 15:40 To: Log4J Users List Subject: AW: log4j configuration via XML SOLVED Does that mean that tomcat has it's own class loader and also each webapp which is deployed in the tomcat webapp directory? Cheers Roland -----Ursprüngliche Nachricht----- Von: Alan Pearlman Spencer [mailto:[EMAIL PROTECTED]] Gesendet: Montag, 19. November 2001 16:27 An: Log4J Users List Betreff: RE: log4j configuration via XML SOLVED I think this explains it: Your Servlet loads the xml file and in it's own classloader: fine. The parser finds the reference to the dtd file and it can't find it as it is in the parent class loader (tomcats not the webapps) If you were to copy the parser jar files to your lib it should also fix the problem... Alan. -----Original Message----- From: Roland Berger [mailto:[EMAIL PROTECTED]] Sent: 19 November 2001 15:19 To: Log4J Users List Subject: AW: log4j configuration via XML SOLVED Hi Alan It looks like I didn't understand tomcat behaviour (and still don't). I had to add to the CLASSPATH the path to the log4j.jar (C:/Programme/jakarta-tomcat-3.2.3/webapps/pcd/WEB-INF/lib/log4j.jar). And only then the ClassLoader has found the log4j.dtd. I always thought when starting up tomcat it will generated a CLASSPATH for every web app!!??? Does that mean that I have to edit the CLASSPATH for my web app in the tomcat startup script's? That doesen't sound JAKARTA projects like. I will post this question also to tomcat mailing list. Thank you for your help Cheers Roland -----Ursprüngliche Nachricht----- Von: Alan Pearlman Spencer [mailto:[EMAIL PROTECTED]] Gesendet: Montag, 19. November 2001 13:55 An: Log4J Users List Betreff: RE: log4j configuration via XML Make sure you use: <init-param> <param-name>log4j-init-file</param-name> <param-value>/ch/plenum/pcd/log4jConfiguration.xml</param-value> </init-param> Alan. -----Original Message----- From: Roland Berger [mailto:[EMAIL PROTECTED]] Sent: 19 November 2001 12:40 To: Log4J Users List Subject: AW: log4j configuration via XML It called: /WEB-INF/classes/ch/plenum/pcd/log4jConfiguration.xml Roland -----Ursprüngliche Nachricht----- Von: Alan Pearlman Spencer [mailto:[EMAIL PROTECTED]] Gesendet: Montag, 19. November 2001 13:38 An: Log4J Users List Betreff: RE: log4j configuration via XML You should only have to put them both in the classes directory: /WEB-INF/classes/ch/plenum/pcd/log4jConfiguration The NullPointer says that the "getClass().getResource(file)" is returning null (maybe you should check this in your code... You "could" put them in a jar in the lib directory, but this should not really help... Is the file called: /WEB-INF/classes/ch/plenum/pcd/log4jConfiguration or /WEB-INF/classes/ch/plenum/pcd/log4jConfiguration.xml Alan. -----Original Message----- From: Roland Berger [mailto:[EMAIL PROTECTED]] Sent: 19 November 2001 12:15 To: Log4J Users List Subject: AW: log4j configuration via XML Hi It seems that the methode *DOMConfigurator.configure(getClass().getResource(file))* can not find the given resource */ch/plenum/pcd/log4jConfiguration*. Tomcat tells me : cannot load servlet name When I put a try catch block around the servlet init methode I find a NullPointerException. Do I have to put the xml config file and its dtd in a jar and place it under WEB-INF/lib/ to allow the classLoader to find the recource? Thanks Roland -----Ursprüngliche Nachricht----- Von: Alan Pearlman Spencer [mailto:[EMAIL PROTECTED]] Gesendet: Montag, 19. November 2001 12:24 An: Log4J Users List Betreff: RE: log4j configuration via XML Your resource name (sorry I left this out) needs to "look" like a path: <init-param> <param-name>log4j-init-file</param-name> <param-value>/ch/plenum/pcd/log4jConfiguration</param-value> </init-param> Try this and let me know... Alan. -----Original Message----- From: Roland Berger [mailto:[EMAIL PROTECTED]] Sent: 19 November 2001 11:04 To: Log4J Users List Subject: AW: log4j configuration via XML Hi Alan I tried it the following way which only works when I specifie the absolut path for the dtd file in the log4j config xml file. But I don't want to give absolut names: Here is how I tried it: Log4j Init Servlet: ------------------- void init() { String prefix = getServletContext().getRealPath("/"); String file = getInitParameter("log4j-init-file"); // if the log4j-init-file is not set, then no point in trying if(file != null) { //DOMConfigurator.configure(prefix+file); DOMConfigurator.configure(getClass().getResource(file)); } else { System.out.println("File name for parameter \"log4j-init-file\" not set in web.xml for servlet PCDInitServlet"); } } where *file* is taken from the web.xml file which looks like that: web.xml file: ------------- <servlet> <servlet-name>PCDInitServlet</servlet-name> <servlet-class>ch.plenum.pcd.PCDInitServlet</servlet-class> <init-param> <param-name>log4j-init-file</param-name> <param-value>ch.plenum.pcd.log4jConfiguration</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> in the log4jconfiguration.xml file I have the following: log4j xml config file: ---------------------- <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> ..snippet... The log4jConfiguration.xml file and its dtd I have placed in the package ch.plenum.pcd. Same as the Servlet class. The DOMConfigurator.configure(getClass().getResource(file)) does not work at all when I give *ch.plenum.pcd.log4jConfiguration* (thats what I understand as resource)(initialisation of the servlet fails). I have to use *DOMConfigurator.configure(prefix+file)* where file is *WEB-INF/classes/ch/plenum/pcd/log4jConfiguration.xml*. Then the config file is found but not the dtd. After the class Loader the Parser tries to find the dtd file but fails also: log4j:ERROR Could not find [log4j.dtd]. Used [sun.misc.Launcher$AppClassLoader@404536] class loader in the search. log4j:ERROR Could not parse input stream [java.io.FileInputStream@7a5cc]. org.xml.sax.SAXParseException: Relative URI "log4j.dtd"; can not be resolved without a document URI. unless I give the absolut URI: *<!DOCTYPE log4j:configuration SYSTEM "file:///C:/Programme/jakarta-tomcat-3.2.3/webapps/pcd/WEB-INF/classes/c h/pl enum/pcd/log4j.dtd"* which I don't like. I hope you or somebody else can help. Thank you for your time you spend on this. Cheers Roland -----Ursprüngliche Nachricht----- Von: Alan Pearlman Spencer [mailto:[EMAIL PROTECTED]] Gesendet: Montag, 19. November 2001 10:45 An: Roland Berger; Log4J Users List Betreff: RE: log4j configuration via XML Hi, What I meant was don't use a file name, use a resource name. This means that the ClassLoader will be used to find the file and that it will be looked for in exactly the same way as the classes. So, if you are using a .war file you put it in the classes directory and reference it as if it were a class. This is what I meant by the etc thing. In your servlet you can have: DOMConfigurator.configure(getClass().getResource(resourceName)); And if you put the config file and the dtd in the same package (directory) it should all fit together. Let me know how it goes... Alan. -----Original Message----- From: Roland Berger [mailto:[EMAIL PROTECTED]] Sent: 19 November 2001 08:21 To: Alan Pearlman Spencer Subject: AW: log4j configuration via XML Hi Alan What do you mean by ".... etc also..."? How do you reference the file name of the jared config xml file in web.xml? e.g. servlet> <servlet-name>PCDInitServlet</servlet-name> <servlet-class>ch.plenum.pcd.PCDInitServlet</servlet-class> <init-param> <param-name>log4j-init-file</param-name> <param-value>WEB-INF\classes\log4jConfiguration.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> Thank you Roland -----Ursprüngliche Nachricht----- Von: Alan Pearlman Spencer [mailto:[EMAIL PROTECTED]] Gesendet: Freitag, 16. November 2001 18:29 An: Log4J Users List Betreff: RE: log4j configuration via XML Hi, I put the dtd in the same directory as the xml file and it works file (as long as it is jared up etc also...) Alan. -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>