Showing posts with label debug AEM and IntelliJ. Show all posts
Showing posts with label debug AEM and IntelliJ. Show all posts

Sunday, June 14, 2020

Debugging in AEM and IDE

In this tutorial, we will first start AEM in debug mode and add remote debug connection in IDE(IntelliJ).

What is Debugging?

It is a systematic process of spotting and fixing the number of bugs, or defects, in a piece of software so that the software is behaving as expected. 

Debugging refers to identifying, analyzing, and removing errors. It is considered to be an extremely complex and tedious task because errors need to be resolved at all stages of debugging. 

When we work in IDE then we need to debug code to find or check what data is capturing.

We normally work on IntelliJ or Eclipse in the AEM project.

There are multiple ways to start AEM in debug mode.

    Step 1: Add the Remote Debugging JVM Parameter

    Start AEM with this JVM parameter

    -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n
    

    Another way to launch AEM instance in debug mode: 
    Include it as a parameter when starting AEM with java -jar directly.

    •  java -Xmx512m -agentlib:jdwp=transport=dt_socket,address=30303,server=y,suspend=n -jar cq-author-4502.jar

    Another way to start AEM in debug mode, do changes in start.bat file for windows

    For Windows Users

    • Go to <AEM-Install>\crx-quickstart\bin
    • Open start.bat file
    • Go to line #25

    You can see line content is:

    if not defined CQ_JVM_OPTS set CQ_JVM_OPTS=-Xmx1024m -XX:MaxPermSize=256M -Djava.awt.headless=true
    

    Append below content to end of this line #25

    You can see line content is:

    if not defined CQ_JVM_OPTS set CQ_JVM_OPTS=-Xmx1024m -XX:MaxPermSize=256M -Djava.awt.headless=true
    

    Append below content to end of this line #25

    -debug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=<port>

    You can use the below line directly. I am using address 30303. You can map to any available port.

    if not defined CQ_JVM_OPTS set CQ_JVM_OPTS=-Xmx1024m -XX:MaxPermSize=256M -Djava.awt.headless=true -debug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=30303
    • Save start.bat file.
    • Start AEM now. Double click to start.bat to run your AEM instance.
    For Mac users:

    You can use the below line in start.sh directly in Mac OS. -z means zero length. for example, if [ -z "$CQ_JVM_OPTS" ]; then means if $CQ_JVM_OPTS is empty. 

      if [ -z "$CQ_JVM_OPTS" ]; then
      	CQ_JVM_OPTS='-server -Xmx2048m -XX:MaxPermSize=256M -Djava.awt.headless=true -agentlib:jdwp=transport=dt_socket,address=30303,server=y,suspend=n'
      	...
      fi
    • Start AEM instance using start.sh on Mac OS.
    The server is now running in debug mode.

    Step 2: Debugging in IDE- IntelliJ

    Open Project in IntelliJ Go to 

    Run > Debug

    IntelliJ debug

    After that below pop-up will appear

    IntelliJ debug Edit Config

    Click Edit Configurations.

    The below screen will appear on clicking "Edit configurations".

    add new configuration

    Click "+" to add a new configuration. Select "Remote"

    Remote config to add debug in intelliJ

    The below screen will appear on selecting Remote.

    Enter below configurations:

    debug configurations

    Name: You can add any name. I want to debug on my local AEM author instance so I used localhost-author.

    Host: IP address on which AEM instance is running. If it is running on the local machine then use "localhost".

    Port: Enter port number on which you have configured debug address. In my case it is "
    30303". You can refer #1 to check your debug port. I used below command to set up debug address:

    if not defined CQ_JVM_OPTS set CQ_JVM_OPTS=-Xmx1024m -XX:MaxPermSize=256M -Djava.awt.headless=true -debug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=30303

    Note: Command-line arguments automatically changed.


    Module: Select module to debug. You can select any module for debugging or directly your project name. I have selected "Core" as my java classes are in the core module.

    Click Apply and Debug.

    debug integrated


    Testing:

    Select the java file which you want to debug.
    Add a breakpoint to line
    from UI, Hit the respective URL.

    Remote Debug In IntelliJ

    You can see, your file is in debug mode.


    Congratulations!!! Your AEM instance is running in debug mode and can debug in IDE.


    ElasticSearch vs Apache SolrCloud

      Elasticsearch and Apache SolrCloud are both powerful, distributed search engines built on top of Apache Lucene. Both Elasticsearch and S...