如何修复java.lang.NoSuchMethodError?

时间:2019-03-31 10:15:51

标签: java

我正在部署Java Web应用程序。但是,ansj软件包在使用Tomcat部署时无法正常工作。奇怪的是,它在本地运行时效果很好。

如何解决此问题?

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

下面是我相关的pom.xml:

我在jar和依赖项中看不到任何冲突。我一直困扰着这个问题好几天了。

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>

下面是所有依赖项的mvn树:

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

3 个答案:

答案 0 :(得分:1)

最后,我自己找到了导致此错误的原因,呵呵。 这肯定是由于罐子的冲突造成的,但这不是常见的情况。 在我们的项目中,我的队友已经创建了三个外部jar。是的,他们在其jar中使用了org.nlpcn.commons.lang.util.logging.LogFactory类。因此存在冲突。就是这样。我将解决通过创建自己的jar避免冲突来解决问题。

答案 1 :(得分:0)

嗨,尝试这样的事情:

添加行家依赖

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

比在/ src / main / resources中创建2个文件

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

在这种情况下,您的公共记录使用的是log4j。 也许这会有所帮助。

或者您可以尝试排除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>

并添加另一个您首选的日志记录库

答案 2 :(得分:0)

对不起,我犯了一个错误。尝试再次排除...:

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