Expand my Community achievements bar.

SOLVED

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

Avatar

Level 9

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 ?
1 Accepted Solution

Avatar

Correct answer by
Level 9

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, 

Mario248_0-1714649085346.png

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>

View solution in original post

5 Replies

Avatar

Community Advisor

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

Avatar

Community Advisor

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

Avatar

Correct answer by
Level 9

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, 

Mario248_0-1714649085346.png

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>

Avatar

Administrator

@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

Avatar

Community Advisor

Hello @Mario248 ,

Please check this thread, https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/upgrading-uber-jar-from-ae...
Try with put the dependency just before the uber jar in core pom.xml