Begin with a source file
A .java file holds human-readable source code. In the conventional example, Main.java contains a public class Main with a main method. Java syntax, types and access rules define what you may write. The compiler checks those rules before the application can be launched as a compiled class.
Java is statically typed, but this does not mean every variable needs an explicit type annotation: the compiler can infer some local variable types. It also does not validate data arriving from a network or a user; runtime checks are still necessary.
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Codectionary!");
}
}Compile source into bytecode
The javac tool, included in the JDK, reads Main.java and normally writes Main.class. The class file contains Java bytecode: instructions for a JVM rather than machine instructions for one particular operating system. javac reports syntax and type errors with file and line information; fix those errors before expecting the program to run.
For this small class, run javac Main.java in the folder containing the file. A larger project may use build tools and dependencies, but the same source-to-bytecode idea still applies.
Terminal — compile
javac Main.javaLet the JVM execute the class
Run java Main without the .class extension. The Java launcher starts a JVM, loads Main and invokes its main method. The JVM verifies and executes bytecode, uses runtime services such as memory management and calls into libraries when the program needs them. A just-in-time (JIT) compiler can translate frequently used bytecode into native machine code as the program runs.
Expected output is Hello, Codectionary!. If you edit Main.java but forget to run javac again, java Main can still run the old Main.class. Recompile, rerun and compare the output.
Terminal — run
java MainJDK, runtime, JVM and standard library
A JDK is the development kit. It includes tools such as javac and java as well as what you need to run Java applications. A runtime-only installation is intended to execute applications, not compile new source. The JVM is the virtual machine that loads and executes bytecode. The standard library is a collection of ready-made classes and APIs, including System, String and collections.
A project may need a particular Java release because language features and APIs change over time. The release used to compile code also affects which older runtimes can load its class files. Verify java -version and javac -version, especially when you have several JDKs installed.
| Part | What it does |
|---|---|
| Main.java | Human-readable source code you edit. |
| javac | Checks and compiles source code. |
| Main.class | Generated bytecode for the JVM. |
| java / JVM | Launches and executes the class. |
| Standard library | Supplies reusable APIs such as System.out. |
Real projects can run differently
The compile-then-run sequence is the clearest beginner model, but it is not the only workflow. The java launcher can run a simple source file directly on supported JDKs, invoking compilation as part of launch. IDEs and build tools can compile for you. Some applications use ahead-of-time compilation or other runtime optimisations instead of relying only on JIT compilation.
The JVM targets a platform-independent bytecode format, but an application still depends on the installed Java version, libraries and any platform-specific APIs. A browser does not normally run .class files. If you see a missing class or unsupported class-file-version error, check the class name, working directory, dependencies and Java versions before changing code at random.
Syntax
javac Main.java
java MainExamples
Compile and launch Main
The two commands deliberately expose the compiler and runtime steps.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Codectionary!");
}
}- The source file is Main.java because it declares public class Main.
- javac Main.java checks the source and creates Main.class when compilation succeeds.
- java Main asks the launcher to load that class and call main.
- println writes one line. After editing, compile again to replace the old bytecode.
Best practices
- Use java -version and javac -version to identify the installed JDK tools.
- Recompile after editing a source file when using the two-command workflow.
- Treat compiler success and correct runtime behaviour as separate checks.
Runtime & versions
Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Java Language Specification