Java
What Java is
›Short versionOpen if you only have a minute.
- Hello.java is the text you edit. Hello.class is the file a JVM actually executes.
- javac checks the source and writes the class file. It does not run the program.
- java Hello starts a JVM, loads Hello.class, and calls main.
- A JDK includes the compiler and the launcher. A JRE can run class files and cannot compile new ones.
- One class file runs on any machine with a compatible JVM. A file path inside the program still has to make sense there.
You save a file named Hello.java, run a compile command, and a second file appears beside it: Hello.class. The first is the text you can edit. The second is what a actually executes.
If you send only the source, the other person still has to compile it. If you send the class file, their computer can run that same file when it has a JVM of its own. James Gosling, working at Sun Microsystems, released the language in 1995 so a program could move between machines without a fresh rewrite for each operating system.
The terminal knows java and does not know javac. Can this machine turn Hello.java into Hello.class?
It can run a class file that already exists. The compiler lives in the JDK. A runtime alone leaves javac unrecognized, so the source still needs a development kit.
Source to running line
Edit
Hello.java is the text you change.
Compile
javac checks the source and writes Hello.class.
Run
java Hello starts a JVM and calls main.
Ship
Copy the class file; each OS supplies its own JVM.
What you edit
- Hello.java is source. Names, braces, and comments stay readable for the next edit.
- The public class name must match the file name, capitals included.
What you run and copy
- Hello.class holds bytecode. A JVM on that machine loads it and calls main.
- Send the class file when the other machine should run without recompiling.
Kit vs runtime
| Install | javac | java | Typical use |
|---|---|---|---|
| JDK | Yes | Yes | Write and run on your machine |
| JRE / runtime | No | Yes | Run class files someone else compiled |
| Hello.java | N/A | N/A | Source the compiler reads |
| Hello.class | N/A | N/A | Bytecode the JVM executes |
Read the diagram definition
sequenceDiagram
participant You
participant javac
participant Class as Hello.class
participant JVM
You->>javac: Hello.java
javac-->>Class: bytecode
You->>JVM: java Hello
JVM->>Class: load
JVM->>JVM: call main
JVM-->>You: prints HelloFrom a .java file to a running program
The that does the translation is named javac. It reads Hello.java, checks that the public class name matches the file, and writes Hello.class. That class file holds , a compact instruction set with the same shape on Windows, macOS, and Linux.
The smallest program that can take this trip is a class with a main method. The JVM looks for public static void main(String[] args) and calls it. public lets the launcher see the method, static means it can run before any object exists, and void means it does not return a value.
The string array is the list of words typed after the class name, and it is empty when you pass none. If the public class is Hello, the file must be Hello.java, including the capital letters. javac refuses the mismatch and writes nothing.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}From the folder that contains Hello.java, the two commands are compile, then run. Leave the .class suffix off the second command. The launcher starts a JVM and asks it to load the class named Hello.
javac Hello.javajava Hello
The path is short once the names are familiar. You edit source, javac checks it and writes bytecode into a class file, and the JVM loads that class and calls main.
What you install
A is the kit that includes both javac and the java launcher, plus the standard libraries. A is a runtime: a JVM and those libraries, without the compiler. Current JDK downloads carry the runtime with them, so a separate JRE is less common than it used to be.
If java works and javac is missing, the machine can run class files someone else already compiled. Installing a JDK puts both commands on the path. When you press Run in an editor, that button is still invoking a compiler and a JVM from a JDK.
The same class file on Windows, macOS, and Linux
You compile Hello.java once on a laptop, then copy Hello.class to a Linux server and to a Mac. Each machine prints the same line, and nobody recompiles. That is : each operating system supplies its own JVM, and that JVM turns the same bytecode into instructions for the CPU it is sitting on.
The JVM on each machine does the local work, including any call into the operating system. A hardcoded path such as C:\data\notes.txt is still a Windows path after the class file lands on Linux, and a library that calls native code still has to match the operating system where it runs.
The practical next step is to install a JDK and run this program yourself, which is what Get started walks through. After the program prints a line, syntax is where the shape of a class and a statement is spelled out.
Simple rule of thumb
Treat Hello.class as the portable artifact and Hello.java as the editable source. If javac is missing, you can run but not rebuild. If java is missing, the class file has no runner on that machine.
Good default: install a JDK, match the public class name to the file name, compile with javac, then run with java and no .class suffix on the command line.