1

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!

Yige Song
  • 333
  • 6
  • 16
  • Does this answer your question? [javac not recognizing external libraries](https://stackoverflow.com/questions/56014926/javac-not-recognizing-external-libraries) – Tom May 10 '21 at 04:27
  • When you use an external library, then you need to specify the path to the classes in the classpath, not just your local files. – Tom May 10 '21 at 04:30
  • @Tom I already add the directory ```src``` (contains the jar file) to the Path variable, but still, nothing changed. Would you like to tell me specifically what I should do, or what command I should use in my case? Thanks for helping! – Yige Song May 10 '21 at 04:42
  • Well then read the linked question please. Adding just the directory doesn't include jar files. – Tom May 10 '21 at 09:50

2 Answers2

4

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.

  • Thank you! This time we are not supposed to use Maven, but it seems powerful. Will give it a try next time – Yige Song May 10 '21 at 13:59
1

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

Yige Song
  • 333
  • 6
  • 16