Wednesday, December 31, 2014

Including Java agent in standalone Spring Boot application

Recently at we've decided to move majority of our stuff to simple containers. It was pretty easy due to use of Spring Boot uber-jars, but the problem was in NewRelic agents which should have to be included separately. That caused uncomfortable situation so we decided to solve it by including NewRelic agent into our uber-jar applications. If you also want to simplify your life please follow provided instructions :)

At first we have to add proper dependency into our pom.xml descriptor:

Now since we have proper jar included into our project it's time to unpack the dependency to have all necessary classes in our application jar file:

After this step we've all agent related classes accessible directly from our jar. But still the file cannot be used as an agent jar. There are some important manifest entries that have to be present in every agent jar. The most important is the Premain-Class attribute specifying main agent class including premain() method. In case of NewRelic it's also important to include Can-Redefine-Classes and Can-Retransform-Classes attributes. The easiest way to do that is to extend maven-jar-plugin configuration:


Now is coming the tricky part :) NewRelic agent also contains class with main() method which causes that Spring Boot repackager plugin is unable to find single main() method so build fails. It's not a problem but we have to remember to specify proper main class in spring-boot-maven-plugin (or in gradle plugin):

That's all! You can execute your application with following command:

java -javaagent:myapp.jar -jar myapp.jar

Last but not least: don't forget to include NewRelic configuration file (newrelic.yml) in the same directory as your application jar. The other solution is to set newrelic.config.file system property to point the fully qualified file name.

You can find sample project with proper configuration on my GitHub:


Diovane said...


I was reading your article, but could not add the New Relic Agent in my app with spring-boot, you have some sample app using the new relic agent with spring-boot.


Jakub Kubrynski said...


what exactly is causing the problem? Do you have NewRelic's classes included in your jar? Does the manifest contain proper statements?


Kevykev003 said...


thanks for the tutorial. I've added the dependency and plugin entries. but im not sure where the section should be placed in my pom.xml file. Also, im running the spring boot project using the embedded tomcat, where should the newrelic.jar and .yml file be located?

thanks in advance.


Kevykev003 said...

sorry... Im not sure where the configuration section should be placed in my pom.xml file

Jakub Kubrynski said...

@Kevykev003 - dependency is places in dependencies element, and plugin entries should be added into build -> plugins. You don't have to add newrelic.jar - it'll be included into fat-jar.

Jesus David Martínez Sanchez said...

great article, something to add

java -javaagent:newrelic.jar -jar myapp.jar

in the same folder of the myapp.jar I added newrelic.jar and works!!!!

kobayashi-maru said...

Hi Jakub,

Interesting article, but I'm not sure what advantage you get out of bundling the agent like that. As long as you throw the newrelic.jar and the YAML file in the same directory as the fat jar, everything pretty much just works...

Jakub Kubrynski said...


The only advantage is that including new-relic agent dependency is a part of your build process, and there are no external files (except newrelic.yml) in the deployment process.


kobayashi-maru said...

Good to know. Thanks!

Prashant Shah said...

Thanks Jakub, I can run spring boot application and able to see performance metrics on New Relic dashboard. But now I want to put this application on cloud environment. Could you please let me know how and where I will put newrelic.yml file which is containing license key. I dont have any clue for cloud.

Jakub Kubrynski said...

In cloud you have to use some provisioning tool like Ansible or Puppet. That's right place for newrelic.yml IMHO

Cherry li said...
This comment has been removed by the author.