Loading...

Java JAR

Package compiled class files into a jar so you can ship and run them as one file. In Pack.java, keep the public class Pack, print packed from main, compile with javac Pack.java, then create Pack.jar with jar --create --file Pack.jar Pack.class. Run with java -cp Pack.jar Pack so the screen shows packed. A second demo prints shipped and uses a jar that names Main-Class so java -jar Pack.jar works. This lesson is jar packaging, not a JUnit @Test check and not a Records accessor print.

One jar, run with -cp

Open Pack.java. Keep the public class name Pack. Delete leftover SumCheck JUnit asserts, leftover Card records demos, leftover Pipe streams pipelines, and leftover Cue lambda walks if they bury the jar story. The teaching file is a tiny main program you package, not an assertEquals test class.
Inside Pack.main print one word:
System.out.println("packed");
Compile first:
javac Pack.java
When braces match, javac stays quiet and writes Pack.class. Then create the jar from the class file:
jar --create --file Pack.jar Pack.class
Run the class from the jar on the classpath:
java -cp Pack.jar Pack
The screen shows packed. You are not launching java Pack from loose classes for this teaching shape. You are asking the JVM to load Pack from Pack.jar.

Pack jar -cp prints packed

java
public class Pack {
    public static void main(String[] args) {
        System.out.println("packed");
    }
}

Walk the path once. javac Pack.java builds Pack.class. jar --create --file Pack.jar Pack.class packs that class into Pack.jar. java -cp Pack.jar Pack starts Pack.main. println writes packed. No @Test runs. No t.name() runs. No stream().map runs.
Say the beginner rule while you look at the folder: compile the source, create the jar from the .class file, run with -cp and the public class name. Do not invent JUnit checks inside Pack.java on this page. Do not treat jar as a second compiler — javac still builds the class; jar only packages it.
If you still see a green JUnit report from a leftover SumCheck file, switch to Pack and keep the packed print. If jar says it cannot find Pack.class, compile again before you create the archive.

Main-Class jar, run with -jar

Keep the same public class Pack. Change the print so the second teaching shape is easy to name:
System.out.println("shipped");
Compile again:
javac Pack.java
Create a jar that records the entry class:
jar --create --file Pack.jar --main-class Pack Pack.class
Run with the jar launcher:
java -jar Pack.jar
The screen shows shipped. --main-class Pack tells the jar which public class holds main. java -jar reads that entry and starts it. Keep one clear print per teaching shape so the screen word stays easy to name.

Pack jar -jar prints shipped

java
public class Pack {
    public static void main(String[] args) {
        System.out.println("shipped");
    }
}

Walk the path once. javac Pack.java refreshes Pack.class. jar --create --file Pack.jar --main-class Pack Pack.class packs the class and stores the entry name. java -jar Pack.jar starts Pack.main. The screen shows shipped. Keep the two demos as two teaching shapes. First prove a -cp run that wants packed. Then prove a -jar run that wants shipped. Both shapes share public class Pack and the jar --create --file habit.
If you run java -jar Pack.jar on a jar that never received --main-class, the launcher fails because no entry class is stored. That failing jar is not the second teaching file. The second teaching file includes --main-class Pack.

JAR habits

Create, classpath run, Main-Class, and -jar run stay separate beginner moves.

📊 JAR habits for Pack

Habit Shape Beginner check
Create jar --create --file Pack.jar Pack.class packs Pack.class into Pack.jar
Classpath run java -cp Pack.jar Pack screen shows packed for the first demo
Main-Class jar --create --file Pack.jar --main-class Pack Pack.class stores entry class Pack
Jar run java -jar Pack.jar screen shows shipped for the second demo
Compile first javac Pack.java refresh .class before you recreate the jar

JAR is not JUnit and not Records

JUnit marked checks with @Test and compared expected values. Records held compact components you printed with accessors. JAR packages class files for shipping and running. Do not mix those three jobs in one beginner file today.
packed here is the first demo screen word from Pack.main inside a jar run with -cp. It is not assertEquals(5, ...) and not t.name() from a Tag record. When a leftover SumCheck file still talks about expected 5, leave that as history and keep Pack's print line.
Without javac, there is no fresh Pack.class to pack. Without jar --create, there is no Pack.jar. Without -cp or a Main-Class -jar path, you have not shown this lesson's run shapes. Keep JUnit annotations off this page. Keep record headers off this page.

Folder checks on the last optional page

List the folder after a successful create. You should see Pack.java, Pack.class, and Pack.jar together. Edit the source, save, run javac Pack.java, recreate the jar with the same create habit, then run again. A stale jar can hide a changed print if you skip create after an edit.
If jar complains about a missing class file, compile first. If java -cp Pack.jar Pack prints nothing, confirm main still calls println. If java -jar Pack.jar says no main manifest attribute, recreate with --main-class Pack.
Do not paste @Test from SumCheck into Pack. Do not paste record Tag from Card as a replacement for packaging. The optional JAR job is an archive of class files you run with -cp or -jar.

End of the optional path

You can keep public class Pack, print packed, compile with javac Pack.java, create Pack.jar with jar --create --file Pack.jar Pack.class, run java -cp Pack.jar Pack, then print shipped, recreate with --main-class Pack, and run java -jar Pack.jar. The table separates create, classpath run, Main-Class, and jar run. This is the last optional Java page in the course map.
Keep the Pack demos for a clear packaging pair. Keep SumCheck JUnit files off this page when you practice jar. Keep Card records files off this page when you practice jar. Keep inventing new optional topics off this page — the map ends here. Those habits are the checkpoint for the optional path. When the first run shows packed or the second run shows shipped, JAR did its job for this lesson.
One last pass through the folder: one public Pack, matching .class, Pack.jar present, create command remembered, -cp or -jar run matching the demo you keep open, no Scanner wait, no assertion required. Compile, recreate the jar, and run once more on the demo you keep open. If the screen still shows packed or shipped, the JAR shape is solid and the optional Java path is complete. Prefer one clear packaging habit per read so each pass teaches the same map.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top