Gradle jar java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder

I have created a new project with vscode, plugins like java and gradle are installed, in the build.gradle I write under dependencies { implementation ‘’. }

and then see the jar under “Project and External Dependencies” in vscode and in the gradle plugin, and can create an instance of Gson in the mainclass by import and so on.

When I run the code everything works. No erros. But when I create a jar with “gradlew build” then I get java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder as error message, when trying to run the .jar file.

I have now searched the entire internet and can’t find a solution. I am using gradle 7.3. do you have any tips or ideas?

Jar files do not typically contain your dependencies and by default Gradle produces a jar containing only the classes built by the project. The expectation is that you are providing your jar and dependency jars on the classpath at runtime.

If you want to repackage all of your dependencies into your jar then I suggest you take a look at using an additional plugin for creating a “fat jar”. For example, John Engelman’s shadow plugin is a popular choice.

I’d recommend you do not use a fat jar though, but use a proper approach, for example by using the application plugin, which will not build a fat “runnable” jar, but a proper distributable archive with all your code, your dependencies and start scripts that glue all together properly: The Application Plugin