Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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>```
                It looks like the method is only in Gson 2, not Gson 1. Unirest uses Gson 2.8.5 Can you show your Maven dependencies?
– Yserbius
                Apr 3, 2020 at 3:53

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

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.

@user13192903 try to use version 2.8.6 version of gson, but the existing repository in m2 and try clean build. – backdoor Apr 3, 2020 at 4:29

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>
                Hey I have tried this along with many other things, but I still cannot get it. i also now get > [18:39:44 WARN]: java.util.concurrent.ExecutionException: java.lang.NoClassDefFoundError: Could not initialize class kong.unirest.json.JSONObject 
– user13192903
                Apr 3, 2020 at 22:53
                I know, such things happen sometimes :-) I can't see your code so I still believe there's a conflict in versions (sometimes it's just duplicates in dependencies) or incorrect usage. I'd start the separate project strongly following the docs kong.github.io/unirest-java and see if it works.
– Viktor Born
                Apr 4, 2020 at 1:08
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.