So far for me it it was so easy to right click in Eclipse IDE and create executable .jar file with few simple clicks. Last week I had to create executable .jar file manually
on my Digital Ocean node on which we hosts usually lots of services.
It took some time to create .jar file directly using only commands but finally after 10 minutes, executable .jar file was ready.
If you have any of below questions then you are at right place:
- Best way to create a jar file in Linux command-line
- How do I make a jar file executable in Linux?
- How do I compile a jar file in Linux?
- How to make a JAR file Linux executable?
- How to Create and Execute a .Jar File in Linux Terminal?
Let’s get started:
Step-1) Make sure you have Java install on Linux system
If Java is already installed then go to step-4
directly. Try running command javac
and if you see below result then there isn’t any Java install on host.
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
Step-2) Install Java on linux host
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
Step-3) Verify on Java
Just try running below commands again and you should see installed java details.
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)
Step-4)
Here are the list of commands you need to execute in order to create executable .jar files.
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 .
And you are all set.
As you see above, you have Crunchify.jar
file created under /root/crunchify/src/build
folder.
The post How to create executable .jar file using Linux commands and without Eclipse Shortcut? appeared first on Crunchify.
0 Commentaires