简体   繁体   English

未找到Apache Commons的类-添加jar之后

[英]Class not found of apache commons - after jar is added

I have created a project which uses apache commons validations.I have tested it as a project and every thing worked as expected. 我创建了一个使用apache commons验证的项目。我已经将其作为一个项目进行了测试,并且一切正常。

Then, I have created a jar file of this project. 然后,我创建了该项目的jar文件。 I have checked that the apache commons validations jar is also compiled and added (as an dependency) to the jar I am building. 我已经检查过是否已编译apache commons验证jar,并将其添加(作为依赖项)到我正在构建的jar中。

Then, I have included this jar (with maven) to other project, and BANG an error!!! 然后,我已经将此罐子(与Maven一起)包含到其他项目中,并且发出错误提示!!! here it is: 这里是:

java.lang.NoClassDefFoundError: org/apache/commons/validator/routines/EmailValidator 

What did I do wrong? 我做错了什么?

Here is the stacktrace: 这是堆栈跟踪:

Jan 21, 2014 3:20:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.evappserver.servlets.RegistrationServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: org/apache/commons/validator/routines/EmailValidator
    at com.evappcom.validator.Validator.emailValidator(Validator.java:29)
    at com.evapp.protocols.BaseProtocol.emailValidation(BaseProtocol.java:124)
    at com.evapp.protocols.RegistrationProtocol.validateRequest(RegistrationProtocol.java:57)
    at com.evappserver.logiclayer.RegistrationLogic.registrateUser(RegistrationLogic.java:27)
    at com.evappserver.servlets.RegistrationServlet.doGet(RegistrationServlet.java:38)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Here is the pom file: 这是pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>EvApp_Server</groupId>
    <artifactId>EvApp_Server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
        <dependency>
            <groupId>oro</groupId>
            <artifactId>oro</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.0-alpha4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.evapp.code</groupId>
            <artifactId>evappcom</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

Here is the maven tree screenshot: 这是Maven树的屏幕截图: 在此处输入图片说明

Here is the included maven dependencies (with my custom jar): 这是包含的Maven依赖项(使用我的自定义jar): 在此处输入图片说明

在我看来,“其他”项目已经具有该apache依赖关系,并且该特定版本没有EmailValidator类。

Here is what I did to solve the problem. 这是我为解决问题所做的工作。 I don't know if it is the right way (I will stick to it until I find a better one). 我不知道这是否是正确的方法(我会坚持下去,直到找到更好的方法为止)。

I have changed the project from which I created the jar to maven, then I exported it to jar without the dependency of the apache common validations. 我将创建jar的项目更改为maven,然后将其导出到jar,而没有apache通用验证的依赖。 After installing it to my local repository and adding it to my main project I have added the dependency of the jar and apache common validations. 将其安装到本地存储库并将其添加到我的主项目后,我添加了jar和apache常见验证的依赖项。

From here every thing worked. 从这里开始,所有事情都起作用了。

I think it is not a good solution because it breaks the encapsulation idea of OOP. 我认为这不是一个好的解决方案,因为它破坏了OOP的封装思想。 It is possible to do the export of the jar from maven with all the dependency but as I know it may cause conflict errors of the dependencies when it is added to other project with the same dependencies. 可以使用所有依赖项从maven导出jar,但是据我所知,当将其添加到具有相同依赖项的其他项目中时,可能会导致依赖项的冲突错误。

I will glad to hear a better solutions :) 我很高兴听到更好的解决方案:)

您需要下载Apache Commons / Lang并确保它包含在CLASSPATH中(或者,如果使用IDE,请确保所有必需的库都包含在构建配置中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM