How to add / import / load JAR into Java project in Eclipse using Build Path


Importing JAR / Modules in Java is a little bit different with Python / PHP / Ruby or another programming. In Java, IDE will handle all we need, for instance Eclipse. When we have custom modules like Json Simple Parser and need to add into our Java Project, then we should follow the right path to do it.

Build Path is the main keyword that will explain to us about importing modules in Java. When create Java project and need to import external modules, we need to using Build Path.

For instance :

1. Open Eclipse and create new Java Project called “json”.

2. json.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class json {
    public static void main(String[] args) {

          System.out.println("=======decode=======");
         
          String s="[0,{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}]";
          Object obj=JSONValue.parse(s);
          JSONArray array=(JSONArray)obj;
          System.out.println("======the 2nd element of array======");
          System.out.println(array.get(1));
          System.out.println();
                       
          JSONObject obj2=(JSONObject)array.get(1);
          System.out.println("======field "1"==========");
          System.out.println(obj2.get("1"));    

                       
          s="{}";
          obj=JSONValue.parse(s);
          System.out.println(obj);
                       
          s="[5,]";
          obj=JSONValue.parse(s);
          System.out.println(obj);
                       
          s="[5,,2]";
          obj=JSONValue.parse(s);
          System.out.println(obj);
    }
}

This will show several error un-resolveable variable. Then we need to donwload Json Simple Parser and place into ROOT PROJECT PATH, eg:

1
/PROJECT_ROOT/json_simple-1.1.jar

3. Adding JAR into Build Path in Eclipse
Right click on json_simple-1.1.jar and choose “Build Path” -> “Add to Build Path”.

4. Now, “CTRL-Space” on JSONArray, JSONValue and you will see import options from json simple parser.

Simple! 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.