Chapters & contents Lesson 1 of 20
3 min read
Java foundationsExpanded reading

From Java source to a running program

Follow source code, compilation, bytecode, and the Java runtime.

What you’ll learn

  • Distinguish the JDK and JVM
  • Explain compilation and execution errors
  • Run a small program with the documented toolchain

Understand the tools

A Java program begins as source text, but the source file and the running program are different things. The Java Development Kit provides tools for developing and running Java applications. One of those tools is a compiler, and the Java Virtual Machine is the execution environment for compiled Java bytecode. Keeping these roles separate helps you diagnose failures. A missing tool command is an environment problem; a malformed statement is a source-code problem; an exception during execution is a runtime problem.

Write the smallest useful program

Begin with one source file and a simple action such as displaying a greeting. Use the official getting-started instructions for the JDK version you installed because Java supports several launch styles and newer source forms. A traditional class-based example is still useful for understanding explicit structure. Save the file with the expected name and extension. A text editor can silently add an extra extension, so verify the actual filename when the compiler cannot locate it.

java · Worked example
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, OpenLearning!");
    }
}
// Save as Hello.java, then run: javac Hello.java
// Start the compiled class with: java Hello

Compile and inspect feedback

In the traditional workflow, javac compiles source into class files containing bytecode. The compiler checks syntax and many type rules before the program runs. Read the first relevant diagnostic and its location rather than changing several lines at random. One missing delimiter can lead to additional messages farther down the file. Compilation succeeding means those checks passed; it does not prove that the program computes the intended result or handles every input correctly.

Launch the program

The java launcher starts the runtime and executes the selected entry point. The launcher may also support running a source file directly, depending on the program form and installed JDK. Do not confuse a source filename with a class name in commands that expect different inputs. Work from a known directory and keep the first exercise free of external libraries. This makes class-path and project-tool issues easier to distinguish from basic language mistakes.

Build a diagnostic habit

Check the installed tool versions and keep the official documentation for that version nearby. If the compiler command is unavailable, inspect installation and command-search configuration before rewriting the program. If execution starts but reports an exception, read the exception type and the frames involving your code. Once the greeting works, deliberately introduce one syntax mistake, correct it, and then introduce one runtime failure. Observing each category safely builds a reusable debugging model.

Try it yourself

Classify these failures: javac is not found; a semicolon is missing in a traditional example; the running application accesses an invalid array index.

Show a possible solution

The first is an installation or command-search problem, the second is a compilation diagnostic, and the third is a runtime exception. Investigate the layer that produced the failure.

Check your understanding

Try 14 questions across java foundations. Read each explanation and revisit anything that needs another pass.

Practise this subject

Go to the source

Use these primary references to check details and explore the topic further.

Saved in this browser. Signed-in progress syncs to your account.