How to use code libraries from github?

14,566

Solution 1

When you have to attach external library in Java, generally you should have jar with compiled sources.

What we have in repository is source code, nothing more, so you can't just pack it and get compiled files (*.class).

So, your options here are at least so:

  1. Download compiled jar, provided by author and attach it to the project within Eclipse (How to import a jar in Eclipse)
  2. Use maven project, for which you can define dependency on some external project: http://mvnrepository.com/artifact/org.json/json/20140107
  3. Compile the project yourself (not recommended): in Frederic Henri's answer

Solution 2

not sure what you mean with the compilation of java file and "no avail" but you need to compile java files before you package them as jar :

 mkdir build
 javac -d build/ *.java

then package the build directory as jar

jar cf JSON-java.jar -C build/ .

then import the jar file in your library of the project - make sure to add it in your eclipse lib settings and you should be able to reference the org.json package

Share:
14,566
Shyam
Author by

Shyam

Updated on June 04, 2022

Comments

  • Shyam
    Shyam almost 2 years

    I need some tools to read JSON from a URL. However, when I tried to use the JSONObject class, it was unavailable. I looked it up and it was suggested that I should download the required library : https://github.com/douglascrockford/JSON-java

    I'm unfamiliar with downloading and creating a jar of Github repositories. I have downloaded the repository in zip fromat. ( I am using windows 8). I navigated to the location, extracted the files and tried to create a jar.

    I found half of the solution in : How to make this (github) source into a library/jar?

    Based on the Suggestion i used the command to make jar:

    git clone git://github.com/douglascrockford/JSON-java cd JSON-java

    jar cf JSON-java.jar *.java

    I got a jar generated in the folder, I dragged it over to the library folder in my eclipse project.

    However, I still don't find any of the required classes available while coding.

    Additionally, I tried compiling the code >javac *.java and then tried the jar creation step but no avail.

    What steps I missed?