title: How to tell tomcat to use an external lib while running from within eclipse
body: <p>How to add a custom made jar inside tomcat so that the servlets can use its functionality.</p>
  
  <p>To clarify: i created a separate java project called kos.lib.jar, exported as a jar, having a class TimeUtil, with public method now() returning a String. Works correctly as a standalone. Now i want to use its power from a servlet. So:</p>
  
  <p>eclipse indigo > New > Project > Dynamic Web Project > Apache tomcat v6.0, dynamic web version 2.5, default configuration for apache tomcat v6.0;
  copied Filecounter, from <a href="http://www.vogella.com/articles/EclipseWTP/article.html" rel="nofollow">http://www.vogella.com/articles/EclipseWTP/article.html</a> (from section 5 onward), and then rmb > servlet1.java > run> on server> tomcat shows the servlet correctly.</p>
  
  <p>Now I add in servlet1.java, under public void init() the simple line <code>TimeUtil.now();</code>, so just an expression. Eclipse autocompletes when I use TimeUtil.n, as far as class paths are concerned eclipse is happy.</p>
  
  <p>Running the servlet (run as > run on server) however gives a tomcat 500: </p>
  
  <pre><code>java.lang.NoClassDefFoundError: kos/lib/time/TimeUtil
      kos.servlets.servlet1.init(servlet1.java:28)
  </code></pre>
  
  <p>Yay, indeed the expression was in source code at line 28. But how to fix it?
  Where to add my lib so that tomcat-run-from-eclipse accepts <code>TimeUtil.now()</code>? I tried a lot, adding it to debug configuration classpath source, inside WEB-INF, inside WebContent/WEB-INF, always the same 500. Then I commented the expression, tomcat runs the page fine again. Then added <code>TimeUtil.now();</code> to the FileDao getcount() method, same 500.</p>
  
  <p>There were related questions but those were about separated eclipse and tomcat (via user agent port), and i just want to use it from eclipse. With eclipse and tomcat separated, i just would put the jar next to tomcats libs like 'servlet-api.jar', but that has no effect in this situation.</p>
  
  <p>I get this </p>
  
  <pre><code>HTTP Status 500 - 
  
  --------------------------------------------------------------------------------
  
  type Exception report
  
  message 
  
  description The server encountered an internal error () that prevented it from fulfilling this request.
  
  exception 
  
      javax.servlet.ServletException: Servlet.init() for servlet servlet1 threw exception
          org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
          org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
          org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
          org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
          org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
          java.lang.Thread.run(Unknown Source)
  
  
      root cause 
  
      java.lang.NoClassDefFoundError: kos/lib/time/TimeUtil
          kos.servlets.servlet1.init(servlet1.java:28)
          javax.servlet.GenericServlet.init(GenericServlet.java:212)
          org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
          org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
          org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
          org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
          org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
          java.lang.Thread.run(Unknown Source)
  
  
      root cause 
  
      java.lang.ClassNotFoundException: kos.lib.time.TimeUtil
          org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
          org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
          kos.servlets.servlet1.init(servlet1.java:28)
          javax.servlet.GenericServlet.init(GenericServlet.java:212)
          org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
          org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
          org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
          org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
          org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
          org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
          java.lang.Thread.run(Unknown Source)
  
  
      note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.
  
  
      --------------------------------------------------------------------------------
  
      Apache Tomcat/6.0.26
  </code></pre>