JsonPath NoClassDefFoundError, or an alternative to JsonPath in Java

36,491

Solution 1

I think you have to add these dependencies to your project: https://code.google.com/p/json-path/downloads/detail?name=json-path-0.8.0-dependencies.zip&can=2&q=

In particular json-smart-1.1.jar where the missing Exception class is contained.

Solution 2

If You are using Maven or Gradle, just need to add this to your dependencies list:

For Maven projects:

In your Maven pom.xml file :

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.2.0</version>
    <scope>test</scope>
</dependency>

For Gradle projects:

In the dependencies section in your Gradle build.gradle file

    testCompile 'com.jayway.jsonpath:json-path:2.2.0'

It should work

Solution 3

When there are conflicting versions of json-smart, if the older version is picked which does not have the expected class.

How to fix

Verify the versions with mvn dependency:tree.

Below, there's net.minidev:json-smart:jar:1.1.1:compile and com.jayway.jsonpath:json-path:jar:1.1.0:test which uses latest version of json-smart.

$ mvn clean dependency:tree | grep json
[INFO] +- net.logstash.log4j:jsonevent-layout:jar:1.7:compile
[INFO] |  +- net.minidev:json-smart:jar:1.1.1:compile
[INFO] |  +- io.spray:spray-json_2.11:jar:1.3.3:compile
[INFO] |  +- org.json:json:jar:20160810:compile
[INFO] +- com.github.fge:json-schema-validator:jar:2.2.6:compile
[INFO] |  +- com.github.fge:json-schema-core:jar:1.2.5:compile
[INFO] |  |  +- org.skyscreamer:jsonassert:jar:1.4.0:test
[INFO] |  |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] +- com.jayway.jsonpath:json-path:jar:1.1.0:test
[INFO] |  |  |  |  +- io.gatling:jsonpath_2.10:jar:0.6.1:test
[INFO] |  |  |  |  +- io.fastjson:boon:jar:0.28:test

So, exclude the older version of json-smart from dependency as json-path has latest version of json-smart.

eg.

    <dependency>
        <groupId>net.logstash.log4j</groupId>
        <artifactId>jsonevent-layout</artifactId>
        <version>1.7</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>net.minidev</groupId>
                <artifactId>json-smart</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
Share:
36,491
firechant
Author by

firechant

Updated on May 31, 2020

Comments

  • firechant
    firechant almost 4 years

    For reasons related to the project I'm working on, I'd like an entire query to a JSON file to be held as a string, for example, $.store.book[*].title (rather than having to store each level of the document temporarily as a separate object).

    I'm currently using JsonPath (version 0.8.0, which was the latest I could find), which is basically exactly what I'm looking for, but I'm getting the exception shown below. I'm just using the sample JSON given on the JsonPath google code page, using one of their sample queries.

    What am I doing wrong here? Alternatively, if there's not a solution, are there alternatives to JsonPath in Java? I want to be able to pass an entire query as a string, and it must be in Java.

    The function:

    public void testJsonPath() throws Exception
    {
        String query = "$.store.book[*].title";
        List toRet = (List) JsonPath.read(practiceJson, query, new Filter[0]);
        System.out.println(toRet.toString());
    }
    

    The exception:

    java.lang.NoClassDefFoundError: net/minidev/json/parser/ParseException
    at com.jayway.jsonpath.spi.JsonProviderFactory$1.create(JsonProviderFactory.java:27)
    at com.jayway.jsonpath.spi.JsonProviderFactory.createProvider(JsonProviderFactory.java:32)
    at com.jayway.jsonpath.JsonPath.read(JsonPath.java:202)
    at com.jayway.jsonpath.JsonPath.read(JsonPath.java:307)
    at net.windward.datasource.test.TestJsonDataSource.testJsonPath(TestJsonDataSource.java:119)
    

    The practice JSON:

    private String practiceJson = "{\n" +
            "    \"store\": {\n" +
            "        \"book\": [ {\n" +
            "            \"category\": \"reference\",\n" +
            "            \"author\": \"Nigel Rees\",\n" +
            "            \"title\": \"Sayings of the Century\",\n" +
            "            \"price\": 8.95\n" +
            "        }, {\n" +
            "            \"category\": \"fiction\",\n" +
            "            \"author\": \"Evelyn Waugh\",\n" +
            "            \"title\": \"Sword of Honour\",\n" +
            "            \"price\": 12.99\n" +
            "        }, {\n" +
            "            \"category\": \"fiction\",\n" +
            "            \"author\": \"Herman Melville\",\n" +
            "            \"title\": \"Moby Dick\",\n" +
            "            \"isbn\": \"0-553-21311-3\",\n" +
            "            \"price\": 8.99\n" +
            "        }, {\n" +
            "            \"category\": \"fiction\",\n" +
            "            \"author\": \"J. R. R. Tolkien\",\n" +
            "            \"title\": \"The Lord of the Rings\",\n" +
            "            \"isbn\": \"0-395-19395-8\",\n" +
            "            \"price\": 22.99\n" +
            "        } ],\n" +
            "        \"bicycle\": [ {\n" +
            "            \"color\": \"red\",\n" +
            "            \"price\": 19.95,\n" +
            "            \"style\": [ \"city\", \"hybrid\" ]\n" +
            "        }, {\n" +
            "            \"color\": \"blue\",\n" +
            "            \"price\": 59.91,\n" +
            "            \"style\": [ \"downhill\", \"freeride\" ]\n" +
            "        } ]\n" +
            "    }\n" +
            "}";
    
  • firechant
    firechant almost 11 years
    Aha, that was it! Thank you very much!
  • Ravikumar11
    Ravikumar11 about 9 years
    Thank you, It is working for me after adding all dependencies.
  • Darko Romanov
    Darko Romanov over 6 years
    I don't know why it doesn't work to me. If I inspect the dependencies I can see jsonPath correctly added to tests but I always have that Exception.
  • papigee
    papigee almost 5 years
    Refreshing your project and then doing gradle refresh (on Eclipse, Right click on Project > Gradle > Refresh Gradle Project) should do it.
  • kiltek
    kiltek about 4 years
    I can't make the static import work of jsonPath(...). It can't find the package "matchers". I am using gradle.