software-engineering, tutorial, java,

Back to Basics: How to create a java JAR using command line

Adrian Precub Adrian Precub Follow Jun 11, 2023 · 3 mins read
Back to Basics: How to create a java JAR using command line
Share this

In the world of Java development, JAR(Java Archive) files play a very important role in packaging and distributing our applications. Whether you are a beginner or an experienced developer, it’s essential to understand the process of creating a JAR file. Of course, with all the IDE’s(Integrated Development Environments) and build tools like maven and gradle available, this is done automatically for us.

If you’re a beginner Java developer and curious about the process of creating JAR files, we have you covered. You’ll get an inside look at what it actually takes to create these JAR files without the help of build tools or IDE’s.

For the purpose of this tutorial, we assume that you already have a JDK(Java Development Kit) installed. If you need to install the JDK, you can follow this tutorial.

1 Write the code

Before creating a jar file, we need to write the java code. This code is what will be translated into bytecode(which the JVM(Java Virtual Machine) can understand and execute), and eventually end up in our jar file. It can include classes, interfaces and other necessary components for our application to work properly. For our purpose, we’ll create a very simple Hello World application. For this, let’s create a file called MyJavaFile.java and add the following code in it:

public class MyJavaFile {
    public static void main(String[] args) {
    System.out.println("Hello World!");
}

2 Compile the code

In order for the JVM to understand our java code, we need to transform it into bytecode. We can do this with the javac command, followed by our java file name. So the name of the file, not the name of the class inside the file:

javac MyJavaFile.java

If the compilation is successful, meaning we have no errors in your code, the result of this command will be a file called MyJavaFile.class.

3 Create a manifest file

A manifest file is a crucial component of a JAR file. It contains key:value pairs with information about the manifest version, main class to be executed when we run a jar, and others. For a more in-depth understanding about how manifest file can be used in java, take a look at the documentation.

In the same folder as our java class, let’s create a directory called META-INF and in this folder, create a file called MANIFEST.MF with the following content:

Manifest-Version: 1.0
Main-Class: MyJavaFile
Built-By: adrian

4 Package jar file

The last step required to create a jar file, is to package our java class and manifest files into a jar file that can be executed from command line.

jar cmvf META-INF/MANIFEST.MF myJarFile.jar MyJavaFile.class
added manifest
adding: MyJavaFile.class(in = 426) (out= 289)(deflated 32%)

5 Test jar file

Now that the jar file is created, it’s time to test it out and make sure it behaves correctly when ran from command line:

java -jar myJarFile.jar
Hello World!

6 Conclusion

By following the above steps, we can package our java code into a portable and distributable format. Remember to write clean and error-free code, compile it into bytecode, create the manifest file and put everything in a jar file. Before sharing with others, it’s always best to test our jar files to make sure they function as intended.

Now that you know the basics, go ahead and create your jar file and share it with the world!

Adrian Precub
Written by Adrian Precub Follow
Staff Engineer