2

How to add a custom made jar inside tomcat so that the servlets can use its functionality.

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:

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 http://www.vogella.com/articles/EclipseWTP/article.html (from section 5 onward), and then rmb > servlet1.java > run> on server> tomcat shows the servlet correctly.

Now I add in servlet1.java, under public void init() the simple line TimeUtil.now();, so just an expression. Eclipse autocompletes when I use TimeUtil.n, as far as class paths are concerned eclipse is happy.

Running the servlet (run as > run on server) however gives a tomcat 500:

java.lang.NoClassDefFoundError: kos/lib/time/TimeUtil
    kos.servlets.servlet1.init(servlet1.java:28)

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 TimeUtil.now()? 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 TimeUtil.now(); to the FileDao getcount() method, same 500.

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.

I get this

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
Kos Petoussis
  • 41
  • 1
  • 5

3 Answers3

1

In Java web applications, the third party libraries must go in WEB-INF/lib directory. If there is no such directory, just create it and Tomcat (or another Java Application Server) will handle it automatically to use the jars inside it as part of the build path of the application.

In short, create a folder lib inside WEB-INF and drop your external jars there.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • i did so, see below. SEVERE: Allocate exception for servlet servlet1 java.lang.ClassNotFoundException: kos.lib.time.TimeUtil at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) What more is needed? – Kos Petoussis Jun 08 '13 at 15:22
  • @KosPetoussis clean your application, rebuild it and redeploy it. It should work. If not, please edit your question and post the stacktrace of the error. – Luiggi Mendoza Jun 08 '13 at 15:24
  • i cleaned. Stacktrace now in question. – Kos Petoussis Jun 08 '13 at 15:28
  • @KosPetoussis the error is pretty descriptive. You put the jar but looks like your `TimeUtil` class is not in the package `kos.lib.time`. Make sure your class is there or to import it from the right package in your code. By the way, it would be good if you remove your web application and the temp files from Tomcat, restart Tomcat and then redeploy your app. – Luiggi Mendoza Jun 08 '13 at 16:45
  • Luiggi if timeutil was not in the package eclipse would give compiler errors let alone autocomplete. This is a RUNTIME error. I start tomcat from eclipse, so where are its temp files? – Kos Petoussis Jun 08 '13 at 17:53
  • @KosPetoussis please review [this](http://stackoverflow.com/q/5934922/1065197) and [this one too](http://stackoverflow.com/a/14972550/1065197). Also, make sure you're deploying your application in Tomcat's deploy folder and not in the folder provided by Eclipse. To change this, refer to [this answer](http://stackoverflow.com/a/14984586/1065197) – Luiggi Mendoza Jun 08 '13 at 18:29
0

Put your jar in

WebContent/WEB-INF/lib

folder instead of

WebContent/WEB-INF

As the tomcat documentation says:

/WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers.

Read this for more : http://tomcat.apache.org/tomcat-4.1-doc/appdev/deployment.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 2
    Just to know, this doesn't affect Tomcat only, it is a common rule for all application servers. – Luiggi Mendoza Jun 08 '13 at 15:06
  • @LuiggiMendoza Yes but the question was realted to the tomcat so i have talked about it and put the reference accordingly. Thanks for your suggestion. – Juned Ahsan Jun 08 '13 at 15:09
  • 1
    Also, please note that Tomcat is in version 7 (version 4 is almost 10 years old). Update your resources as well. – Luiggi Mendoza Jun 08 '13 at 15:10
  • i created a folder lib under WebContent/WEB-INF, so that lib is next to folder classes and file web.xml, then i copied kos.lib.jar to C:\dev\Java\eclipse\workspace\web examples tomcat 6\WebContent\WEB-INF\lib, same error. Did i misunderstand your suggestion? Please help. Is some setting needed in the runtime config in eclipse now? – Kos Petoussis Jun 08 '13 at 15:19
  • You need to put the jar file in tomcat/webapp/yourapplication/WEB-INF/lib folder. – Juned Ahsan Jun 08 '13 at 15:34
  • @JunedAhsan how do i do that since tomcat is started from eclipse. If it were that obvious i would not ask. There is a runtime config i seem to be missing, but which... – Kos Petoussis Jun 08 '13 at 17:54
0

I solved it. Turns out that tomcat-under-eclipse points to a specific tomcat, in my case C:\dev\webserver\tomcat\6.0.32\lib and the only thing needed was to put the custom lib THERE. (if i stop the server, rename the jar, and restart from servlet i get the noclassdeffound runtime) The exact path of the web server can be checked in runtime configurations > classpath. For more fun set the source code of the custom lib in 'source' (bonus: now you can set breakpoints even when the code arrives at your custom source!)

It had absolutely nothing to do with web-inf answers.

Thanks for trying guys, anyway.

Kos Petoussis
  • 41
  • 1
  • 5