uzytkownik1234 uzytkownik1234 - 7 months ago 10
Java Question

How does importing libraries in Java work

I'm starting to program in Java and I can't quite understand how importing libraries works.

Suppose I need to import a library in order to be able to write a specific program. I save it in my PC.

How is someone who does not have the same library on their machine supposed to be able to run the program? This is probably a silly question, but I really am new to programming.. Thanks.

Answer

They need a java runtime (JRE) installed.

Third party libraries like apache commons, accompany your application jar.

myapp.jar
lib/apache-commons.jar
lib/ido.jar

The myapp.jar (zip format) contains a METAINF/MANIFEST.MF containing an entry

Main-Class: ...
Class-Path: lib/apache-commons.jar, lib/ido.jar

And with that myapp.jar is an executable jar.

A build tool like maven can be used to build such a distribution: take care of library versions (dependencies), preparing the manifest, generate to output folder.