java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application

前端 未结 5 625
半阙折子戏
半阙折子戏 2020-11-29 22:45

I have a Maven Java project that uses Mashape Unirest for sending HTTP requests to other URLs. I am currently writing an integration test (using TestNG) tha

5条回答
  •  没有蜡笔的小新
    2020-11-29 23:12

    As already mentioned by previous comments, It's mainly because of the conflicting versions of httpcore jar, the static field INSTANCE is been added to BasicLineFormatter class in versions > 4.3.1, Though you might have added the latest version of the httpcore jar in your dependencies, but its highly possible that other (lower) version of jar is getting picked up.

    So, first to confirm that, wrong jar is getting picked up, Use the following line of code -

    ClassLoader classLoader = .class.getClassLoader();
    URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
    System.out.println(resource);
    

    If this prints, the lower version of the jar, then it's confirmed that it's picking the lower version of the httpcore jar (May be from other dependencies of your project),

    Solution -

    Add following maven/gradle dependencies at the top of dependency list (Or above the other project dependency which caused the conflict) -

    
         com.mashape.unirest
         unirest-java
         1.4.5
    
    
        org.apache.httpcomponents
        httpcore
        4.4.1
    
    
        org.apache.httpcomponents
        httpclient
        4.4.1
    
    

提交回复
热议问题