Error while trying to use an API. java.lang.NoSuchFieldError: INSTANCE

后端 未结 4 906
北海茫月
北海茫月 2020-12-11 03:05

I am trying to connect to the smartsheet api using a java program. Initially I had problems with the site certificate which was resolved by adding it to the java keystore. N

相关标签:
4条回答
  • 2020-12-11 03:29

    the reason for this problem is: org.apache.http.conn.ssl.AllowAllHostnameVerifier class,which is execused in the runtime,has no field 'INSTANCE'.

    My project classpath contains two same name class of "org.apache.http.conn.ssl.AllowAllHostnameVerifier". One cames from a jar customized by our company,which has no field 'INSTANCE'. Another cames from maven central repository,which has the field 'INSTANCE'. My code sometimes run the logic of the latter jar and sometimes the fromer jar,which is the reason I guessed.

    my classpath search result

    the comparison of the two jar

    0 讨论(0)
  • 2020-12-11 03:33

    I know its I am replying a bit late actually I am also struggling the same problem and I found the solution by using Maven Shade plugin.

    The Problem is the JAR conflict probably your project is using a different Version Of HTTPclient then your container over which your Appliaction is running.

    To resolve this use the Below Maven Shade Plugin which will change the package name of HttpClient to the specified one which packaging the JAR. This will also refactor all the usage in your code.

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <relocations>
                  <relocation>
                    <pattern>org.apache.http</pattern>
                    <shadedPattern>org.shaded.apache.http</shadedPattern>
                  </relocation>
                </relocations>
            </configuration>
        </execution>
    </executions>
    </plugin>
    

    The Above sample will change HttpClient Package with org.shaded.apache.http from org.apache.http

    Maven Shade will also create a fat/uber jar so your final package size get increased and will have all the classes which you have mentioned in the Dependency in POM.

    If you don't want to include the all your dependency jar in your final jar then add the Scope for the Dependency as <scope>provided</scope>.

    0 讨论(0)
  • 2020-12-11 03:35

    Other posts about this error seem to suggest that it's typically caused by conflicting versions of httpcore jar. i.e., an older version of httpcore on the classpath.

    For more information, I'd suggest you checkout the following posts:

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

    • java.lang.NoSuchFieldError: INSTANCE

    0 讨论(0)
  • 2020-12-11 03:42

    I was using intellij for both android and spring development. In my case, I accidentally chose Android sdk as the module SDK.

    After choosing JDK 1.8 and rebuilding the project fixed the issue for me

    0 讨论(0)
提交回复
热议问题