User Tools

Site Tools


doc:appunti:prog:java_compile_jar_command_line

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
doc:appunti:prog:java_compile_jar_command_line [2021/11/08 16:43] – [Finding the reqired libraries] niccolodoc:appunti:prog:java_compile_jar_command_line [2021/11/08 18:16] (current) – [Running the Java program from jar file] niccolo
Line 22: Line 22:
  
  
-===== Download the included libraries =====+===== Where to put the required libraries =====
  
-It is advisable to **create directory** which will contains all the **libraries** (downloaded as **.jar** archives) required by your projectthat directory should be placed at the root level of your Java project.+The project files are layed-out hierarchically starting from the project's root directory; in our example the source files are into the path **org.vmax.amba**. 
 + 
 +We decided to put **the required libraries** into a **subdirectory** relative to the project's root directory; we created a subdirectory named **lib** and placed all the **.jar** archives into it. At runtimethe compiled jar archive will search the libraries starting from its directory, descending the same **lib** subdirectory:
  
 <file> <file>
Line 43: Line 45:
          └─ midrive/          └─ midrive/
 </file> </file>
 +
 +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** environment variable is intialized with the name of all the **jar libraries**, separated by a colon:
  
-<code>+<code bash> 
 +#!/bin/sh 
 +CLASSPATH="$(echo lib/*.jar | sed 's/\.jar /\.jar:/g')"
 find . -name '*.class' | xargs -r rm find . -name '*.class' | xargs -r rm
 find . -name '*.java' > sources.txt find . -name '*.java' > sources.txt
-javac --class-path $CLASSPATH @sources.txt+javac --class-path "$CLASSPATH@sources.txt
 </code> </code>
  
 ===== 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 lib/*.jar)"
 echo 'Main-Class: org.vmax.amba.AppMain' > manifest.txt echo 'Main-Class: org.vmax.amba.AppMain' > manifest.txt
 echo "Class-Path: $CLASSES" >> manifest.txt echo "Class-Path: $CLASSES" >> manifest.txt
- 
 jar --create --verbose \ jar --create --verbose \
     --file='firmware-editor-tool.jar' \     --file='firmware-editor-tool.jar' \
-    --main-class='org.vmax.amba.AppMain' \ 
     --manifest='manifest.txt' \     --manifest='manifest.txt' \
     $(find . -type f -name '*.class')     $(find . -type f -name '*.class')
 </code> </code>
 +
 +
 +===== Running the Java program from jar file =====
 +
 +Finally we can **start the Java program** from the jar archive:
 +
 +<code>
 +java -jar /path/java-program/firmware-editor-tool.jar
 +</code>
 +
 +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.1636386185.txt.gz · Last modified: 2021/11/08 16:43 by niccolo