Unable to use com.google.code.gson class in aem | Community
Skip to main content
Mario248
Level 7
May 1, 2024
Solved

Unable to use com.google.code.gson class in aem

  • May 1, 2024
  • 3 replies
  • 2665 views

I am trying to use GsonBuilder from com.google.code.gson so I added below depdency in 

main/pom.xml

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>

core/pom.xml

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

Also I imported gson package in "bnd-maven-plugin" plugin as below

core/pom.xml

<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<executions>
<execution>
<id>bnd-process</id>
<goals>
<goal>bnd-process</goal>
</goals>
<configuration>
<bnd><![CDATA[
Import-Package: javax.annotation;version=0.0.0,!com.google.gson.*,*
]]></bnd>
</configuration>
</execution>
</executions>
</plugin>

I am including this in one of my servlet, 

Gson gson = new GsonBuilder().create();
Climate climateRes = gson.fromJson(climateResponse, Climate.class);
Caused by: java.lang.ClassNotFoundException: com.google.gson.GsonBuilder not found by my-project.core [643]
 
I tried to check the status of my servlet in felix console(http://localhost:4502/system/console/components) and it is active and I dont see any problem. Also I tried search "com.google.code.gson" bundle in http://localhost:4502/system/console/bundles and I dont see this bundle. Should I change any scope? I tried with <scope>provided</scope> but did not help anything.
 
Am I missing anything here ?
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mario248

It worked after removing ! and import package. Thanks for your input. 

But still I dont see Gson (com.google.gson) bundle not appearing in system/console. But my servlet code is working fine. I wonder how it is working. I tried to search this "com.google.gson" java package in http://localhost:4502/system/console/depfinder and found below details, 

My bundle is also listed. but my POM file have below 2.10.1 but I see 2.8.9 version. I am really confused who provided this version. 

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
<scope>test</scope>
</dependency>

3 replies

sravs
Community Advisor
Community Advisor
May 1, 2024

@mario248 , Try upgrading to latest version of gson dependency.

gkalyan
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 1, 2024

@mario248 

TheClassNotFoundException indicates that the Gson library is not available at runtime in your OSGi environment.
 
Like @sravs mentioned it might be due to version of gson. 
 
This might be simple but check to make sure that the Gson library is properly installed and started as an OSGi bundle as com.google.code.gson bundle is not in the Felix console.
 
In your bnd-maven-plugin configuration, you have excluded com.google.gson.* from the import package directive.
You might need to remove the !com.google.gson.* exclusion to allow OSGi to import Gson packages.
 
Finally regarding the scope, "provided" is used when the container already provides the dependency at runtime. If the Gson library is not provided by your runtime environment, you should not use this scope. The default compile scope should be sufficient.
 
Mario248
Mario248AuthorAccepted solution
Level 7
May 2, 2024

It worked after removing ! and import package. Thanks for your input. 

But still I dont see Gson (com.google.gson) bundle not appearing in system/console. But my servlet code is working fine. I wonder how it is working. I tried to search this "com.google.gson" java package in http://localhost:4502/system/console/depfinder and found below details, 

My bundle is also listed. but my POM file have below 2.10.1 but I see 2.8.9 version. I am really confused who provided this version. 

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
<scope>test</scope>
</dependency>
kautuk_sahni
Community Manager
Community Manager
June 7, 2024

@mario248 We hope the AEM community has been beneficial to you. We look forward to your return as either a learner or a mentor. The community is enriched by SMEs like you. Please encourage your AEM peers to also participate. Happy AEM learning!

Kautuk Sahni
Sady_Rifat
Community Advisor
Community Advisor
May 2, 2024

Hello @mario248 ,

Please check this thread, https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/upgrading-uber-jar-from-aem-6-5-5-to-6-5-17-creates-issues-in/m-p/610049
Try with put the dependency just before the uber jar in core pom.xml