user13192903
user13192903

Reputation: 1

java.lang.NoSuchMethodError: com.google.gson.Gson.newBuilder()Lcom/google/gson/GsonBuilder;

I'm trying to use Unirest in my program but I keep getting this error java.lang.NoSuchMethodError: com.google.gson.Gson.newBuilder()Lcom/google/gson/GsonBuilder;

I have tried using different maven versions of Gson but I still continue to get this seror

Edid added my pom.xml, I tried deleted my .m2 but I am still having this problem

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.konghq/unirest-java -->
        <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.7.00</version>
            <classifier>standalone</classifier>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.twitter4j</groupId>
            <artifactId>twitter4j-core</artifactId>
            <version>[4.0,)</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>Spigot8</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/spigot-1.8.8-R0.1-SNAPSHOT-latest.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>LATEST</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>```

Upvotes: 0

Views: 7140

Answers (3)

Tapiwa
Tapiwa

Reputation: 49

Could be a conflict between the GSON versions in your POM file versus what is in your container (Tomcat/JBoss) etc. Inspect the JARS in WEB-INF/lib. If there are two different versions, then delete the one which is not the same as the one currently in your maven project's POM file. I had a similar issue - my project was using gson-2.6.3.jar but a conflicting gson-2.1.jar also existed in WEB-INF/lib. I deleted gson-2.1.jar and this resolved my problem

Upvotes: 2

Viktor Born
Viktor Born

Reputation: 396

It seems like a dependencies issue.

Please make sure your dependencies are correct. Unirest 3.7.00 uses Gson 2.8.6

https://mvnrepository.com/artifact/com.konghq/unirest-java/3.7.00

Also, if you don't use Gson separately you don't need to specify it. Just add a dependency for unirest-java as it's described in Maven repository:

<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.7.00</version>
</dependency>

Upvotes: 0

backdoor
backdoor

Reputation: 901

This issue is possibly due to conflicts in Gson versions.

Go to your repository if you are on windows:

C:\Users\User_name.m2\repository\com\google\code\gson

or on Mac:-

/.m2/repository/com/google/code/gson

Delete all the existing folders.

Now, add below maven dependency in your pom file:

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

Re-build your maven project and try to run.

Upvotes: 1

Related Questions