Essential Java Terminal Commands — A Practical Cheat Sheet

If you're starting out with Java or simply prefer working from the terminal (like I sometimes do), or simply wish to play around with terminal, knowing the right commands can save you a lot of frustration. So, here's a practical guide to the most common javac
, java
, and jar
terminal commands.
🛠️ 1. Compile a Java File
javac MyClass.java
This compiles MyClass.java
into MyClass.class
. Think of it as converting your code into something the JVM can understand.
▶️ 2. Run a Compiled Class
java MyClass
Runs the MyClass.class
file. Do not add .class
when running it.
📁 3. Compile with Output Directory
javac -d out MyClass.java
This puts the compiled .class
file inside an out
folder.
Useful when you want to keep things clean — especially for larger projects.
📂 4. Compile Multiple Files
javac *.java
Compiles all .java
files in the current folder. Simple and effective.
📦 5. Compile with Classpath (External Libraries)
javac -cp libs/library.jar MyClass.java
Tells Java to include external .jar
files when compiling.
Alternative:
javac -classpath libs/library.jar MyClass.java
Same thing, just longer syntax.
🔁 6. Run with Classpath
java -cp .:libs/library.jar MyClass
Use :
on Mac/Linux, ;
on Windows:
java -cp .;libs/library.jar MyClass
📦 7. Create a Simple JAR File
jar cfv MyApp.jar MyClass.class
c
→ createf
→ name of jar filev
→ show what’s being added
🚀 8. Create an Executable JAR (With Main-Class)
jar cfe MyApp.jar MyClass MyClass.class
e
→ set entry point (Main-Class)This creates a JAR you can run directly.
▶️ 9. Run a JAR File
java -jar MyApp.jar
Runs the JAR if it has a proper Main-Class
in its manifest.
🔍 10. View JAR Contents
jar tf MyApp.jar
Lists the contents of the JAR. Great for double-checking what went in.
⚡ Bonus Commands
🔄 Compile + Run in One Line
javac MyClass.java && java MyClass
Convenient for testing small programs quickly.
📚 Compile All Java Files Recursively
javac -d out $(find . -name "*.java")
Compiles all .java
files in the current folder and subfolders.
🧹 Delete All .class
Files
find . -name "*.class" -delete
Use this when you want a clean slate (especially before a fresh build).
Subscribe to my newsletter
Read articles from Shailesh Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shailesh Patil
Shailesh Patil
I'm a Java backend developer learning core concepts to deepen my understanding. Most resources were either too shallow or too overwhelming—so I started sharing my perspective to simplify the learning process. If you're on a similar path, I hope this helps you too.