One of the many advantages of Java, in my experience, is its ability to deliver applications in a neat and tidy package (called a JAR, or Java archive.) JAR files make it easy for users to download and launch an application they want to try, easy to transfer that application from one computer to another (and Java is cross-platform, so sharing liberally can be encouraged), and easy to understand for new programmers to look inside a JAR to find out what makes a Java app run.
There are many ways to create a JAR file, including toolchain solutions such as Maven and Gradle, and one-click build features in your IDE. However, there are also stand-alone commands such as
gjar, and just plain old
jar, which are useful for quick and simple builds, and to demonstrate what a JAR file needs to run.
On Linux, you may already have the
jar commands as part of an OpenJDK package, or GCJ (GCC-Java.) You can test whether any of these commands are installed by typing the command with no arguments:
Try 'fastjar --help' for more information.
jar: must specify one of -t, -c, -u, -x, or -i
jar: Try 'jar --help' for more information
Usage: jar [OPTION...] [ [--release VERSION] [-C dir] files] ...
Try `jar --help' for more information.
I have all of them installed, but you only need one. All of these commands are capable of building a JAR.
On a modern Linux system such as Fedora, typing a missing command causes your OS to prompt you to install it for you.
Build a JAR
First, you need a Java application to build.
To keep things simple, create a basic "hello world" application in a file called hello.java:
It's a simple application that somewhat trivializes the real-world importance of managing external dependencies. Still, it's enough to get started with the basic concepts you need to create a JAR.
Next, create a manifest file. A manifest file describes the Java environment of the JAR. In this case, the most important information is identifying the main class, so the Java runtime executing the JAR knows where to find the application's entry point.
$ mdir META-INF
$ echo "Main-Class: Main" > META-INF/MANIFEST.MF
Compiling Java bytecode
Next, compile your Java file into Java bytecode.
$ javac hello.java
Alternately, you can use the Java component of GCC to compile:
$ gcj -C hello.java
Either way, this produces the file
$ file Main.class
Main.class: compiled Java class data, version XX.Y
Creating a JAR
You have all the components you need so that you can create the JAR file.
I often include the Java source code as a reference for curious users, but all that's required is the
META-INF directory and the class files.
fastjar command uses syntax similar to the
$ fastjar cvf hello.jar META-INF Main.class
Alternately, you can use
gjar in much the same way, except that
gjar requires you to specify your manifest file explicitly:
$ gjar cvf world.jar Main.class -m META-INF/MANIFEST.MF
Or you can use the
jar command. Notice this one doesn't require a Manifest file because it auto-generates one for you, but for safety I define the main class explicitly:
$ jar --create --file hello.jar --main-class=Main Main.class
Test your application:
$ java -jar hello.jar
Hello Java World
jar help you manually or programmatically build JAR files, while other toolchains such as Maven and Gradle offer features for dependency management. A good IDE may integrate one or more of these features.
Whatever solution you use, Java provides an easy and unified target for distributing your application code.