17. Package, Jars, and Deployment in Java
Now, we know how to develop a Java application but, how we deliver our developed application to the end-user. End-user has not the system…
17. Package, Jars, and Deployment in Java

Now, we know how to develop a Java application but, how we deliver our developed application to the end-user. End-user has not the system as us. When we deliver our application to the end-user, the application must be compatible with the end-user system as well.
So, In Java, we have multiple options to deploy our application. Let’s see what are they.
- Local → The GUI, the program we developed deploy into the executable JAR. The entire application runs on the end user’s system and the application is completely stand-alone.
- Combination of Local and Remote → The application is distributed with a client portion running on the user’s local system, connected to a server where other parts of the application are running.
- Remote → The entire application runs on the remote server. And we access the application through the web browser.

image snippet from the HeadFirst Java book
Before we see how we deploy the application, we need to understand that maintaining source files and class files in different folders is also very important. Because organizing files in different folders can reduce your stress. So, create a project with two main directories called source and classes. Then, compile your source code files with “-d (directory) flag”.
cd MyProject/Source
javac -d ../class MyApp.java
And you can compile all the source files using the *“javac -d ../class .java” **command.
JAR
A JAR file is nothing but Java ARchive. It bundles all the classes into one single JAR file. This means you can hand over one single JAR file rather than hand overring class files. And JAR is based on the pkzip file format.
If we hand over the class then the client can run a class that contains the main method through the command line. But, the question is how the client runs the JAR file? To run the JAR file, the JAR file must be an executable JAR. Let’s see how can we make an executable JAR file?
Make executable JAR file
- Make sure that you separate the class files from the source code files and put all the class files into the class directory.
- Create a manifest.txt file and write inside which class has the main method like “Main-Class: MyApp”. Don’t put a class extension to the end and make sure that this class has only one line. Then, put the manifest file into the classes directory.
- At last, run the jar tool to create the JAR file. Make sure that, this command should run in the classes directory.
%cd MyProject/classes
%jar -cvmf manifest.txt app1.jar *.class OR
%jar -cvmf manifest.txt app1.jar MyApp.class
So, now the point is to come to how we run the .jar file. JVM is capable for load class files from the JAR and invoking the main method. JVM doesn’t care from where the class files come but you need to give a valid classpath. Using “-jar app1.jar” command, you tell to the JVM that, this time you have a JAR file to run then, JVM looks for the main method that you state in the manifest.txt file in the given jar file. But, if the JVM is unable to find the main method, then it will throw a runtime exception. These processes are depending on the client’s operating system sometimes are you able to run the executable JAR file by double clicking on it.
Package
Now, you organize class files and source code files separately and It looks clear now. But, in the Java API, all the classes are under a package. For example, ArrayList is in java.util package, Socket class in the java.net package, and JButton in the javax.swing package etc… Why these all are in the package? We can put it all together. It also works right!
Here is the thing, assume that you have worked on a project with two developers. At a time, you three developed a class with the same name. And you all want to save the three classes with the same name in your project. What happened next? It creates naming conflicts. But, when we put those classes in separate packages, we will solve the problem. Not only that, our application is flexible to use other libraries as well.
Packaging isn’t just for preventing naming conflicts, Packaging is a key feature of Java development that every developer must follow. A class full name comes with combining the package name and the class name together and technically we called the full name of a class as a fully-qualified class name.
If you notice that, java.util.ArrayList, java.net.Socket and java.lang.String has started their fully-qualified name with java. All the classes follow some valid structure. We discuss that packaging prevents the name conflict but, what happens when there is two class with the same fully-qualified class name? So, to reduce the risk of naming convention, Sun strongly suggests that to use the reverse domain name as the first part of the package name because domain names are unique.
For a example:- com.thilini.java.training.A
Put class in to the package
- As the first step, choose the name for the package. You must be sure that the package name is unique.
- Put the package statement into the class
package com.thilini.java.traning;
import java.util.ArrayList;
class A{}
- Set up matching directory structure → For this example, you must create four directories. First, create the “com” folder then inside the com folder, create “thilini” folder then, inside the thilini folder, create “java” folder. Inside the java folder, create “training” folder. And put A class into the training folder.
Compiling and Running with the Package
In the Core Java library, JVM easily finds the location of the class but, in our own packages, it is a little bit tricky. We use the d flag to compile our source code but, this time it is different.
%cd MyProject/source
%javac -d/classes/com/thilini/java/trainig/A.java
To compile all the class inside same package
%cd MyProject/source
%javac -d/classes/com/thilini/java/trainig/*.java
To Run the code, use fully-qualified name of the class.
%cd MyProject/classes
%java com.thilini.java.trainig.A
Make executable JAR file with packages
- → Make sure that your classes in the correct class structure as per the package name.
- → Create a manifest.txt file and write inside which class has the main method like “Main-Class: com.thilini.java.trainig.MyApp”. This time you must use the fully qualified class name. And manifest.txt file is in the classes directory not inside the packages.
- → At last, run the jar tool to create the JAR file. Make sure that, this command should run in the classes directory. In here, I specified the com directory because, to get everything inside it.
%cd MyProject/classes
%jar -cvmf manifest.txt app1.jar com
Additionally, if you want to look at actually what is inside the .jar file. Then, you can list out all with the below code.
% jar -tf packEx.jar
tf stands for Table File.
To extract the content of a JAR file, you can use the below code.
% jar -xf packEx.jar
xf stands for Extract File.
And one more thing, when you put all into the jar file, then the JVM creates a directory called META-INF stands for Meta Information. And creates MANIFEST.MF under the META-INF directory and puts all the contents of manifest.txt to the MANIFEST.MF. It means JVM doesn’t include the manifest.txt file into the jar it only takes the content of it.
Java Web Start (JWS)

Image snippet from the HeadFirst Java Book
With the Java Web Start, Java applications launch through the web browser, and still the Java application runs as a stand-alone application. When we talk about how it happened the first time the user accessed the application, the client machine gets installed a small program like a plugin-in through the link. So, the small java program lives on the client machine and we know it as Java Web Start (helper app).
After the JWS installed the client machine, its starts main() method and you don’t need to start it through the web page link. The surprising thing is that JWS still able to listen to the class changes on the server without going for updating. But, to run the JWS on the client side, Java must be installed otherwise it doesn’t work and Its very easy to use. JWS is only executable Jar file.
How JWS starts work
- → Client clicks the JWS application link on the web browser. Ex :- <a href=“MyApp.jnlp” >Click</a>
- The server gets the HTTP request and sends back .jnlp to the client.
- JWS started and request the executable JAR file.
- The server serves executable JAR files.
- JWS get the JAR and invokes the main() method.
The .jnlp file
jnlp stands for Java Network Launch Protocol. To make the JWS App, we need to create .jnlp file. This file is nothing but an a XML file that uses to find the main() method.
Happy Learning!
References
Sierra, K. and Bates, B. (2005). Head first Java. Sebastopol, Ca: O’Reilly.
메타데이터
- post_id
- eb20af0d70d0
- slug
- 17-package-jars-and-deployment-in-java-eb20af0d70d0
- url
- https://medium.com/@thilini_/17-package-jars-and-deployment-in-java-eb20af0d70d0
- canonical_url
- https://medium.com/@thilini_/17-package-jars-and-deployment-in-java-eb20af0d70d0
- author_url
- https://medium.com/@thilini_
- status
- ok
- fetched_at
- 2026-07-26 20:54:42