Expand my Community achievements bar.

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

Error while using data-sly-use to call Java class method

Avatar

Level 4

I am getting below error while using data-sly-use to call Java class method. what can be the issue ? any configuration issue ?

 

17.06.2020 01:37:28.281 *ERROR* [0:0:0:0:0:0:0:1 [1592372248213] GET /content/we-retail/us/en/sample.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/content/we-retail/us/en/sample/jcr:content/root/responsivegrid/customcomponent', selectorString='null', extension='html', suffix='null'
org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: No use provider could resolve identifier mycomponent
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:510) [org.apache.sling.scripting.core:2.0.56]
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) [org.apache.sling.engine:2.6.18]
at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:44) [org.apache.sling.engine:2.6.18]
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:82) [org.apache.sling.engine:2.6.18]

 

HTL component - 

 

<div data-sly-use.titleobj="mycomponent">
<h1>${titleobj.myTitle}</h1>
</div>

 

 

java class

mycomponent.java

 

import com.adobe.cq.sightly.WCMUsePojo;

public class mycomponent extends WCMUsePojo
{

private String myTitle;

@Override
public void activate()
{
myTitle = "My Project" + getCurrentPage().getTitle();
}

public string getmyTitle()
{
return myTitle;
}

}

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@sonuk85184451 I read from the comments that you have your Java class in the same folder. This error clearly says that the Java class is not available. In your case it can be because that the Java Class is not compiled properly. If you see that your Java Class name is in small letters. As per Java standards and compiler , you need your Class name to start with Capital case. This will compile your Java classes properly and this issue should get resolved I believe. Please change your Java class name to MyComponent.java and test this

 

Disclaimer:- I am not 100% sure on this since I have never used Java class inside component. but give it a try and let us know

 

Side note:- I would recommend always writing Sling Models rather than writing it inside the component folder itself. 

 

Veena ✌

 

View solution in original post

5 Replies

Avatar

Community Advisor

Hi @sonuk85184451 ,

 

you have used mycomponent.java class as
<div data-sly-use.titleobj="mycomponent">
when you specify java class without its package name, it means the java class is expected to be present in the same folder where your HTL file is present.

Looking at the exception you are getting, it is clear the class is not present in the same folder of your component . You have to specify your class with package name.

For ex: if class is present in package com.myproject.mymodels.mycomponent, then, the use include should be 

<div data-sly-use.titleobj="com.myproject.mymodels.mycomponent">

 

You can refer https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/use-api-java.html for more details on where you can create your java class and how to call it in HTL.

 

Thanks,

Nupur

 

Avatar

Level 4

Hi @Nupur_Jain - thanks for responding. component.java is in same folder only . 

 

arunk85184451_0-1592375947330.png

 

Avatar

Community Advisor

@sonuk85184451 

 

You have not added any package statement to the java class created. You will have to add

"package apps.weretail.components.content.customcomponent;" in the begining of java file.

 

Try this and let me know.

Avatar

Correct answer by
Community Advisor

@sonuk85184451 I read from the comments that you have your Java class in the same folder. This error clearly says that the Java class is not available. In your case it can be because that the Java Class is not compiled properly. If you see that your Java Class name is in small letters. As per Java standards and compiler , you need your Class name to start with Capital case. This will compile your Java classes properly and this issue should get resolved I believe. Please change your Java class name to MyComponent.java and test this

 

Disclaimer:- I am not 100% sure on this since I have never used Java class inside component. but give it a try and let us know

 

Side note:- I would recommend always writing Sling Models rather than writing it inside the component folder itself. 

 

Veena ✌