I'm developing a dynamic web application with Java (Servlets/JSP) in Eclipse. I'm trying to use an external JAR (I'm using StringUtils
from Apache Commons) and I'm confused as to where I should put the jar (/lib
, /WEB-INF/lib
?) and how do I need to configure my class path (in Eclipse).
I tried putting the JARs in both of the aforementioned places, and loading them to the classpath by clicking Add JAR
in the project properties and both solution compile fine, but give a runtime error like so:
SEVERE: Servlet.service() for servlet UserList threw exception java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils at cs236369.hw5.db.MySqlDbHandler.insert(MySqlDbHandler.java:58) at cs236369.hw5.servlets.UserList.doGet(UserList.java:50) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)
So... how do I load external JARs?
Please take note that I am not that familiar with how external JARs are loaded in Java, the VM, or how Eclipse manages it all, so I would appreciate detailed solutions.
The best way is to add the jar to your project is as follows: Create a folder called lib in your project folder. copy all the jar files you need to this folder . Refresh your project in eclipse.
You can just copy the jar to the WEB-INF/lib directory of the project. The jar is automatically added to the project classpath if its a web project. if its not a web project you can right click on the library and go to “Build Path” -> “Add to Build Path”.
To include external JAR files, you can either: Copy all the JAR files of the external packages to the Java's Extension Directories (NOT applicable to JDK 9). For Windows, the JDK extension directory is located at " <JAVA_HOME>\jre\lib\ext " (e.g., " c:\Program Files\Java\jdk1. 8.0_xx\jre\lib\ext ").
Create an executable JAR file:Specify the manifest: Select the option Generate the manifest file. Seal content of the JAR file: select the option Seal JAR. Select the main class that acts as an entry point to the application: Click Browse button next to the text field Main Class.
They must be placed in yourapp/WEB-INF/lib
. (In eclipse you are adding them properly). So make sure the jar is really there. Note that if you are starting the app from within eclipse, you would have to configure deployment assembling.
The deployment assembly is configured through right click > properties > deployment assembly
in eclipse. There you should "Add" your "Java build path entries"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With