dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

[Bug] When running a Dubbo 3.3.6 application on JDK 25, application startup fails with `NoClassDefFoundError: com/google/gson/JsonSyntaxException`.

Open CrazyLionCat opened this issue 6 months ago • 2 comments

Pre-check

  • [x] I am sure that all the content I provide is in English.

Search before asking

  • [x] I had searched in the issues and found no similar issues.

Apache Dubbo Component

Java SDK (apache/dubbo)

Dubbo Version

When running a Dubbo 3.3.6 application on JDK 25, application startup fails with NoClassDefFoundError: com/google/gson/JsonSyntaxException.

The application does not depend on Gson and does not intend to use Gson as Dubbo's JSON implementation. Dubbo's default JSON implementation should be fastjson2, and the same application starts normally on JDK 21.

Steps to reproduce this issue

  Caused by: java.lang.NoClassDefFoundError: com/google/gson/JsonSyntaxException
      at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
      at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:2985)
      at java.base/java.lang.Class.getConstructor0(Class.java:3180)
      at java.base/java.lang.Class.getConstructor(Class.java:2199)
      at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:623)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1111)
      at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1142)
      at org.apache.dubbo.common.utils.JsonUtils.loadExtensions(JsonUtils.java:81)
      at org.apache.dubbo.common.utils.JsonUtils.createJsonUtil(JsonUtils.java:52)
      at org.apache.dubbo.common.utils.JsonUtils.getJson(JsonUtils.java:40)
      at org.apache.dubbo.common.utils.JsonUtils.toJavaObject(JsonUtils.java:106)
      at org.apache.dubbo.registry.client.metadata.store.MetaCacheManager.toValueType(MetaCacheManager.java:73)

Expected behavior

Dubbo should not fail startup because the optional Gson dependency is absent, especially when Gson is not selected as the JSON implementation.

If fastjson2 is the default/preferred JSON implementation, Dubbo should be able to select it without forcing ServiceLoader to instantiate or inspect the Gson implementation class.

Actual behavior

On JDK 25, ServiceLoader appears to inspect the Gson JSON implementation during Dubbo JSON SPI loading. Since Gson is not on the classpath, this causes startup failure before Dubbo can use fastjson2.

Workaround

Setting the following system property avoids the issue:

System.setProperty("dubbo.json-framework.prefer", "fastjson2");

Environment

  • Dubbo version: 3.3.6
  • JDK version: 25
  • Works on: JDK 21
  • Registry: Nacos
  • Gson dependency: not present

What you expected to happen

Intercept exceptions, skip processing

Anything else

No response

Do you have a (mini) reproduction demo?

  • [ ] Yes, I have a minimal reproduction demo to help resolve this issue more effectively!

Are you willing to submit a pull request to fix on your own?

  • [ ] Yes I am willing to submit a pull request on my own!

Code of Conduct

CrazyLionCat avatar May 28 '26 08:05 CrazyLionCat

I would like to work on this issue.

shangxinxiaolin avatar Jun 02 '26 10:06 shangxinxiaolin

I verified that this issue exists in Dubbo 3.3.6, but it has already been fixed on the current 3.3 branch.

The fix was introduced by commit apache/dubbo@3720471e56717b7e3494a7352aa437ef95507fe1, from PR #15754.

The relevant change is in JsonUtils.loadExtensions(): ServiceLoader.iterator().hasNext() is now inside the try/catch, so optional JSON providers with missing dependencies can be skipped.

Iterator<JsonUtil> it = loader.iterator();
// In JDK 21+, ServiceLoader.hasNext() may throw NoClassDefFoundError
// when checking class dependencies, so we need to catch it here
while (true) {
    try {
        if (!it.hasNext()) {
            break;
        }
        JsonUtil extension = it.next();
        if (extension.isSupport()) {
            if (name != null && name.equals(extension.getName())) {
                return extension;
            }
            extensions.put(extension.getName(), extension);
        }
    } catch (Throwable ignored) {
        // Ignore loading failures (e.g., NoClassDefFoundError in JDK 25)
        // and continue with the next extension
    }
}

I also verified locally that current 3.3 no longer reproduces the issue without Gson on the classpath.

SavitarC avatar Jun 02 '26 15:06 SavitarC

如果你只是想让本地能够测试而不是生产上升级到java25的话有个办法可以绕过这个异常让你继续执行下去

Image 通过修改实例化的顺序再加上本身代码就有短路的逻辑,让其跳过后面的初始化 Image Image Image 我本地是用的Jackson的实现所以我做了2件事让程序继续走下去了 1. -Ddubbo.json-framework.prefer=jackson 2. Image Image

hato233 avatar Aug 24 '26 17:08 hato233

I'd like to take this one ([Bug] When running a Dubbo 3.3.6 application on JDK 25, ap). I'll look into the root cause and follow up with a PR. (claiming via @LeonxLJX)

LeonxLJX avatar Sep 01 '26 10:09 LeonxLJX

I'd like to take this one ([Bug] When running a Dubbo 3.3.6 application on JDK 25, ap). I'll look into the root cause and follow up with a PR. (claiming via @LeonxLJX)

LeonxLJX avatar Sep 01 '26 11:09 LeonxLJX

I'd like to take this one ([Bug] When running a Dubbo 3.3.6 application on JDK 25, ap). I'll dig into the root cause and follow up with a PR shortly. (claiming via @LeonxLJX)

LeonxLJX avatar Sep 01 '26 11:09 LeonxLJX