Java Intellj: org.json.simple does not exist

6.8k views Asked by At

I know that questions have been asked before, but it does not solve my problem in my school project.

I downloaded the JSON.simple jar file from https://mkyong.com/java/json-simple-example-read-and-write-json/, then move it to the src directory and import it.

In the src folder I also have Main.java, Manager.java and Peer.java, in which Manager and Peer uses the json.simple package.

I am using Intellj.

At src directory, I try to run javac Main.java, I get 14 errors including

import org.json.simple.parser.JSONParser;
                             ^
.\Manager.java:95: error: cannot find symbol

Then I tried to provide the full path for the jar file: javac -cp C:\Users\yiges\Desktop\COMP90015\Project3\WhiteBoard\src\json-simple-1.1 Main.java

I get the 4 errors including:

imple-1.1 Main.java
Main.java:15: error: cannot find symbol
            Manager manager = new Manager(port);
            ^
  symbol:   class Manager
  location: class Main

Then I tried to compile all java files: javac -cp C:\Users\yiges\Desktop\COMP90015\Project3\WhiteBoard\src\json-s imple-1.1 *.java or simply javac *.java

I get again 14 errors including:

Manager.java:1: error: package org.json.simple does not exist
import org.json.simple.JSONObject;
                      ^

How do I solve this issue?

Thanks for helping!

2

There are 2 answers

0
Yige Song On

Problem solved.

It turns out that I should use the javac -cp .;json-simple-1.1.1.jar C:\Users\yiges\Desktop\COMP90015\Project3\WhiteBoard\src\Main.java command to compile the code

1
Jonad García San Martín On

if you create the project with Maven you are going to avoid all the import and compile problems and you will focus only on code. If you are new to Maven, it´s simpler than you can think. To create the proyect see this short video

Working with Maven in IntelliJ IDEA

Then you can easily import JSON.simple library adding the following dependency to pom.xml file, inside tags:

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

In real projects you are going to use Maven or Gradle, so the soon you learn that is better. If continue with errors even with Maven or you need to solve this without Maven, post a reproducer or even your project.