doc:appunti:prog:java_compile_jar_command_line
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
doc:appunti:prog:java_compile_jar_command_line [2021/11/08 12:03] – [Download the included libraries] niccolo | doc:appunti:prog:java_compile_jar_command_line [2021/11/08 18:16] (current) – [Running the Java program from jar file] niccolo | ||
---|---|---|---|
Line 3: | Line 3: | ||
On a **Debian GNU/Linux** version 11.0 Bullseye it is required to install the **default-jdk** package, to have the **javac** compiler. | On a **Debian GNU/Linux** version 11.0 Bullseye it is required to install the **default-jdk** package, to have the **javac** compiler. | ||
- | ===== Reqired | + | ===== Finding the reqired |
- | A Java project generally | + | A Java project generally |
<code java> | <code java> | ||
Line 19: | Line 19: | ||
</ | </ | ||
- | ===== Download | + | Each library may exists on the internet in several versions: it may be an hard work to **guess which exact version** was used by the developer for each one. You can use several **internet repositories** to search and download the archives you need, we suggest at least **[[https:// |
- | It is advisable | + | |
+ | ===== Where to put the required libraries ===== | ||
+ | |||
+ | The project files are layed-out hierarchically starting from the project' | ||
+ | |||
+ | We decided to put **the required libraries** into a **subdirectory** relative to the project' | ||
< | < | ||
Line 40: | Line 45: | ||
| | ||
</ | </ | ||
+ | |||
+ | This may not be the optimal solution, because jar libraries are not installed system-wide and thus they are not shared among other Java programs. This recipe has the advantage that the installation of the Java program and the required libraries can be accomplished by an **unprivileged user** in its home directory. | ||
+ | |||
===== Compile the Java code ===== | ===== Compile the Java code ===== | ||
- | FIXME **$CLASSPATH** | + | To be on the safe side, we delete all the previously compiled files which have extension **.class**, then we create a list of all the **.java** files which are to be compiled. The **CLASSPATH** |
- | < | + | < |
+ | #!/bin/sh | ||
+ | CLASSPATH=" | ||
find . -name ' | find . -name ' | ||
find . -name ' | find . -name ' | ||
- | javac --class-path $CLASSPATH @sources.txt | + | javac --class-path |
</ | </ | ||
===== Pack the Jar archive ===== | ===== Pack the Jar archive ===== | ||
- | FIXME **$CLASSES** | + | Finally we want to pack the Java program into a **jar** archive. We have to create a **manifest file** where we declare the Java class that is **the entry point** of the program and we list all the libraries that the **program depends on**. The archive will be named as requested and it will contains all the **.class** files found by the **find** command: |
<code bash> | <code bash> | ||
+ | #!/bin/sh | ||
+ | CLASSES=" | ||
echo ' | echo ' | ||
echo " | echo " | ||
- | |||
jar --create --verbose \ | jar --create --verbose \ | ||
--file=' | --file=' | ||
- | --main-class=' | ||
--manifest=' | --manifest=' | ||
$(find . -type f -name ' | $(find . -type f -name ' | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Running the Java program from jar file ===== | ||
+ | |||
+ | Finally we can **start the Java program** from the jar archive: | ||
+ | |||
+ | < | ||
+ | java -jar / | ||
+ | </ | ||
+ | |||
+ | Into the jar archive there is the **MANIFEST.MF** file, which will indicate where to search for the required libraries, relative to the same jar archive directory. | ||
doc/appunti/prog/java_compile_jar_command_line.txt · Last modified: 2021/11/08 18:16 by niccolo