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 Systemset toIntelliJ(NOTMaven) - with the following folder structure:
java-external-library # The root folder of the project ├── docs # Documentation ├── libs # Libraries ├── out # Classes └── src # Sources
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:
- Enter
gsonin the search box and press theReturnkey (⏎) - 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
Filessection:- click
View Allto get a list of all jar files for this version- Click
gson-2.13.2.jarto download the jar with the classes - Click
gson-2.13.2-sources.jarto download the Java source files
- Click
- click
- Click latest version number (eg. today
Copy the library files gson-3.12.3.jar and gson-3.12.3-sources.jar
to your project's libs/ folder
cd $DEV/java-external-library
javac -d out -cp out:libs/gson-2.13.2.jar **/*.javaThe 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 (
;)
java -cp out:libs/gson-2.13.2.jar MainYou 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.
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
