NoCLassFoundError while using ResourceResolver | Community
Skip to main content
Level 6
June 13, 2022
Solved

NoCLassFoundError while using ResourceResolver

  • June 13, 2022
  • 3 replies
  • 1753 views

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 

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 DEBAL_DAS

ok surw @debal_das thanks


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?lang=en

 

 

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.

3 replies

DEBAL_DAS
New Member
June 13, 2022

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.

 

Debal Das, Senior AEM Consultant
Level 6
June 13, 2022

HI @debal_das 

 

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

 

thanks

DEBAL_DAS
New Member
June 13, 2022

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.

Debal Das, Senior AEM Consultant
arunpatidar
Community Advisor
Community Advisor
June 13, 2022

Hi,

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

 

It seems sling model is not created with proper annotations

Arun Patidar
ArpitVarshney
Community Advisor
Community Advisor
June 13, 2022

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