user5047085
user5047085

Reputation:

trying to read json file at url

I have this java file:

package foo;

import java.net.*;
import java.io.*;
import org.json.simple.*;


class JsonObject {
  String foo;
}

public class Bar {

    public static void main(String[] args){

        URL url = new URL("https://raw.githubusercontent.com/oresoftware/oredoc/master/test/builds/java/one/test.json");
        try (InputStream is = url.openStream();
        JsonReader rdr = Json.createReader(is)) {

                 JsonObject obj = rdr.readObject();
                 JsonArray results = obj.getJsonArray("data");
                 for (JsonObject result : results.getValuesAs(JsonObject.class)) {
                       System.out.print(result.getJsonObject("from").getString("name"));
                       System.out.println(result.getString("message", ""));

                     }
             }

    }
}

I borrowed the code from here: https://www.oracle.com/technetwork/articles/java/json-1973242.html

I am simply trying to experiment with reading a json array, from the url given (the url is live and works, just put in a browser and see).

I believe the jar file for org.json.simple.* is located here:

I am trying to build without maven, it was working fine until I tried to import the org.json.simple jar:

#!/usr/bin/env bash

dir="$(dirname ${BASH_SOURCE})";
export CLASSPATH="${CLASSPATH}:$dir/foo/java-json.jar"

javac "$dir/foo/"*.java

jar -c -m "$dir/foo/manifest.mf" -f foo.jar "$dir/foo/"*.class

java -jar "$dir/foo.jar"

My only guess is that I am using the CLASSPATH env variable wrong, but I am getting this error:

./test/builds/java/one/foo/Bar.java:5: error: package org.json.simple does not exist
import org.json.simple.*;
^
./test/builds/java/one/foo/Bar.java:22: error: cannot find symbol
       JsonReader rdr = Json.createReader(is)) {
       ^
  symbol:   class JsonReader
  location: class Bar
./test/builds/java/one/foo/Bar.java:22: error: cannot find symbol
       JsonReader rdr = Json.createReader(is)) {
                        ^
  symbol:   variable Json
  location: class Bar
./test/builds/java/one/foo/Bar.java:25: error: cannot find symbol
                 JsonArray results = obj.getJsonArray("data");
                 ^
  symbol:   class JsonArray
  location: class Bar
./test/builds/java/one/foo/Bar.java:25: error: cannot find symbol
                 JsonArray results = obj.getJsonArray("data");
                                        ^
  symbol:   method getJsonArray(String)
  location: variable obj of type JsonObject
./test/builds/java/one/foo/Bar.java:27: error: cannot find symbol
                         System.out.print(result.getJsonObject("from").getString("name"));
                                                ^
  symbol:   method getJsonObject(String)
  location: variable result of type JsonObject
./test/builds/java/one/foo/Bar.java:29: error: cannot find symbol
                         System.out.println(result.getString("message", ""));
                                                  ^
  symbol:   method getString(String,String)
  location: variable result of type JsonObject
7 errors

the first line says that org.json.simple package does not exist, so had do I tell the java commands about that jar file? For all I know the CLASSPATH env var is correct!

Upvotes: 0

Views: 414

Answers (1)

XuanGang
XuanGang

Reputation: 496

As you've said, that problem is packages is not exist.

  1. Move org.json.simple packages to the compiled jar folder.
  2. run jar.

Upvotes: 0

Related Questions