如何在不面对 java.lang.ClassNotFoundException 的情况下将 JAR 库添加到 WAR 项目?类路径 vs 构建路径 vs/WEB-INF/lib [英] How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib

查看:26
本文介绍了如何在不面对 java.lang.ClassNotFoundException 的情况下将 JAR 库添加到 WAR 项目?类路径 vs 构建路径 vs/WEB-INF/lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

推荐答案

CLASSPATH 环境变量仅由 java.exe 命令使用,并且仅当命令在没有任何 -cp-classpath-jar 参数的情况下被调用.CLASSPATH 环境变量被 Eclipse、Netbeans 和 IDEA 等 IDE 忽略.另见 java.lang.ClassNotFoundException 尽管使用了 CLASSPATH环境变量.

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

构建路径仅用于获取项目代码进行编译所需的库.手动将 JAR 放置在 /WEB-INF/lib 中,或设置 Deployment Assembly,或让外部构建系统(如 Maven)放置 code> 作为构建期间生成的 WAR 的 /WEB-INF/lib 中的 JAR,仅适用于获取代码以在目标环境上部署和运行所需的库.请注意,您不应在 /WEB-INF/lib 中创建子文件夹.JAR 必须放在根目录中.

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.

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

Some libraries are already provided by the target JEE 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. You do not need to manually add them to Build Path. See also How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?

其他库,通常是第三方库,如 Apache Commons、JDBC 驱动程序和 JEE 库,这些库不是由目标 servletcontainer 提供的(例如,Tomcat 不支持许多开箱即用的 JEE 库,例如 JSF、JSTL、CDI、JPA,EJB 等),需要以 /WEB-INF/lib 结尾.您只需将物理 JAR 文件复制并粘贴到其中即可.您不一定需要在 Build Path 中指定它.或许只有当您已经将它作为用户库使用时,但您应该使用部署程序集设置来代替.另请参阅 在 Eclipse 构建路径中使用用户库时的 ClassNotFoundException.

Other libraries, usually 3rd party ones like Apache Commons, JDBC drivers and JEE libraries which are not provided by the target servletcontainer (e.g. Tomcat doesn't support many JEE libraries out the box such as JSF, JSTL, CDI, JPA, EJB, etc), need to end up in /WEB-INF/lib. You can just copy and paste the physical JAR files in there. 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 instead. See also ClassNotFoundException when using User Libraries in Eclipse build path.

如果您使用的是 Maven,那么您需要确保将库标记为 provided</scope> 如果目标运行时已经提供了这些库,例如如 JEE、Servlet、EL 等,以防您部署到 WildFly、TomEE 等.这样它们就不会出现在生成的 WAR 的 /WEB-INF/lib 中(并可能导致与服务器捆绑的库),但它们最终会出现在 Eclipse 的构建路径中(并编译项目的代码).另请参阅 如何通过 Maven 正确安装和配置 JSF 库?

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 JEE, 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). See also How to properly install and configure JSF libraries via Maven?

这篇关于如何在不面对 java.lang.ClassNotFoundException 的情况下将 JAR 库添加到 WAR 项目?类路径 vs 构建路径 vs/WEB-INF/lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆