Installing Airbrake in a Java Application.

Configuring Airbrake is a simple task whether you are a java Novice or Wizard. In our examples we show you how to configure it with Maven and without it. If you identify bugs or would like to suggest a feature please refer to Airbrake-java on Github.

Maven: Project management/workflow tool

  • If you want to bundle your dependencies into a WAR or EAR file, set the packaging type of your project to EAR or WAR.
  • If you want a JAR file that includes your code and dependencies "All-In-One", use the assembly plugin with the jar-with-dependencies descriptor.
  • If you want to pull your dependencies into the target directory interactively, use the dependency plugin to copy your files in.

log4j (v1): Feature rich logging utility

  • You have the ability to set logging parameters in the config file, rather than recompiling
  • You can configure the format that logs are written in (text, html, SMTP headers, etc..)
  • You can filter results by the severity:
    • DEBUG : low level detailed info such as cache hit/miss, DB connections
    • INFO : coarse info such as progress or state of current action
    • WARN : unexpected events but more than likely won't halt the application
    • ERROR : unexpected but serious event, unstable app state

Project 1: Maven with Airbrake and log4j

Config file pom.xml, packaging dependencies in one JAR

<project xmlns="" xmlns:xsi=""                                

Config file for (place anywhere in your projects Class path)

log4j.rootLogger=INFO, stdout, airbrake                                                                                                 

log4j.appender.stdout.layout.ConversionPattern=[%d,%p] [%c{1}.%M:%L] %m%n


Simple Java app with an exception:

import org.apache.log4j.*;
public class App{

  private static final Logger logger = Logger.getLogger(App.class);

   public static void main(String args[])
         int a[] = new int[2];
         System.out.println("Access element three :" + a[3]);
      catch(ArrayIndexOutOfBoundsException e)
         System.out.println("Exception thrown  :" + e); 
      System.out.println("Out of the block");

Build App:

#~> mvn clean compile assembly:single -e -X -l build.log

Project 2: Using Airbrake directly without Maven.

Ensure you add the Airbrake jar to your classpath:

Simple java app with an exception:

import airbrake.*;

public class App{

   public static void main(String args[])
         int a[] = new int[2];
         System.out.println("Access element four :" + a[4]);
      catch(ArrayIndexOutOfBoundsException e)
            AirbrakeNotice notice = new AirbrakeNoticeBuilder("YOUR_KEY_HERE", e, "env").newNotice();
            AirbrakeNotifier notifier = new AirbrakeNotifier();
         System.out.println("Exception thrown  :" + e);
      int a[] = new int[2];
      System.out.println("Out of the block");