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 revisionLast revisionBoth sides next revision | ||
doc:appunti:prog:java_compile_jar_command_line [2021/11/08 16:41] – [Finding the reqired libraries] niccolo | doc:appunti:prog:java_compile_jar_command_line [2021/11/08 18:15] – [Pack the Jar archive] niccolo | ||
---|---|---|---|
Line 19: | Line 19: | ||
</ | </ | ||
- | 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:// | + | 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:// |
- | ===== Download | + | ===== Where to put the required |
- | It is advisable | + | The project files are layed-out hierarchically starting from the project' |
+ | |||
+ | We decided | ||
< | < | ||
Line 43: | 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** 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