eclipse 如何在不面对 java.lang.ClassNotFoundException 的情况下将 JAR 库添加到 WAR 项目?类路径 vs 构建路径 vs /WEB-INF/lib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6792197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:41:16  来源:igfitidea点击:

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

eclipsejakarta-eejarclasspathwar

提问by BalusC

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

我应该如何将 JAR 库添加到 Eclipse 中的 WAR 项目而不面对java.lang.ClassNotFoundExceptionjava.lang.NoClassDefFoundError

The CLASSPATHenvironment variable does not seem to work. In some cases we add JAR files to the Build Pathproperty of Eclipse project to make the code compile. We sometimes need to put JAR files inside /WEB-INF/libfolder of the Java EE web application to make the code to run on classes inside that JAR.

CLASSPATH环境变量似乎并没有工作。在某些情况下,我们将 JAR 文件添加到Eclipse 项目的Build Path属性中以使代码编译。我们有时需要将 JAR 文件放在/WEB-INF/libJava EE Web 应用程序的文件夹中,以使代码在该 JAR 中的类上运行。

I do not exactly understand why CLASSPATHdoes not work and in which cases we should add JARs to Build Pathand when exactly those JARs should be placed in /WEB-INF/lib.

我不完全理解为什么CLASSPATH不起作用,在哪些情况下我们应该将 JAR 添加到构建路径以及何时应该将这些 JAR 放入/WEB-INF/lib.

回答by BalusC

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

CLASSPATH环境变量仅用于由所述java.exe命令并在使用时连那么只有没有任何的-cp-classpath-jar参数。它被 Eclipse、Netbeans 和 IDEA 等 IDE 忽略。另请参阅java.lang.ClassNotFoundException 尽管使用了 CLASSPATH 环境变量

The Build Pathis 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/libof 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.

构建路径是仅适用于需要获得该项目的代码编译库。手动将 JAR 放入/WEB-INF/lib,或设置部署程序集,或让外部构建系统(如 Maven)在构建期间将生成的 WAR 中的<dependency>JAR 放入/WEB-INF/lib,仅适用于获取代码以在目标环境上部署和运行所需的库也。请注意,您不应该在/WEB-INF/lib. JAR 必须放在根目录中。

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 Pathonly. In Eclipse, you normally do that by setting the Targeted Runtimeaccordingly. It will automatically end up in Build Path. See also How do I import the javax.servlet API in my Eclipse project?

有些库已经由目标 Java EE 服务器或 servletcontainer 提供,例如 JSP、Servlet、EL 等,因此您不需要将这些库的 JAR 放在/WEB-INF/lib. 而且,它只会导致类加载的麻烦。仅在构建路径中(间接)指定它们就足够了。在 Eclipse 中,您通常通过相应地设置Targeted Runtime来做到这一点。它将自动结束于Build Path。另请参阅如何在我的 Eclipse 项目中导入 javax.servlet API?

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 assemblysetting for this. See also ClassNotFoundException when using User Libraries in Eclipse build path.

其他库,通常是第三方库,如 Apache Commons、JDBC 驱动程序和目标 servletcontainer 未提供的标准库(例如,Tomcat 不支持开箱即用的 JSF、CDI、JPA、EJB 等),需要以/WEB-INF/lib. 您不一定需要在Build Path 中指定它。仅当您已经将其作为User Library 使用时,但您应该为此使用部署程序集设置。另请参阅在 Eclipse 构建路径中使用用户库时的 ClassNotFoundException

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/libof 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).

如果您使用的是 Maven,那么您需要确保将库标记为<scope>provided</scope>目标运行时已经提供的库,例如 Java EE、Servlet、EL 等,以防您部署到 WildFly、TomEE 等. 这样它们就不会出现在/WEB-INF/lib生成的 WAR 中(并可能导致与服务器捆绑的库发生冲突),但它们最终会出现在 Eclipse 的构建路径中(并编译项目的代码)。

回答by home

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).

构建路径中的那些 JAR 仅在构建(编译)过程中被引用。如果您导出 Web 应用程序,它们不会包含在最终 WAR 中(试一试)。

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 andhave the need to access shared objects (e.g. a Singleton).

如果您在运行时需要 JAR,您必须将它们放在 WEB-INF/lib 或服务器类路径中。只有当多个 WAR 共享一个公共代码库并且需要访问共享对象(例如单例)时,将 JAR 放在服务器类路径中才有意义。

回答by Tal Haham

If you are using Maven:

如果您使用的是Maven

Open the project properties, and under Deployment Assemblyclick Add...

打开项目属性,然后在部署程序集下单击添加...

Then select Java Build Path Entriesand select Maven Dependencies

然后选择Java Build Path Entries并选择Maven Dependencies

回答by Alex Dembo

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.

通过设置权限解决。使用 PySpark 和 Oracle jdbc 时遇到相关问题。该错误并未说明无法访问该文件,只是说明无法加载该类。

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

因此,如果有人仍然在挣扎,请检查权限。有些人可能会觉得这很明显。

回答by Denys

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:

本文解释了 Spring Boot 应用程序中的相同问题。还有关于如何下载 Oracle jar 文件并包含依赖项、设置数据源等的有用信息:

Configuring Spring Boot with Oracle

使用 Oracle 配置 Spring Boot

回答by anikesh sharma

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

我想给出以下链接问题ClassNotFoundException oracle.jdbc.driver.OracleDriver的答案仅在 servlet 中,使用 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

Ans:在Myeclipse中进入Server-->左键点击Myeclipse Tomcat7-->Configure Server Connector-->(Expand)Myeclipse Tomcat7--> Paths-->Prepend to classpath-->Add jar (add oracle14 jar)- -> 好的