Chang Liu
Chang Liu

Reputation: 21

How to fix a java.lang.NoSuchMethodError?

I'm deploying a Java web application. But, the ansj package doesn't work normally when deployed using Tomcat. Strangely, it works well when running locally.

How can I fix the problem?

Java

public static boolean isSimilarity(String doc1, String doc2) {
    Set<String> result = new HashSet<>();
    String[] page1 = NlpAnalysis.parse(doc1).toString().split(",");
    String[] page2 = NlpAnalysis.parse(doc2).toString().split(",");
    Set<String> set1 = new HashSet<>();
    Set<String> set2 = new HashSet<>();
    set1.addAll(Arrays.asList(page1));
    set2.addAll(Arrays.asList(page2));
    double or = 0;
    result.addAll(set1);
    result.retainAll(set2);
    or = (double) result.size() / set1.size();
    return or > 0.95;
}


31-Mar-2019 17:44:16.075 严重 [http-nio-8082-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [testServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause
 java.lang.NoSuchMethodError: org.nlpcn.commons.lang.util.logging.LogFactory.getLog()Lorg/nlpcn/commons/lang/util/logging/Log;
    at org.ansj.library.DicLibrary.<clinit>(DicLibrary.java:23)
    at org.ansj.splitWord.Analysis.<init>(Analysis.java:64)
    at org.ansj.splitWord.analysis.NlpAnalysis.<init>(NlpAnalysis.java:258)
    at org.ansj.splitWord.analysis.NlpAnalysis.parse(NlpAnalysis.java:266)
    at services.esti.Service.isSimilarity(Service.java:32)
    at services.esti.Service.incrementNum(Service.java:59)
    at services.esti.Service.getTotalPageNum(Service.java:71)
    at services.esti.Walker.getPageNum(Walker.java:208)
    at services.esti.StartTest.threadTest(StartTest.java:49)
    at api.estimate.testServlet.doGet(testServlet.java:43)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at

Below is my related pom.xml:

I don't see any conflicts in the jars and dependencies. I have been staying with the problem for several days.

xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.cufe</groupId>
    <artifactId>dc-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.2-b02</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <!--compatible with mysql8.0-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
        </dependency>

        <!--HtmlUnit(headless web browser)-->
        <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit -->
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.33</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.ansj/ansj_seg -->
        <dependency>
            <groupId>org.ansj</groupId>
            <artifactId>ansj_seg</artifactId>
            <version>5.1.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>7.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>7.7.1</version>
        </dependency>
    </dependencies>


    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>*.xml</include>
                    <include>*.properties</include>
                    <include>*.txt</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Below is the mvn tree of all the dependencies:

Java

--------------------------< com.cufe:dc-web >---------------------------
[INFO] Building dc-web 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ dc-web ---
[INFO] com.cufe:dc-web:war:1.0-SNAPSHOT
[INFO] +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
[INFO] +- javax.servlet.jsp:javax.servlet.jsp-api:jar:2.3.2-b02:provided
[INFO] +- jstl:jstl:jar:1.2:compile
[INFO] +- taglibs:standard:jar:1.1.2:compile
[INFO] +- mysql:mysql-connector-java:jar:8.0.15:compile
[INFO] |  \- com.google.protobuf:protobuf-java:jar:3.6.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.4:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.4:compile
[INFO] +- net.sourceforge.htmlunit:htmlunit:jar:2.33:compile
[INFO] |  +- xalan:xalan:jar:2.7.2:compile
[INFO] |  |  \- xalan:serializer:jar:2.7.2:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.8:compile
[INFO] |  +- org.apache.commons:commons-text:jar:1.4:compile
[INFO] |  +- org.apache.httpcomponents:httpmime:jar:4.5.6:compile
[INFO] |  |  \- org.apache.httpcomponents:httpclient:jar:4.5.6:compile
[INFO] |  |     +- org.apache.httpcomponents:httpcore:jar:4.4.10:compile
[INFO] |  |     \- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.33:compile
[INFO] |  +- net.sourceforge.htmlunit:neko-htmlunit:jar:2.33:compile
[INFO] |  |  \- xerces:xercesImpl:jar:2.12.0:compile
[INFO] |  |     \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] |  +- net.sourceforge.htmlunit:htmlunit-cssparser:jar:1.2.0:compile
[INFO] |  +- commons-io:commons-io:jar:2.6:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  +- commons-net:commons-net:jar:3.6:compile
[INFO] |  \- org.eclipse.jetty.websocket:websocket-client:jar:9.4.12.v20180830:compile
[INFO] |     +- org.eclipse.jetty:jetty-client:jar:9.4.12.v20180830:compile
[INFO] |     |  \- org.eclipse.jetty:jetty-http:jar:9.4.12.v20180830:compile
[INFO] |     +- org.eclipse.jetty:jetty-xml:jar:9.4.12.v20180830:compile
[INFO] |     +- org.eclipse.jetty:jetty-util:jar:9.4.12.v20180830:compile
[INFO] |     +- org.eclipse.jetty:jetty-io:jar:9.4.12.v20180830:compile
[INFO] |     \- org.eclipse.jetty.websocket:websocket-common:jar:9.4.12.v20180830:compile
[INFO] |        \- org.eclipse.jetty.websocket:websocket-api:jar:9.4.12.v20180830:compile
[INFO] +- org.ansj:ansj_seg:jar:5.1.6:compile
[INFO] |  \- org.nlpcn:nlp-lang:jar:1.7.7:compile
[INFO] +- org.jsoup:jsoup:jar:1.11.3:compile
[INFO] +- org.apache.lucene:lucene-core:jar:7.7.1:compile
[INFO] \- org.apache.lucene:lucene-queryparser:jar:7.7.1:compile
[INFO]    +- org.apache.lucene:lucene-queries:jar:7.7.1:compile
[INFO]    \- org.apache.lucene:lucene-sandbox:jar:7.7.1:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.467 s
[INFO] Finished at: 2019-04-01T12:31:00+08:00
[INFO] ------------------------------------------------------------------------

Upvotes: 1

Views: 3400

Answers (3)

Chang Liu
Chang Liu

Reputation: 21

Finally,I found the cause of the bug by myself,hhhhhh. That's surely because of conflict of jars.But it's not the common case. In our project, there are already three external jars created by my teammates.Yes,they use the class org.nlpcn.commons.lang.util.logging.LogFactory in their jars.So there exists conflict.That's it.I will solve the problem by creating my own jar in avoid of conflict.

Upvotes: 1

Vladimir
Vladimir

Reputation: 710

Sorry i made a mistake ...pls. try to exclude once again ... :

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>2.33</version>
    <exclusions>
      <exclusion>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
      </exclusion>
    </exclusions>
</dependency>

Upvotes: 0

Vladimir
Vladimir

Reputation: 710

Hi try something like this:

add a maven dependecy

<!-- same dependency as used in htmlunit, try to remove after its working -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>  
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

Than create 2 Files in /src/main/resources

commons-logging.properties

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.configuration=log4j.properties

log4j.properties

log4j.rootLogger=DEBUG, CA, NTEventLog

#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

In this case your commons logging is using log4j. Perhaps this helps.

Or you can try excluding commons-logging:

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>2.33</version>
    <exclusions>
      <exclusion>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-logging</artifactId>
      </exclusion>
    </exclusions>
</dependency>

and add an another your preferred logging lib

Upvotes: 0

Related Questions