简体   繁体   中英

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.

Then, I have created a jar file of this project. I have checked that the apache commons validations jar is also compiled and added (as an dependency) to the jar I am building.

Then, I have included this jar (with maven) to other project, and BANG an error!!! 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:

<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: 在此处输入图片说明

Here is the included maven dependencies (with my custom 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. 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.

From here every thing worked.

I think it is not a good solution because it breaks the encapsulation idea of 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.

I will glad to hear a better solutions :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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