18

When I try to open my jsp through Tomcat, I receive the following messages:

The type org.json.simple.JSONObject cannot be resolved. It is indirectly referenced from required .class files.

The method getJSONObject() from the type Ejercicio refers to the missing type JSONObject.

I have a java class that goes like this:

    package E;
    import org.json.simple.*;
    import javax.json.*;
    import org.json.*;

    public class Ejercicio {
        public int a;
        public String b;

    public Ejercicio (int a, String b) {
        this.a=a;
        this.b=b;
        }

    public JSONObject getJSONObject() {
        JSONObject obj = new JSONObject();
        obj.put("a", a);
        obj.put("b", b);
        return obj;
        }
    }

My jsp goes as follows:

    <%@page import="java.io.*" %>
    <%@page import="java.util.*"%>
    <%@page import="E.Ejercicio"%>
    <%@page import="javax.json.*"%>
    <%@page import="org.json.simple.*"%>
    <%@page import="org.json.*"%>
    <%@page import="net.sf.json.*" %>

    <%  
    ArrayList<Ejercicio> miArray = new ArrayList<Ejercicio>();
    miArray.add(new Ejercicio (1,"Hola"));
    miArray.add(new Ejercicio (2,"Caracola"));
    miArray.add(new Ejercicio (3,"Perola"));
    for (Ejercicio temp:miArray) {
        out.println("<p>"+temp.b+"</p>");
        }

    JSONArray jsonArray = new JSONArray();
    for (int i=0; i < miArray.size(); i++) {
        jsonArray.put(miArray.get(i).getJSONObject());
        }
    %>

I have added a lot of jars to both the lib folder inside WEB-INF and the Tomcat lib trying to solve the problem with no results (commons-beanutils-1.8.1, commons-collections-3.2.1, commons-lang-2.5, commons-logging-1.1.1, ezmorph-1.0.6, java-json, javax.json-1.0, json-lib-2.4-jdk15, json-rpc-1.0, json-simple-1.1.1-sources, org.json).

When I compiled the java class I got the "Some input files use unchecked or unsafe operations" message. Could this be related to the jps not compiling?

8 Answers 8

23

You are missing json-simple-1.1.1.jar from your classpath.

if you are using Maven, add below into your pom.xml.

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Or you can download it from here.

Sign up to request clarification or add additional context in comments.

1 Comment

What do you mean when you say "from your classpath"? I have added json-simple-1.1.1in my project lib and compiled the class again with the following sentence: javac -cp .;"C:\Program Files\apache-tomcat-8.5.13\webapps\Heroismo\WEB-INF\lib\*" Ejercicio.java, but I get the same error when trying to open the jsp. I don't know Maven.
13

Firstly, you don't need the json-simple.jar in your project.
Therefore remove the import org.json.simple.*

Secondly, the best practice is to import only the required classes from your jar.Therefore you can replace import org.json.*; by

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;

Comments

7

for me definetely downloded the jar and add it to eclipse simple

just follow these steps 1. download the .jar file at this link https://jar-download.com/artifacts/org.json 2. locate and extract the json.jar file

in eclipse -right click on your project and go to build path -add external jar -hit apply

that's enter image description hereit ; hope it helps guys

1 Comment

Additionally: Sometimes the libraries are disabled in the Order and Export tab so the project won't pick them up even if they were added.
3

I was facing the same issue. I tried adding jar file in the classpath but it was not working for me. If you are using Java gradle project than you can try adding these lines in build.gradle :

implementation group: 'org.json', name: 'json', version: '20220320'
    
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

If you are using Maven, add below into your pom.xml:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20220320</version>
</dependency>
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Comments

1

Is some version of json-simple.jar in your project libs ?

1 Comment

Yes, it is. Before reading your comment I just had a jar called json-simple-1.1.1-sources. Now I have added json-simple-1.1.1, but I get the same error when trying to open the jsp.
1

You can download the requisite jar, for me what worked is the downloading of my missing json20140107 jar and adding the import statements as below:

import org.json.JSONException;

import org.json.simple.JSONObject;

Comments

0

I didn't see the right answer anywhere but my TomCat JSON library problem was with the artifact deployment. If you're using intelliJ, go to project structure >> artifacts and add the jar to your lib folder

Check out my project structure

enter image description here

Comments

0

This works for the org.json package:

Copy the source files from here

https://github.com/stleary/JSON-java

Note: this is the source code link (JSON-java) taken from from http://www.json.org/

into \src\org\json folder

In Eclipse, click the Open perspective button, then Resource. enter image description here

Right click on org.json and then hit Refresh to add the java source code into the project.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.