Including Java agent in standalone Spring Boot application
Recently at DevSKiller.com 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:
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: https://github.com/jkubrynski/spring-boot-new-relic-agent
At first we have to add proper dependency into our pom.xml descriptor:
<dependency> <groupiId>com.newrelic.agent.java<<groupId> <artifactId>newrelic-agent</artifactId> <version>3.12.1</version> <scope>provided</scope> </dependency>
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:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.9</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>unpack-dependencies</goal> </goals> <configuration> <includeArtifactIds>newrelic-agent</includeArtifactIds> <outputDirectory>${project.build.outputDirectory}</outputDirectory> </configuration> </execution> </executions> </plugin>
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:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.5</version> <configuration> <archive> <manifestEntries> <Premain-Class>com.newrelic.bootstrap.BootstrapAgent</Premain-Class> <Can-Redefine-Classes>true</Can-Redefine-Classes> <Can-Retransform-Classes>true</Can-Retransform-Classes> </manifestEntries> </archive> </configuration> </plugin>
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):
<configuration> <mainClass>my.custom.Application</mainClass> </configuration>
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: https://github.com/jkubrynski/spring-boot-new-relic-agent
Comments
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.
thanks.
what exactly is causing the problem? Do you have NewRelic's classes included in your jar? Does the manifest contain proper statements?
Cheers
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.
Kevin
java -javaagent:newrelic.jar -jar myapp.jar
in the same folder of the myapp.jar I added newrelic.jar and works!!!!
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...
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.
Cheers
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.
Hello. It doesn't work.
java -javaagent:target/herokuapp-1.0.0.jar -jar target/herokuapp-1.0.0.jar
Exception in thread "main" java.lang.ClassNotFoundException: com.newrelic.bootstrap.BootstrapAgent
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
FATAL ERROR in native method: processing of -javaagent failed
Aborted (core dumped)
-----------------------------------------------------------------------
Exception in thread "main" java.lang.ClassNotFoundException: com.newrelic.bootstrap.BootstrapAgent
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
FATAL ERROR in native method: processing of -javaagent failed
Aborted (core dumped)
-----------------------------------------------------------------------
Any suggestions? Thanks!
Failed to find Premain-Class manifest attribute in granter-web/target/GRANTER.war
ClassNotFoundException: com.newrelic.bootstrap.BootstrapAgent
And it’s working fine No problem.
My request is, I dent want to run this command as I start my project by starting main method. So I want to configure my NR agent with maven itself so later on when I will start my server with spring boot it will automatically send the statistics on NR server.
Thanks in advance.
I have a simple spring boot hello-world application. There is nothing much in it.
I did all the steps and even copied the pom file from the given github link, still the new relic GUI is not showing my application.
What might be the reason? Please help. Really stuck here.
Good article, my app running well as i follow each step in your article.
How can build my application without using -javaagent in command line argument with spring boot.
This solution is not valid any more: https://github.com/spring-projects/spring-boot/issues/6626
Corporate TRaining Spring Framework
Project Centers in Chennai For CSE
Spring Training in Chennai
vé máy bay đi Mỹ giá rẻ 2021
giá vé máy bay tết 2021
đặt vé máy bay đi toronto canada
săn vé máy bay đi Pháp
giá vé máy bay sang Anh quốc
giá vé máy bay Vietjet
combo hà nội đà nẵng
combo nha trang 3 ngày 2 đêm 2021
visa trung quốc giá rẻ tphcm
cách ly khách sạn
TOGAF Training In Bangalore | TOGAF Online Training
Oracle Cloud Training In Bangalore | Oracle Cloud Online Training
Power BI Training In Bangalore | Power BI Online Training
Alteryx Training In Bangalore | Alteryx Online Training
API Training In Bangalore | API Online Training
Ruby on Rails Training In Bangalore | Ruby on Rails Online Training
Tally Course in Chennai
CCNA Course in Chennai
SEO Training in Chennai
Hadoop Training in Chennai
Cloud Computing Training in Chennai
Blue Prism Training in Chennai
JAVA Course in Chennai
Selenium Course in Chennai
Python course in Chennai
AWS Course in Chennai
Data Science Training in Chennai
DevOps certification in Chennai
Exception in thread "main" java.lang.ClassNotFoundException: datadog.trace.bootstrap.AgentBootstrap
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:431)
at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:525)
FATAL ERROR in native method: processing of -javaagent failed
But the manifest has the premain and main class properly and also the datadog classes are packages under BOOT-INF/classes/** path.
Manifest
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.1
Built-By: makes
Build-Jdk: 11.0.10
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: com.user.info
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Premain-Class: datadog.trace.bootstrap.AgentBootstrap
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.user.info.userInfoApplication
Spring-Boot-Version: 2.3.4.RELEASE
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
after that i have executed below cmd but i am getting below error. how to resolve this issue.
java -javaagent:newrelic.jar -jar myapp.jar
Error: Unable to access jarfile myapp.jar
java -javaagent:newrelic.jar -jar myapp.jar