02. Getting started with Java packages, projects, classpath & jar files

This extends “Setting up Java step by step tutorial“.

What are packages & why are they important?

A java package is a group of similar types of classes, interfaces and sub-packages. In Java you have built-in packages in the src.zip with *.java files & rt.jar with *.class files within packages such as java.lang, java.util, java.sql etc. and user-defined packages like com.main, com.service, etc. Packages have the benefits of easy maintenance, access protection, and removes naming collisions as you can have two “HelloWorld.java” files in different packages like com.app1 and com.app2. Packages are nothing but a folder structure.

Step 1: Create a package named com.main under “c:\projects”. This is nothing but creating a new folder named “com” under “c:\projects”, and another folder named “main” under “c:\projects\com”.

HelloWorld.java in com.main package

Step 2: Create a file named “HelloWorld.java” in the “c:\projects\com\main” folder (aka directory).

Step 3: Create a new folder named “service” under “c:\projects\com”.

HelloService.java in com/service package

Step 4: Create a file named “HelloService.java” under “c:\projects\com\service”

Compile & run it as a Java app

Step 5: Compile it with javac.

Step 6: Run it with java.

Output:

A project is made up of packages

A project is nothing but a folder above the packages. So, we could put the packages com/main & com/service inside a folder called “beginner-proj-1“.

Step 1: Create a new folder named “beginner-proj-1” under “c:\projects”, and move the folder “com”, under “c:\projects\beginner-proj-1”

What if you try to run it from “c:\projects” or any other folders?

-classpath or -cp to the rescue

This is where the classpath comes in. You can use either “-classpath” or “-cp”. It tells where to find your *.class files.

Package your entire project as a deployable jar file

JAR stands for Java ARchive, which is like a zip file to hold all your java packages (i.e. folders/sub folders inside a project) and artifacts like class files, properties files, etc within the packages. The project here is “beginner-proj-1” and version of the jar file is “1.0.0”.

To list the jar file contents:

-C” means change to that folder.

Q. What if you want to only include the .class files?
A. Your jar files should only have the .class files.

Option 1:

Go to that particular project (i.e. beginner-proj-1) and then run

Option 2:

It is a good practice to place the compiled class files into a separate sub folder like “\bin”.

and then jar only the “beginner-proj-1\bin” folder with

You can use a jar file in the classpath instead of a folder

jar files can be deployed to multiple environments like test, uat (i.e. User Acceptance Testing), prod (i.e. Production), etc. You can also give it to a friend to run it as long as he/she has “Java Runtime Environment” (i.e JRE) version 8.

Note: The jar file beginner-proj-1-1.0.0.jar created on Windows-64bit can be run on a Linux or Mac machine as long as JRE 8 for that operating system is installed. That is why it is called “Write once, and run anywhere“.

Multiple projects (i.e more jar files)

Let’s create two projects instead of one. beginner-proj-1 containing the “com/main/HelloWorld.class” and beginner-proj-2 containing the “com/service/HelloService.class”. Create 2 jar files as shown below:

Now, to run the application

Practice writing your own Java applications without using an IDE. The 250+ Core Java interview questions and answers & Java coding exercises have lots of code for you to practice.

Next Java Beginner Tutorial: Setting up Maven step by step tutorial.


800+ Java Q&As & tutorials

Top