Skip to content

ebouchut-laplateforme/java-external-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using an External Library in a Java Project

This project is aimed at installing, and using an external library in a Java project.

🦕You will do it the old way (once), meaning you will NOT use Maven for this purpose.
This will give you a grasp of a fraction of what Maven does for us behind the curtain.

To achieve this goal you need to:

  • Search for the library
  • Install the library locally
  • Compile our Java Source Files
  • Run the App
  • Configure the IDE

For the sake of example we will download, install the Gson library. We will use it to display/serialize an instance of an object of type Person as JSON.

We create a new Java project:

  • named java-external-library
  • with the Build System set to IntelliJ (NOT Maven)
  • with the following folder structure:
    java-external-library  # The root folder of the project 
      ├── docs             # Documentation
      ├── libs             # Libraries 
      ├── out              # Classes
      └── src              # Sources 

Download the Library

An (External) Java library is packaged as a Jar file.
A Jar file is an archive, that is a zip file with a conventional structure).

You can use the maven repository website to search for the Gson library:

https://mvnrepository.com/

  • Enter gson in the search box and press the Return key ()
  • You should end up on the Gson page
    In the table that lists the versions available:
    • Click latest version number (eg. today 2.13.2)
    • In the Files section:
      • click View All to get a list of all jar files for this version
        • Click gson-2.13.2.jar to download the jar with the classes
        • Click gson-2.13.2-sources.jar to download the Java source files

Install the Library

Copy the library files gson-3.12.3.jar and gson-3.12.3-sources.jar to your project's libs/ folder

Compile the App

cd $DEV/java-external-library

javac -d out -cp out:libs/gson-2.13.2.jar **/*.java

The above command compiles the Java source files under src/. This saves the generated Java classes (.class files) in the out/ folder.

The value of the -cp option contains the classpath, that is the folders where to find the classes (out) and the path to the library (libs/gson-2.13.2.jar). Classpath entries are separated with a special character:

  • On Linux/macOS: a colon (:)
  • On Windows: a semicolon (;)

Run the App

java -cp out:libs/gson-2.13.2.jar Main

Configure the IDE

You then configure your Java project in IntelliJ IDEA to declare where to find this library:

Of course, we do not even envision to code in Java without an IDE. Therefore, let's climb the second step, not to do this in a CLI anymore.

gson-003-config-ide.png

This indicates where the IDE will find the Gson library:

  • classes in libs/libs/gson-2.13.2.jar
  • source files in libs/gson-2.13.2-sources.jar

About

Where to download and how to install and use an external Java library (Gson), the old way ;-).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages