Part 01 / Stage 1
How Java runs
Goal: Install JDK 21, set up an editor, compile and run your first program, and understand what
javacandjavaactually do. Time: ~3 hours · Level: absolute beginner, no coding experience needed
What you will learn
- What Java is, and where it is used
- The three letters that confuse everyone: JDK vs. JRE vs. JVM
- How to install JDK 21 LTS and check it worked
- Setting up an editor or IDE (VS Code or IntelliJ)
- Compiling with
javacand running withjava - What happens between your source code and the JVM: bytecode
- The classpath, and how to read a classpath error when you hit one
- Your first build:
Hello.javaand a welcome banner
Topics in this part
Practice
Try each one yourself first. Then check the solution.
Every exercise is one .java file with a public static void main(String[] args) method, just like Hello.java in the README — no variables, no loops, no if, no classes of your own beyond the one main lives in. Those come in later parts.
Exercise 1: My intro card
Scenario: On the first day of a coding bootcamp in Pune, everyone prints a small intro card.
What to do: Write a program that prints your name, city and goal on three lines, with a line of - above and below.
Sample output:
------------------------------
Name : Asha Kulkarni
City : Pune
Goal : Become a Java developer
------------------------------
💡 Hint: There is no string-repeat operator in Java yet (that is a String method, Part 2) — just type the 30 dashes out once, by hand.
Exercise 2: Fix the broken program
Scenario: A friend sends you this file. It does not compile.
public class Greeting
public static void main(String[] args) {
System.out.println("Good morning")
System.Out.println("Have a nice day");
System.out.println("Bye");
}
}
What to do: Fix every error so it compiles and prints three lines.
Sample output:
Good morning
Have a nice day
Bye
💡 Hint: There are three separate bugs: a missing { right after the class name, a missing ; after "Good morning"), and System.Out with a capital O (Java is case-sensitive — it is System.out).
Exercise 3: Ticket counter banner
Scenario: Pune Junction's ticket window wants a small screen banner.
What to do: Print a banner, 28 characters wide, with a border of * and two centred-ish lines of text inside it.
Sample output:
****************************
* Pune Junction - PNR *
* Window 3, Open 24x7 *
****************************
💡 Hint: Count characters by hand (or paste into an editor with a ruler) so both * line up under the top and bottom border.
Exercise 4: Compile to one folder, run from another
Scenario: You want to keep compiled .class files out of your source folder, the way real projects do.
What to do: Write a program that prints Fact: 7 x 8 = 56 using one System.out.println with + to join the text and (7 * 8). Then:
- Compile it straight into a build folder:
javac FactFile.java -d /tmp/java-ex4-out. - Run it from there, without moving your source file:
java -cp /tmp/java-ex4-out FactFile.
Sample output:
$ javac FactFile.java -d /tmp/java-ex4-out
$ java -cp /tmp/java-ex4-out FactFile
Fact: 7 x 8 = 56
💡 Hint: -d <folder> tells javac where to put the .class file; -cp <folder> tells java where to look for it. Your source file never has to move.
Exercise 5: Read your own classpath
Scenario: You want to see exactly what the JVM knows about itself, without guessing.
What to do: Write a program that prints three lines using System.getProperty(...): java.version, java.class.path, and os.name.
Sample output: (your own values will differ — this is (varies))
java.version = 21.0.11
java.class.path = .
os.name = Mac OS X
💡 Hint: System.getProperty("java.version") returns a String; join it with + the same way the README's ClasspathCheck.java does.
Exercise 6: Reproduce and fix a classpath error
Scenario: The README showed a classpath error happening to someone else. Now make it happen to you, on purpose, and write down exactly what you saw. What to do:
- Write a one-line program (any message) called
ClasspathFix.java, insidesolutions/(or wherever you keep your practice files). - Compile it there:
javac ClasspathFix.java. - Step out of that folder (
cd ..) and runjava ClasspathFix. Copy the exact error text into a comment at the top of your file. - Fix it with
java -cp <folder> ClasspathFix(no need tocdback), and copy that successful run into the same comment.
Sample output: (the broken command, from one folder above solutions/)
$ java ClasspathFix
Error: Could not find or load main class ClasspathFix
Caused by: java.lang.ClassNotFoundException: ClasspathFix
Sample output: (the fixed command, from the same folder)
$ java -cp solutions ClasspathFix
Classpath fixed. This line only prints once java finds ClasspathFix.class.
💡 Hint: The error is the same whether the .class file truly does not exist, or it exists somewhere -cp is not pointing at. ClassNotFoundException only ever means "not found on the classpath I was given."
Exercise 7: Bytecode peek with javap
Scenario: You want to see, in real bytecode, what System.out.println(6 * 7) actually costs.
What to do:
- Write
AddFacts.javawith one line:System.out.println(6 * 7);insidemain. - Compile it, then run
javap -c AddFacts.class. - Count the instructions inside
mainonly (not the constructor above it), and write that count as a comment at the bottom of your file.
Sample output:
$ javap -c AddFacts.class
Compiled from "AddFacts.java"
public class AddFacts {
public AddFacts();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: bipush 42
5: invokevirtual #13 // Method java/io/PrintStream.println:(I)V
8: return
}
Instructions in main: 4
💡 Hint: 6 * 7 never appears in the bytecode at all — javac already worked it out at compile time and stored 42 directly (bipush 42, "byte push"). This is the same constant-folding idea as Python's compiler folding 1760 * 2 into 3520.
📁 About the solution filenames: Exercises 1, 2, 3 and 5 are solved in
solutions/ex01.java,ex02.java,ex03.java,ex05.java— the stage-wide naming convention for every part from here on. Exercises 4, 6 and 7 keep their lesson-mandated names (FactFile.java,ClasspathFix.java,AddFacts.java) instead, because for those three the filename is the subject of the exercise (classpath and bytecode commands name the file directly) — renaming them would break the exercise itself.exNN.javafiles use a non-publictop-level class (class Ex01 { ... }), which is why the filename does not need to match the class name — only apublicclass has that requirement (Part 1, Section 4).
👉 Solutions
Show the solutions for Part 1 (7 files)
solutions/AddFacts.java
// Exercise 7: Bytecode peek with javap
public class AddFacts {
public static void main(String[] args) {
System.out.println(6 * 7);
}
}
// Instructions in main: 4
solutions/ClasspathFix.java
// Exercise 6: Reproduce and fix a classpath error
//
// Real transcript (run from the solutions/ folder):
//
// $ javac ClasspathFix.java
// $ cd ..
// $ java ClasspathFix
// Error: Could not find or load main class ClasspathFix
// Caused by: java.lang.ClassNotFoundException: ClasspathFix
//
// Fix: either cd back into solutions/, or point -cp at it directly:
//
// $ java -cp solutions ClasspathFix
// Classpath fixed. This line only prints once java finds ClasspathFix.class.
public class ClasspathFix {
public static void main(String[] args) {
System.out.println("Classpath fixed. This line only prints once java finds ClasspathFix.class.");
}
}
solutions/ex01.java
// Exercise 1: My intro card
class Ex01 {
public static void main(String[] args) {
System.out.println("------------------------------");
System.out.println("Name : Asha Kulkarni");
System.out.println("City : Pune");
System.out.println("Goal : Become a Java developer");
System.out.println("------------------------------");
}
}
solutions/ex02.java
// Exercise 2: Fix the broken program (the fixed version)
class Ex02 {
public static void main(String[] args) {
System.out.println("Good morning");
System.out.println("Have a nice day");
System.out.println("Bye");
}
}
solutions/ex03.java
// Exercise 3: A ticket counter banner
class Ex03 {
public static void main(String[] args) {
System.out.println("****************************");
System.out.println("* Pune Junction - PNR *");
System.out.println("* Window 3, Open 24x7 *");
System.out.println("****************************");
}
}
solutions/ex05.java
// Exercise 5: Read your own classpath (and a couple of other JVM properties)
class Ex05 {
public static void main(String[] args) {
System.out.println("java.version = " + System.getProperty("java.version"));
System.out.println("java.class.path = " + System.getProperty("java.class.path"));
System.out.println("os.name = " + System.getProperty("os.name"));
}
}
solutions/FactFile.java
// Exercise 4: Compile to one folder, run from another
// Commands used (run from the solutions/ folder):
// javac FactFile.java -d /tmp/java-ex4-out
// java -cp /tmp/java-ex4-out FactFile
public class FactFile {
public static void main(String[] args) {
System.out.println("Fact: 7 x 8 = " + (7 * 8));
}
}
Common mistakes
| Mistake | Why it is wrong | Fix |
|---|---|---|
java Hello.java |
java wants a class name, not a file name |
java Hello (no .java, no .class) |
javac Hello |
javac wants a file name (with .java), not a class name |
javac Hello.java |
File hello.java with public class Hello |
Filename must match the public class name exactly, including case | Rename the file to Hello.java |
Running java Hello from the wrong folder |
The classpath (default: current folder) has no Hello.class in it |
cd to the right folder, or use java -cp <folder> Hello |
Reading ClassNotFoundException as "the file is missing" |
It means "not found on the classpath I was given", which is a different problem | Check which folder -cp points at, or where you are standing |
| Installing a JRE instead of a JDK | A JRE can run programs but has no javac to compile them |
Install a JDK (this course: JDK 21) |
Expecting java --version to show your IDE's bundled JDK |
Your terminal may use a different java than your IDE |
Check both; point your IDE at the same JDK your terminal uses |
Quick recap
- JDK = tools to write and compile Java (install this); JRE = what is needed to run it; JVM = the engine inside the JRE that runs bytecode.
javac File.javacompiles source into a.classfile (bytecode);java ClassNameruns it, no extension.- The public class name and the file name must match exactly.
- Source → bytecode (
.class, made byjavac) → the JVM runs it (java), optionally JIT-compiling hot code to real machine code. javap -cshows you the real bytecode inside a.classfile.- The classpath (
-cp, default.) is wherejavalooks for classes; "could not find or load main class" +ClassNotFoundExceptionmeans it was not found there.
Checklist
- I installed JDK 21 and
java --version/javac --versionboth show 21. - I set up VS Code or IntelliJ for Java.
- I can explain JDK vs. JRE vs. JVM in my own words.
- I compiled
Hello.javawithjavacand ran it withjava. - I can describe source → bytecode → JVM, and have looked at real bytecode with
javap -c. - I deliberately caused a classpath error, read the real message, and fixed it.
- I built and ran the welcome banner program myself.