Expand my Community achievements bar.

SOLVED

NoCLassFoundError while using ResourceResolver

Avatar

Level 7

Hi 

"C:\Program Files\Java\jdk-11.0.14\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1\lib\idea_rt.jar=51762:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1\bin" -Dfile.encoding=UTF-8 -classpath D:\AEM\AEM_POC\aempoc\core\target\classes;C:\Users\akshay\.m2\repository\com\adobe\cq\core.wcm.components.core\2.18.0\core.wcm.components.core-2.18.0.jar com.aempoc.core.models.demo
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/sling/api/resource/ResourceResolver
at com.aempoc.core.models.demo.main(demo.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.sling.api.resource.ResourceResolver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more

Process finished with exit code 1

 

 

while executing below code

 

package com.aempoc.core.models;

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

public class demo {
@SlingObject
private static ResourceResolver resourceResolver;

public static void main(String args[]){
System.out.println("HI"+ resourceResolver.getUserID());
}
}

 @sunil_kumar_ @DEBAL_DAS @Bhuwan_B @arunpatidar 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

No BundleContext bundleContext = FrameworkUtil.getBundle(type).getBundleContext(); won't be working from main class due to below reason -

 

"A Bundle object is the access point to define the lifecycle of an installed bundle. Each bundle installed in the OSGi environment must have an associated Bundle object."

 

Here , main class isn't part of the installed bundle.

 

If you are planning to perform CRUD operation from main class then you can use javax.jcr.Session https://experienceleague.adobe.com/docs/experience-manager-64/developing/platform/access-jcr.html?la...

 

 

But If you want to use ResourceResolver object at any cost then you can use groovy script.

 

Before that I would like to know the use case first.

View solution in original post

7 Replies

Avatar

Employee Advisor

Annotation: @SlingObject
Description: Injects commonly used sling objects if the field matches with the class: request, response, resource resolver, current resource, SlingScriptHelper. 

 

We use @SlingObject with Sling model and it's a Sling model annotation. We can't directly use @SlingObject in main class.

 

Please let me know your use case.

 

Avatar

Level 7

HI @DEBAL_DAS 

 

so if I want to user resourceresolver instance in mail class what i need to do.

 

thanks

Avatar

Employee Advisor

I feel we can give a try with 

BundleContext bundleContext = FrameworkUtil.getBundle(type).getBundleContext();

 

As  sometimes we need an OSGi service or the BundleContext also in non OSGi / DS controlled class.

 

Let me do one POC and I will share my observation with you.

Avatar

Correct answer by
Employee Advisor

No BundleContext bundleContext = FrameworkUtil.getBundle(type).getBundleContext(); won't be working from main class due to below reason -

 

"A Bundle object is the access point to define the lifecycle of an installed bundle. Each bundle installed in the OSGi environment must have an associated Bundle object."

 

Here , main class isn't part of the installed bundle.

 

If you are planning to perform CRUD operation from main class then you can use javax.jcr.Session https://experienceleague.adobe.com/docs/experience-manager-64/developing/platform/access-jcr.html?la...

 

 

But If you want to use ResourceResolver object at any cost then you can use groovy script.

 

Before that I would like to know the use case first.

Avatar

Community Advisor

Hi,

Please check sling model example at https://github.com/arunpatidar02/aemaacs-aemlab/blob/master/core/src/main/java/com/community/aemlab/... 

 

It seems sling model is not created with proper annotations



Arun Patidar

Avatar

Community Advisor

Hey @akshaybhujbale 

Completely agreed with @DEBAL_DAS, these annotations worked when your java class is defined as Sling Model and for that, there are mandatory annotations.

You can learn it from official documentations here https://sling.apache.org/documentation/bundles/models.html 

Regards,

Arpit Varshney