Wie erstelle ich eine ausführbare .jar-Datei mit Linux-Befehlen und ohne Eclipse-Verknüpfung?
Veröffentlicht: 2022-01-03
Bisher war es für mich so einfach, mit der rechten Maustaste in Eclipse IDE zu klicken und mit wenigen einfachen Klicks eine ausführbare .jar-Datei zu erstellen. Letzte Woche musste ich .jar file manually
auf meinem Digital Ocean-Knoten erstellen, auf dem wir normalerweise viele Dienste hosten.
Es dauerte einige Zeit, die .jar-Datei direkt nur mit Befehlen zu erstellen, aber nach 10 Minuten war die ausführbare .jar-Datei fertig.
Wenn Sie eine der folgenden Fragen haben, dann sind Sie hier richtig:
- Der beste Weg, um eine JAR-Datei in der Linux-Befehlszeile zu erstellen
- Wie mache ich eine JAR-Datei unter Linux ausführbar?
- Wie kompiliere ich eine JAR-Datei unter Linux?
- Wie mache ich eine JAR-Datei unter Linux ausführbar?
- Wie erstelle und führe ich eine .jar-Datei im Linux-Terminal aus?
Lass uns anfangen:
Schritt-1) Stellen Sie sicher, dass Sie Java auf einem Linux-System installiert haben
Wenn Java bereits installiert ist, fahren Sie direkt mit step-4
fort. Versuchen Sie, den Befehl javac
, und wenn Sie das folgende Ergebnis sehen, gibt es keine Java-Installation auf dem Host.
1 2 3 4 5 6 7 8 |
root @ localhost : ~ / java / src # javac Command 'javac' not found , but can be installed with : apt install openjdk - 11 - jdk - headless apt install default - jdk apt install openjdk - 8 - jdk - headless apt install ecj |
Schritt-2) Installieren Sie Java auf dem Linux-Host
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
root @ localhost : ~ / java / src # apt install openjdk-11-jdk-headless Reading package lists . . . Done Building dependency tree Reading state information . . . Done Suggested packages : openjdk - 11 - demo openjdk - 11 - source The following NEW packages will be installed : openjdk - 11 - jdk - headless 0 upgraded , 1 newly installed , 0 to remove and 92 not upgraded . Need to get 217 MB of archives . After this operation , 228 MB of additional disk space will be used . Get : 1 http : //mirrors.linode.com/ubuntu cosmic-updates/main amd64 openjdk-11-jdk-headless amd64 11.0.1+13-3ubuntu3.18.10.1 [217 MB] Fetched 217 MB in 3s ( 70.3 MB / s ) Selecting previously unselected package openjdk - 11 - jdk - headless : amd64 . ( Reading database . . . 107409 files and directories currently installed . ) Preparing to unpack . . . / openjdk - 11 - jdk - headless_11 . 0.1 + 13 - 3ubuntu3.18.10.1_amd64.deb . . . Unpacking openjdk - 11 - jdk - headless : amd64 ( 11.0.1 + 13 - 3ubuntu3.18.10.1 ) . . . Setting up openjdk - 11 - jdk - headless : amd64 ( 11.0.1 + 13 - 3ubuntu3.18.10.1 ) . . . update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jar to provide / usr / bin / jar ( jar ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jarsigner to provide / usr / bin / jarsigner ( jarsigner ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / javac to provide / usr / bin / javac ( javac ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / javadoc to provide / usr / bin / javadoc ( javadoc ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / javap to provide / usr / bin / javap ( javap ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jcmd to provide / usr / bin / jcmd ( jcmd ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jdb to provide / usr / bin / jdb ( jdb ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jdeprscan to provide / usr / bin / jdeprscan ( jdeprscan ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jdeps to provide / usr / bin / jdeps ( jdeps ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jimage to provide / usr / bin / jimage ( jimage ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jinfo to provide / usr / bin / jinfo ( jinfo ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jlink to provide / usr / bin / jlink ( jlink ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jmap to provide / usr / bin / jmap ( jmap ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jmod to provide / usr / bin / jmod ( jmod ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jps to provide / usr / bin / jps ( jps ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jrunscript to provide / usr / bin / jrunscript ( jrunscript ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jshell to provide / usr / bin / jshell ( jshell ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jstack to provide / usr / bin / jstack ( jstack ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jstat to provide / usr / bin / jstat ( jstat ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jstatd to provide / usr / bin / jstatd ( jstatd ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / rmic to provide / usr / bin / rmic ( rmic ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / serialver to provide / usr / bin / serialver ( serialver ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jaotc to provide / usr / bin / jaotc ( jaotc ) in auto mode update - alternatives : using / usr / lib / jvm / java - 11 - openjdk - amd64 / bin / jhsdb to provide / usr / bin / jhsdb ( jhsdb ) in auto mode |
Schritt-3) Auf Java überprüfen
Versuchen Sie einfach, die folgenden Befehle erneut auszuführen, und Sie sollten installierte Java-Details sehen.

1 2 3 4 5 6 7 |
root @ localhost : ~ / crunchify / src / package # which java / usr / bin / java root @ localhost : ~ / crunchify / src / package # java -version openjdk version "11.0.1" 2018 - 10 - 16 OpenJDK Runtime Environment ( build 11.0.1 + 13 - Ubuntu - 3ubuntu3.18.10.1 ) OpenJDK 64 - Bit Server VM ( build 11.0.1 + 13 - Ubuntu - 3ubuntu3.18.10.1 , mixed mode , sharing ) |
Schritt 4)
Hier ist die Liste der Befehle, die Sie ausführen müssen, um ausführbare .jar-Dateien zu erstellen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
root @ localhost : ~ # pwd / root root @ localhost : ~ # mkdir crunchify root @ localhost : ~ # cd crunchify/ root @ localhost : ~ / crunchify # mkdir src root @ localhost : ~ / crunchify # mkdir src/package root @ localhost : ~ / crunchify # cd src/package/ root @ localhost : ~ / crunchify / src / package # vi Crunchify.java root @ localhost : ~ / crunchify / src / package # cat Crunchify.java public class Crunchify { public static void main ( String args [ ] ) { System . out . println ( "\n\nHello there... /n This is an example to create executable .jar file using only commands...\n\n" ) ; } } root @ localhost : ~ / crunchify / src / package # cd .. root @ localhost : ~ / crunchify / src # mkdir build root @ localhost : ~ / crunchify / src # mkdir build/classes root @ localhost : ~ / crunchify / src # javac -sourcepath src -d build/classes package/Crunchify.java root @ localhost : ~ / crunchify / src # cd package/ root @ localhost : ~ / crunchify / src / package # cp Crunchify.java ../ root @ localhost : ~ / crunchify / src / package # cd .. root @ localhost : ~ / crunchify / src # java -classpath build/classes/ Crunchify Hello there . . . / n This is an example to create executable . jar file using only commands . . . root @ localhost : ~ / crunchify / src # echo Main-Class: Crunchify>myManifest root @ localhost : ~ / crunchify / src # jar cfm build/Crunchify.jar myManifest -C build/classes/ . root @ localhost : ~ / crunchify / src # java -jar build/Crunchify.jar Hello there . . . / n This is an example to create executable . jar file using only commands . . . root @ localhost : ~ / crunchify / src # root @ localhost : ~ / crunchify / src # cd build/ root @ localhost : ~ / crunchify / src / build # ls -ltra total 16 drwxr - xr - x 2 root root 4096 Mar 31 00 : 48 classes drwxr - xr - x 4 root root 4096 Mar 31 00 : 52 . . - rw - r -- r -- 1 root root 830 Mar 31 00 : 52 Crunchify . jar drwxr - xr - x 3 root root 4096 Mar 31 00 : 52 . |
Und Sie sind bereit.
Wie Sie oben sehen, haben Sie die Datei Crunchify.jar
im Ordner /root/crunchify/src/build
erstellt.