Provider org.glassfish.json.JsonProviderImpl not found at javax.json.spi.JsonProvider.provider(JsonProvider.java:97)

Shaurya Manhar picture Shaurya Manhar · Oct 30, 2017 · Viewed 26.2k times · Source

How to run Java API for JSON Processing(JSR 374) in eclipse?

I am trying to parse JSON string to JsonParser(javax.json.stream.JsonParser). Also added javax.json-api-1.0.jar in the build path. At runtime, there is an exception. My code is

import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;
public class jsonnn {
public class jsonnn {
public static void main(String[] args) {
        // Parse back
        final String result = "{\"name\":\"Falco\",\"age\":3,\"bitable\":false}";
        final JsonParser parser = Json.createParser(new StringReader(result));
        String key = null;
        String value = null;
        while (parser.hasNext()) {
            final Event event = parser.next();
            switch (event) {
            case KEY_NAME:
                key = parser.getString();
                System.out.println(key);
                break;
            case VALUE_STRING:
                value = parser.getString();
                System.out.println(value);
                break;
            }
        }
        parser.close();
    }
}

And the exception is

Exception in thread "main" javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
at javax.json.spi.JsonProvider.provider(JsonProvider.java:97)
at javax.json.Json.createParser(Json.java:90)
at jsonnn.main(jsonnn.java:14)
Caused by: java.lang.ClassNotFoundException: org.glassfish.json.JsonProviderImpl
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javax.json.spi.JsonProvider.provider(JsonProvider.java:94)
... 2 more

Answer

Mike picture Mike · Oct 31, 2017

You're developing against an API and have no implementation for that API. You need to make sure you have an implementation of the JSON-P specification to actually run the code you're trying to use.

In the JSR 374 official website Getting Started guide, it shows that you need two Maven dependencies - one for the API and one for the reference implementation - before you can use JSON-P 1.1:

<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>1.1</version>
</dependency>

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1</version>
</dependency>

Since it looks like you're not using Maven, you will need to download the implementation JAR from Maven Central manually: https://repo1.maven.org/maven2/org/glassfish/javax.json/1.1/

Or just click this direct link to download the JAR: javax.json-1.1.jar