How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib

前端 未结 6 1587
一个人的身影
一个人的身影 2020-11-22 01:23

How should I add JAR libraries to a WAR project in Eclipse without facing java.lang.ClassNotFoundException or java.lang.NoClassDefFoundError?

相关标签:
6条回答
  • 2020-11-22 02:04

    This article explains the same problem in Spring Boot application. Also useful information how to download Oracle jar file and include dependency, setup datasource etc:

    Configuring Spring Boot with Oracle

    0 讨论(0)
  • 2020-11-22 02:17

    I want to give the answer for the folowing link question ClassNotFoundException oracle.jdbc.driver.OracleDriver only in servlet, using Eclipse

    Ans: In Myeclipse go to Server-->left click on Myeclipse Tomcat7-->Configure Server Connector-->(Expand)Myeclipse Tomcat7--> Paths-->Prepend to classpath-->Add jar (add oracle14 jar)-->ok

    0 讨论(0)
  • 2020-11-22 02:18

    If you are using Maven:

    Open the project properties, and under Deployment Assembly click Add...

    Then select Java Build Path Entries and select Maven Dependencies

    0 讨论(0)
  • 2020-11-22 02:19

    The CLASSPATH environment variable is only used by the java.exe command and even then only when used without any of the -cp, -classpath, -jar arguments. It is ignored by IDEs like Eclipse, Netbeans and IDEA. See also java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable.

    The Build Path is only for libraries which are required to get the project's code to compile. Manually placing JAR in /WEB-INF/lib, or setting the Deployment Assembly, or letting an external build system like Maven place the <dependency> as JAR in /WEB-INF/lib of produced WAR during the build, is only for libraries which are required to get the code to deploy and run on the target environment too. Do note that you're not supposed to create subfolders in /WEB-INF/lib. The JARs have to be placed in the root.

    Some libraries are already provided by the target Java EE server or servletcontainer, such as JSP, Servlet, EL, etc. So you do not need put JARs of those libraries in /WEB-INF/lib. Moreover, it would only cause classloading trouble. It's sufficient to (indirectly) specify them in Build Path only. In Eclipse, you normally do that by setting the Targeted Runtime accordingly. It will automatically end up in Build Path. See also How do I import the javax.servlet API in my Eclipse project?

    Other libraries, usually 3rd party ones like Apache Commons, JDBC drivers and standard libraries not provided by the target servletcontainer (e.g. Tomcat doesn't support JSF, CDI, JPA, EJB, etc out the box), need to end up in /WEB-INF/lib. You do not necessarily need to specify it in Build Path. Only perhaps when you already have it as User Library, but you should then use Deployment assembly setting for this. See also ClassNotFoundException when using User Libraries in Eclipse build path.

    In case you're using Maven, then you need to make absolutely sure that you mark libraries as <scope>provided</scope> if those are already provided by the target runtime, such as Java EE, Servlet, EL, etc in case you deploy to WildFly, TomEE, etc. This way they won't end up in /WEB-INF/lib of produced WAR (and potentially cause conflicts with server-bundled libraries), but they will end up in Eclipse's Build Path (and get the project's code to compile).

    0 讨论(0)
  • 2020-11-22 02:19

    Those JARs in the build path are referenced for the build (compile) process only. If you export your Web Application they are not included in the final WAR (give it a try).

    If you need the JARs at runtime you must place them in WEB-INF/lib or the server classpath. Placing your JARs in the server classpath does only make sense if several WARs share a common code base and have the need to access shared objects (e.g. a Singleton).

    0 讨论(0)
  • 2020-11-22 02:27

    Resolved by setting permissions. Had related issue using PySpark and Oracle jdbc. The error does not state that the file cannot be accessed, just that the class cannot be loaded.

    So if anyone still struggles, check the permissions. Some might find it obvious tho'.

    0 讨论(0)
提交回复
热议问题