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).
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.main; //write .class to subfolders com/main import com.service.HelloService; public class HelloWorld { public static void main(String[] args) { System.out.println("My first java code without IDE & with packages"); HelloService.print(); } } |
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”
1 2 3 4 5 6 7 8 9 10 |
package com.service; //write class file to subfolder com/service public class HelloService { public static void print() { System.out.println("A class in another package is called"); } } |
Compile & run it as a Java app
Step 5: Compile it with javac.
1 |
c:\projects>javac com/main/*.java com/service/*.java |
Step 6: Run it with java.
1 |
C:\projects>java com.main.HelloWorld |
Output:
1 2 3 |
My first java code without IDE & with packages A class in another package is called |
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”
1 2 3 4 |
C:\projects\beginner-proj-1>java com.main.HelloWorld My first java code without IDE & with packages A class in another package is called |
What if you try to run it from “c:\projects” or any other folders?
1 2 3 |
C:\projects>java com.main.HelloWorld Error: Could not find or load main class com.main.HelloWorld |
-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.
1 2 3 4 |
C:\projects>java -classpath c:/projects/beginner-proj-1 com.main.HelloWorld My first java code without IDE & with packages A class in another package is called |
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”.
1 2 3 4 5 6 7 8 9 10 |
c:\projects>jar -cvf beginner-proj-1-1.0.0.jar -C ./beginner-proj-1 . added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/main/(in = 0) (out= 0)(stored 0%) adding: com/main/HelloWorld.class(in = 524) (out= 354)(deflated 32%) adding: com/main/HelloWorld.java(in = 282) (out= 197)(deflated 30%) adding: com/service/(in = 0) (out= 0)(stored 0%) adding: com/service/HelloService.class(in = 442) (out= 312)(deflated 29%) adding: com/service/HelloService.java(in = 214) (out= 147)(deflated 31%) |
To list the jar file contents:
1 2 3 4 5 6 7 8 9 10 11 |
c:\projects>jar -tvf beginner-proj-1-1.0.0.jar 0 Thu May 18 22:23:36 AEST 2017 META-INF/ 69 Thu May 18 22:23:36 AEST 2017 META-INF/MANIFEST.MF 0 Thu May 18 21:58:12 AEST 2017 com/ 0 Thu May 18 22:07:42 AEST 2017 com/main/ 524 Thu May 18 22:07:42 AEST 2017 com/main/HelloWorld.class 282 Thu May 18 22:07:36 AEST 2017 com/main/HelloWorld.java 0 Thu May 18 22:07:42 AEST 2017 com/service/ 442 Thu May 18 22:07:42 AEST 2017 com/service/HelloService.class 214 Thu May 18 21:58:28 AEST 2017 com/service/HelloService.java |
“-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
1 2 |
c:\projects\beginner-proj-1>jar -cvf beginner-proj-1-1.0.0.jar com/main/HelloWorld.class com/service/HelloService.class |
Option 2:
It is a good practice to place the compiled class files into a separate sub folder like “\bin”.
1 2 |
c:\projects\beginner-proj-1>javac -d bin com/main/*.java com/service/*.java |
and then jar only the “beginner-proj-1\bin” folder with
1 2 |
c:\projects>jar -cvf beginner-proj-1-1.0.0.jar -C beginner-proj-1\bin . |
You can use a jar file in the classpath instead of a folder
1 2 3 |
c:\projects>java -cp beginner-proj-1-1.0.0.jar com.main.HelloWorld My first java code without IDE & with packages A class in another package is called |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
c:\projects>jar -cvf beginner-proj-1-1.0.0.jar -C ./beginner-proj-1 . added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/main/(in = 0) (out= 0)(stored 0%) adding: com/main/HelloWorld.class(in = 524) (out= 354)(deflated 32%) adding: com/main/HelloWorld.java(in = 282) (out= 197)(deflated 30%) c:\projects>jar -cvf beginner-proj-2-1.0.0.jar -C ./beginner-proj-2 . added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/service/(in = 0) (out= 0)(stored 0%) adding: com/service/HelloService.class(in = 442) (out= 312)(deflated 29%) adding: com/service/HelloService.java(in = 214) (out= 147)(deflated 31%) |
Now, to run the application
1 2 3 |
c:\projects>java -cp beginner-proj-1-1.0.0.jar;beginner-proj-2-1.0.0.jar com.main.HelloWorld My first java code without IDE & with packages A class in another package is called |
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.