Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Getting NoClassDefFoundError exception for com/google/cloud/recaptchaenterprise/v1/RecaptchaEnterpriseServiceClient

Avatar

Level 4


Hello everyone,

 

We are integrating Google reCaptcha enterprise version with AEM cloud. We are able to load reCaptcha challenge on custom form but while generating assessment score(through java code) we are getting NoClassDefFoundError exception.
We have imported below in 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.*;version=0.0.0;resolution:=optional,*
]]></bnd>
</configuration>
</execution>
</executions>
</plugin>
 
maven build is getting successful and bundle is also active. But its giving error for below line in java class:
RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()
 
supriyahande_2-1717490672387.png

Error: 

com/google/cloud/recaptchaenterprise/v1/RecaptchaEnterpriseServiceClient

Cannot serve request to /content/my-program/home/members.recaptcha-response in com.my.program.core.servlets.GetRecaptchaResponseServlet

Exception:

java.lang.NoClassDefFoundError: com/google/cloud/recaptchaenterprise/v1/RecaptchaEnterpriseServiceClient
	at com.my.program.core.services.RecaptchaService.getResponse(RecaptchaService.java:61)
	at com.my.program.core.servlets.GetRecaptchaResponseServlet.doPost(GetRecaptchaResponseServlet.java:44)


Can anyone please help here? 

 
4 Replies

Avatar

Community Advisor

Hi, 

The issue is caused because the Java class is not present at runtime, that is because you need to wrap the Google JAR into your bundle. Please check this thread: https://aemconcepts.com/aem-osgi/use-3rd-party-api-or-jar-or-dependency-in-aem/

 

Hope this helps



Esteban Bustamante

Avatar

Community Advisor

Hi @supriya-hande ,

The NoClassDefFoundError for com/google/cloud/recaptchaenterprise/v1/RecaptchaEnterpriseServiceClient indicates that the class is not available in the classpath at runtime, even though the Maven build is successful and the bundle is active. This can happen due to several reasons, such as incorrect packaging of dependencies or issues with OSGi classloading. Here are the steps you can take to resolve this issue:

Steps to Resolve the Issue

  1. Ensure Dependency is Properly Included: Make sure that the required Google Cloud ReCaptcha Enterprise dependency is included in your core module’s pom.xml.

 

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-recaptchaenterprise</artifactId>
    <version>1.1.3</version>
</dependency>

 

Embed the Dependency in the OSGi Bundle: You need to ensure that the google-cloud-recaptchaenterprise dependency and its transitive dependencies are embedded in the OSGi bundle. This can be done using the maven-bundle-plugin or the bnd-maven-plugin.

 

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>4.2.1</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Embed-Dependency>
                google-cloud-recaptchaenterprise;scope=compile|runtime
            </Embed-Dependency>
            <Import-Package>
                !com.google.cloud.recaptchaenterprise.v1.*, 
                *
            </Import-Package>
        </instructions>
    </configuration>
</plugin>

 

Shade the Dependency: Another approach is to shade the dependency. This means relocating the package to avoid any classloader issues. This is particularly useful if there are conflicting versions of the same dependency.

 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <relocations>
                    <relocation>
                        <pattern>com.google.cloud.recaptchaenterprise.v1</pattern>
                        <shadedPattern>my.shaded.google.cloud.recaptchaenterprise.v1</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </execution>
    </executions>
</plugin>

 

 

  • Check for Transitive Dependencies: Ensure all transitive dependencies required by google-cloud-recaptchaenterprise are included and properly embedded in the OSGi bundle.

  • Review and Update BND Instructions: The bnd-maven-plugin configuration you have might need adjustments. Ensure that the package import instructions are correct and do not unintentionally exclude necessary packages.

 

<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.*;version=0.0.0;resolution:=optional,*
                Embed-Dependency: google-cloud-recaptchaenterprise;scope=compile|runtime
                ]]></bnd>
            </configuration>
        </execution>
    </executions>
</plugin>

 

  1. Check OSGi Dependencies: Verify that the google-cloud-recaptchaenterprise library and its dependencies are correctly resolved and active in the OSGi environment. You can do this by using the AEM Web Console:

    • Go to http://<aem-instance>:4502/system/console/bundles.
    • Search for google-cloud-recaptchaenterprise and ensure that its bundle is active.

Example Project Structure

Here’s an example of what your core module’s pom.xml might look like with the maven-bundle-plugin configuration:

 

<project>
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-recaptchaenterprise</artifactId>
            <version>1.1.3</version>
        </dependency>
        ...
    </dependencies>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>4.2.1</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Embed-Dependency>
                            google-cloud-recaptchaenterprise;scope=compile|runtime
                        </Embed-Dependency>
                        <Import-Package>
                            !com.google.cloud.recaptchaenterprise.v1.*, 
                            *
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
            ...
        </plugins>
    </build>
    ...
</project>

 

 

By ensuring that the google-cloud-recaptchaenterprise dependency and its transitive dependencies are correctly embedded and resolved in your OSGi bundle, you should be able to resolve the NoClassDefFoundError. Additionally, make sure that there are no conflicting versions of the same dependency and that the build process correctly includes all necessary classes.


Avatar

Community Advisor

@supriya-hande  -  As suggested by @EstebanBustamante  you need to add the jar as part of bundle, i did it long time back and here is the process https://aemsimplifiedbynikhil.wordpress.com/2020/08/23/resolve-dependencies-by-converting-jar-to-bun... .


But please check the link shared by Esteban to see how you can include the Jar as bundle. As my approach might be outdated.

Avatar

Administrator

@supriya-hande Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni