12

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. Now when I am trying to run my code, I get the following error.

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
    at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
    at com.smartsheet.api.internal.http.DefaultHttpClient.<init>(DefaultHttpClient.java:64)
    at com.smartsheet.api.SmartsheetBuilder.build(SmartsheetBuilder.java:203)
    at SmartsheetConnection.main(SmartsheetConnection.java:13)

This is my code (I followed their documentation).

import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.models.enums.*;
import com.smartsheet.api.oauth.*;

public class SmartsheetConnection {
    public static void main(String[] args) throws SmartsheetException {
        // Set the access token.
        Token token = new Token();
        token.setAccessToken("foo");
        Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken(token.getAccessToken()).build();
    }
}

The line that is throwing error is (line 144)

@Deprecated
    public static final X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER
        = AllowAllHostnameVerifier.INSTANCE; 

but I am not sure what to make of it. I am using maven to get the dependencies. Does it have something to do with the version of the Apache HttpComponents?

Here is the pom.xml

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.smartsheet</groupId>
            <artifactId>smartsheet-sdk-java</artifactId>
            <version>2.0.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-apache-client</artifactId>
            <version>1.9</version>
        </dependency>
    </dependencies>
mahacoder
  • 895
  • 3
  • 14
  • 29

4 Answers4

11

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:

Community
  • 1
  • 1
Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
  • This was it in my case. Older copy of the jars in a unit test folder that was part of the project. – David Thielen Dec 26 '16 at 16:41
  • Thanks for the update, David. Glad to hear that you identified (and resolved) the issue. When you have a moment, can you please mark this Answer as "accepted", so that others may benefit from the info in the future? Thanks! – Kim Brandl Dec 26 '16 at 20:54
  • Thanks for the upvote, David. Sorry, I didn't notice that it wasn't your question originally...but appreciate you chiming in for the benefit of others. – Kim Brandl Dec 27 '16 at 02:36
  • @KimBrandl I'm facing same issue. My project will be a dependency to a parent project which is again having multiple child projects where mulitple dependencies like spark-core and hbase-cients internally contains httpclient jar. So, how to force maven to use particular maven version of a dependency so that we won't face any runtime issue like nosuchfielderror? – Pruthvi Chitrala Apr 09 '17 at 07:42
  • @PruthviChitrala one possible way is to exclude the older dependencies brought by spark-core or hbase-clients or any other dependency. You could do this using tag and specifying the artifact that you want to exclude from that . See Dependency Exclusions from https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html – Agraj Jun 23 '17 at 11:31
  • Super helpful. In my case there was a bunch of old version jars. I was trying to access ews-java-api.jar. Removing all the old version jars for different packages from glassfish/domain.../lib/ resolved this. – DGT Jun 27 '17 at 22:51
5

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

Animesh Sinha
  • 1,045
  • 9
  • 16
1

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

xuedisong
  • 11
  • 2
0

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

user2829759
  • 3,372
  • 2
  • 29
  • 53