如何将JAR库添加到WAR项目而不面临java.lang.ClassNotFoundException?类路径 vs 构建路径 vs /WEB-INF/lib

发布于 2024-11-25 12:45:17 字数 385 浏览 2 评论 0原文

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

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

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2024-12-02 12:45:17

CLASSPATH 环境变量仅由 java.exe 命令使用,即使如此,仅当在没有任何 -cp 的情况下调用该命令时, -classpath-jar 参数。 Eclipse、Netbeans 和 IDEA 等 IDE 会忽略 CLASSPATH 环境变量。另请参阅 java.lang.ClassNotFoundException 尽管使用 CLASSPATH环境变量

构建路径仅适用于编译项目代码所需的库。手动将 JAR 放入 /WEB-INF/lib 中,或设置部署程序集,或者让 Maven 等外部构建系统放置 作为构建期间生成的 WAR 的 /WEB-INF/lib 中的 JAR,仅适用于在目标环境上部署和运行代码所需的库。请注意,您不应该在 /WEB-INF/lib 中创建子文件夹。 JAR 必须放置在根目录中。

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

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

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

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.

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 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?

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.

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?

多情出卖 2024-12-02 12:45:17

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

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

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

罪歌 2024-12-02 12:45:17

如果您使用的是Maven

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

,然后选择Java Build Path Entries并选择< Maven 依赖项

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

梦醒时光 2024-12-02 12:45:17

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

因此,如果有人仍然遇到困难,请检查权限。有些人可能会发现这是显而易见的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文