json-path/JsonPath · error · JsonPathException

Gson not found on path

Error message

Gson not found on path

What it means

GsonMappingProvider's constructor uses a Callable that loads Gson via Class.forName-style instantiation; if com.google.gson.Gson is not on the classpath it logs "Gson not found on class path" and rethrows JsonPathException. The provider cannot work at all without the Gson dependency, so initialization fails eagerly.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/mapper/GsonMappingProvider.java:59

    }

    public GsonMappingProvider(Callable<Gson> factory) {
        this.factory = factory;
    }

    public GsonMappingProvider() {
        super();
        try {
            Class.forName("com.google.gson.Gson");
            this.factory = new Callable<Gson>() {
                @Override
                public Gson call() {
                    return new Gson();
                }
            };
        } catch (ClassNotFoundException e) {
            logger.error("Gson not found on class path. No converters configured.");
            throw new JsonPathException("Gson not found on path", e);
        }
    }

    @Override
    public <T> T map(Object source, Class<T> targetType, Configuration configuration) {
        if(source == null){
            return null;
        }
        try {
            return factory.call().getAdapter(targetType).fromJsonTree((JsonElement) source);
        } catch (Exception e){
            throw new MappingException(e);
        }
    }

    @Override
    public <T> T map(Object source, TypeRef<T> targetType, Configuration configuration) {
        if(source == null){

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Add the Gson dependency, e.g. Maven: <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></dependency> or Gradle: implementation 'com.google.code.gson:gson:2.10.1'
  2. Verify the jar is actually packaged (mvn dependency:tree / check the fat jar contents)
  3. If Gson is not wanted, use a different MappingProvider (JacksonMappingProvider, JsonSmartMappingProvider) instead
  4. Check for version exclusions or provided-scope on gson in your build

Example fix

// before (build.gradle)
dependencies { implementation 'com.jayway.jsonpath:json-path:2.9.0' }
// after
dependencies {
    implementation 'com.jayway.jsonpath:json-path:2.9.0'
    implementation 'com.google.code.gson:gson:2.10.1'
}
Defensive patterns

Strategy: validation

Validate before calling

try {
    Class.forName("com.google.gson.Gson");
} catch (ClassNotFoundException e) {
    throw new IllegalStateException("Add com.google.code.gson:gson to the classpath before using GsonMappingProvider");
}

Try / catch

try {
    Configuration conf = Configuration.builder().mappingProvider(new GsonMappingProvider()).build();
} catch (JsonPathException e) {
    // Gson missing — fall back to Jackson or JsonSmart mapping provider
    conf = Configuration.defaultConfiguration();
}

Prevention

When it happens

Trigger: Instantiating GsonMappingProvider (directly or by registering it as the MappingProvider in Configuration) in a project whose build does not include com.google.code.gson:gson.

Common situations: Adding gson support config copied from docs without adding the dependency; shading/excluding gson in a fat jar; runtime environments (app servers, Android) that strip optional deps; switching from JacksonSmart/Jackson provider to Gson provider.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/aa5f7fa1a5f984f0. Report an issue: GitHub.