如何将JAR库添加到WAR项目中,而不会面临java.lang.ClassNotFoundException? Classpath vs Build Path 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

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

问题描述

如何将JAR库添加到Eclipse中的WAR项目中,而不必面向 java.lang.ClassNotFoundException java.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项目的构建路径属性以使代码编译。我们有时需要将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添加到 em>并且当这些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 参数。它被IDE,Netbeans和IDEA等IDE忽略。另请参见 java.lang.ClassNotFoundException尽管使用CLASSPATH环境变量

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.

构建路径仅适用于获取项目代码进行编译所需的库。手动将JAR放在 / WEB-INF / lib 中,或者设置部署程序集,或者让像Maven这样的外部构建系统放置< dependency> 作为生成的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.

目标Java EE服务器或servlet容器(如JSP,Servlet,EL等)已经提供了一些库。您不需要将这些库的JAR放在 / WEB-INF / lib 中。而且这只会引起类加载的麻烦。只需(间接)在构建路径中指定它就足够了。在Eclipse中,通常通过相应地设置 Targeted Runtime 来实现。它将自动结束于构建路径。另请参见如何导入javax我的Eclipse项目中的.servlet API

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?

其他库,通常是第三方,如Apache Commons,JDBC驱动程序和目标servletcontainer未提供的标准库(例如Tomcat不支持JSF,CDI,JPA,EJB等),需要最终在 / WEB-INF / lib 中。您不一定需要在构建路径中指定它。只有当您已经拥有用户库时,您才可以使用部署程序集设置。另请参见在Eclipse构建路径中使用用户库时的ClassNotFoundException

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.

如果您使用Maven,那么您需要确保将库标记为< scope>提供的< /范围> 如果这些已经由目标运行时提供,例如Java EE,Servlet,EL等,以防您部署到WildFly,TomEE等。这样他们不会最终在 / WEB-INF / lib 产生的WAR(并可能导致与服务器捆绑库的冲突),但它们将最终在Eclipse的 并获得项目的代码进行编译)。

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

这篇关于如何将JAR库添加到WAR项目中,而不会面临java.lang.ClassNotFoundException? Classpath vs Build Path vs / WEB-INF / lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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