How can you start AEM in debug mode?

Starting AEM in debug mode allows you to debug Java code, inspect logs, and analyze AEM's internal processes. This can be especially useful for troubleshooting issues and examining the behavior of components or services.

Here’s how to start AEM in debug mode :

1. Using the Command Line (Java Debugging) :

To run AEM in debug mode via the command line:

  1. Navigate to your AEM installation directory:

    • Go to the folder where AEM is installed, usually located in aem/author or aem/dispatcher.
  2. Edit the start script:

    • On Windows, open the start.bat file.
    • On Linux/macOS, open the start.sh file.
  3. Add Debugging Parameters:
    Find the section where the AEM JVM is started, and add the following JVM options for remote debugging:

    • For Windows (start.bat):

      set AEM_JVM_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
      
    • For Linux/macOS (start.sh):

      export AEM_JVM_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"
      
      • -Xdebug: Enables debugging.
      • -Xrunjdwp:transport=dt_socket: Use the socket transport for remote debugging.
      • server=y: AEM will listen for debugger connections.
      • suspend=n: AEM will not wait for the debugger to attach before starting.
      • address=8000: The debugger port AEM listens on (you can change it if needed).
  4. Restart AEM:
    Run the script (start.bat or start.sh) to start AEM in debug mode. This will start AEM with remote debugging enabled on port 8000.


2. Connecting to AEM with an IDE (e.g., IntelliJ, Eclipse) :

Once AEM is running in debug mode, you can attach your IDE debugger to the process:

For IntelliJ IDEA :
  1. Open IntelliJ IDEA.
  2. Go to Run → Edit Configurations.
  3. Click the "+" icon to add a new Remote Debug Configuration.
  4. Set the Host to localhost and Port to 8000 (or the port you used).
  5. Click OK.
  6. Click the Debug icon to start debugging and connect to the AEM instance.
For Eclipse :
  1. Open Eclipse.
  2. Go to Run → Debug Configurations.
  3. Right-click on Remote Java Application and select New.
  4. Set the Host to localhost and Port to 8000.
  5. Click Apply, then click Debug.

Now, you can set breakpoints in your Java code, step through it, and inspect variables!


3. Additional Debugging Tips
  • Access the AEM Logs:
    You can find the logs in the crx-quickstart/logs/ directory. Look for the error.log or aem-error.log for issues related to Java exceptions.

  • Enable More Verbose Logging:
    For additional logging output, you can modify the log configuration file (logback.xml) located in the crx-quickstart directory to increase the verbosity.


Summary: Steps to Start AEM in Debug Mode :
  1. Edit start.bat or start.sh to include the Java debug options.
  2. Restart AEM to launch it with debugging enabled on port 8000.
  3. Connect your IDE (IntelliJ, Eclipse) to localhost:8000 to start debugging.