简体   繁体   English

如何修复 java.lang.NoSuchMethodError?

[英]How to fix a java.lang.NoSuchMethodError?

I'm deploying a Java web application.我正在部署一个 Java Web 应用程序。 But, the ansj package doesn't work normally when deployed using Tomcat.但是, ansj包在使用 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:下面是我相关的 pom.xml:

I don't see any conflicts in the jars and dependencies.我在 jars 和依赖项中没有看到任何冲突。 I have been staying with the problem for several days.我一直在解决这个问题好几天了。

xml 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:以下是所有依赖项的 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] ------------------------------------------------------------------------

Finally,I found the cause of the bug by myself,hhhhhh.终于,我自己找到了bug的原因,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.在我们的项目中,我的队友已经创建了三个外部jar。是的,他们在他们的jar中使用类org.nlpcn.commons.lang.util.logging.LogFactory。所以存在冲突。就这样。我会解决通过创建我自己的 jar 以避免冲突来解决问题。

Hi try something like this:嗨尝试这样的事情:

add a maven dependecy添加 maven 依赖

<!-- 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比在 /src/main/resources 中创建 2 个文件

commons-logging.properties commons-logging.properties

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

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.在这种情况下,您的公共日志记录使用的是 log4j。 Perhaps this helps.也许这有帮助。

Or you can try excluding commons-logging:或者您可以尝试排除 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并添加另一个您喜欢的日志库

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>

暂无
暂无

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

相关问题 如何修复java.lang.NoSuchMethodError - How to fix java.lang.NoSuchMethodError 线程“ main”中的异常java.lang.NoSuchMethodError:main-如何解决? - Exception in thread “main” java.lang.NoSuchMethodError: main - how to fix it? java.lang.NoSuchMethodError: - java.lang.NoSuchMethodError: java.lang.NoSuchMethodError - java.lang.NoSuchMethodError 如何获取 java.lang.NoSuchMethodError - How to get java.lang.NoSuchMethodError 如何修复 java.lang.NoSuchMethodError: sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;) - How to fix java.lang.NoSuchMethodError: sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;) 如何修复在Hive下运行的geoip2 java的“java.lang.NoSuchMethodError” - How to fix “java.lang.NoSuchMethodError” for geoip2 java running under Hive 如何修复此错误:java.lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)' - How to fix this error: java.lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)' 即使存在这样的方法,也如何修复java.lang.NoSuchMethodError,并且在本地服务器中不会​​发生错误 - How to fix java.lang.NoSuchMethodError even if such a method exist, and error do not occur in local server 知道如何解决这个问题吗? 线程“WindowsNativeRunloopThread”中的异常 java.lang.NoSuchMethodError - Any idea how to fix this? Exception in thread "WindowsNativeRunloopThread" java.lang.NoSuchMethodError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM