You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java 17集成New Relic Agent遇java.sql.SQLException类未找到错误

问题:Docker部署New Relic Agent时出现NoClassDefFoundError(java.sql.SQLException)

问题详情

本地运行时,使用以下版本的New Relic Agent可正常采集指标:

<groupId>com.newrelic.agent.java</groupId>
<artifactId>newrelic-agent</artifactId>
<version>8.5.0</version>

通过命令 curl -O https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip 下载newrelic.jar后,部署到基于linux/x86_64 openjdk:17-jdk-buster镜像的Docker容器中时,启动报错,异常信息如下:

Unable to start New Relic Agent. Please remove -javaagent from your startup arguments and contact New Relic support.
java.lang.NoClassDefFoundError: java/sql/SQLException
    at com.newrelic.agent.database.ParsedDatabaseStatement.<init>(ParsedDatabaseStatement.java:26)
    at com.newrelic.agent.database.SelectVariableStatementFactory.<init>(SelectVariableStatementFactory.java:19)
    at com.newrelic.agent.database.DefaultDatabaseStatementParser.<init>(DefaultDatabaseStatementParser.java:55)
    at com.newrelic.agent.database.DatabaseService.<init>(DatabaseService.java:40)
    at com.newrelic.agent.service.ServiceManagerImpl.doStart(ServiceManagerImpl.java:260)
    at com.newrelic.agent.service.AbstractService.start(AbstractService.java:63)
    at com.newrelic.agent.Agent.continuePremain(Agent.java:173)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at com.newrelic.bootstrap.BootstrapAgent.startAgent(BootstrapAgent.java:175)
    at com.newrelic.bootstrap.BootstrapAgent.premain(BootstrapAgent.java:117)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:491)
    at java.instrument/sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:503)
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 19 more

已尝试的操作:

  • 更换不同版本的New Relic Agent
  • 修改mysql-connector依赖版本:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>

但问题仍未解决,寻求可行解决方案。

解决方案

1. 检查JDK镜像模块完整性

openjdk:17-jdk-buster可能是裁剪版JDK,缺失java.sql模块:

  • 更换为完整OpenJDK镜像,比如openjdk:17-jdk-full
  • 若必须使用当前镜像,在启动命令中添加--add-modules java.sql参数,强制JVM加载该模块

2. 匹配Agent与JDK版本兼容性

New Relic Agent 8.5.0对JDK 17的支持可能存在局限:

  • 升级到New Relic Agent最新稳定版本(如9.x系列),新版本对高版本JDK兼容性更完善
  • 核对New Relic官方兼容矩阵,确认所用Agent版本支持JDK 17

3. 调整启动参数与依赖配置

  • 确保Docker启动命令中-javaagent指向的newrelic.jar路径正确,文件完整无损坏
  • 将mysql-connector-java依赖设置为provided或确保其被打包进应用依赖包,避免容器环境缺失JDBC类
  • 临时修改newrelic.yml配置,禁用数据库监控模块,验证Agent是否能正常启动,以此定位问题根源

4. 验证容器内类路径

进入Docker容器执行以下操作排查:

  • 执行java -cp <应用类路径>:newrelic.jar -version,检查是否能加载java.sql.SQLException类
  • 执行jar tf newrelic.jar,查看Agent包是否包含必要的数据库相关依赖类

内容的提问来源于stack exchange,提问作者Pujitha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 10:15:13