Installing Java & Your First Program
Installing Java & Your First Program
In the previous lesson you learned what Java is and how the JVM works. Now it is time to get Java running on your machine and write your very first program. By the end of this lesson you will have the JDK installed, understand the difference between javac and java, and know exactly what every line of a Hello World class does.
Installing the JDK
Java programs need the JDK (Java Development Kit) to be compiled and run. There are several distributions; the most widely used are:
- Oracle JDK — the official release from Oracle. Free for development; requires a license for commercial production use beyond Java 17.
- OpenJDK — the fully open-source reference implementation. Free for all use. Most Linux distributions ship it via their package managers.
- Eclipse Temurin (Adoptium) — a community-built OpenJDK distribution, highly recommended for beginners and professionals alike.
Step-by-step installation:
- Go to adoptium.net (Eclipse Temurin) and download the JDK 21 installer for your operating system.
- Run the installer and accept the defaults. On Windows, tick the option to set JAVA_HOME and add Java to the PATH.
- Open a terminal (Command Prompt / PowerShell on Windows, Terminal on macOS/Linux) and verify the installation:
You should see output like:
JAVA_HOME to locate the JDK. Setting it during installation saves you from headaches later.
javac vs java — Two Separate Tools
The JDK ships with two command-line programs you will use constantly:
javac— the Java compiler. It reads your.javasource files and produces.classfiles containing bytecode.java— the Java launcher. It starts the JVM and runs a compiled.classfile (or a JAR archive).
The workflow is always: write → compile with javac → run with java. This two-step process is what lets the same bytecode run on any operating system with a JVM installed.
Writing Your First Program
Create a new file called HelloWorld.java and type the following exactly:
HelloWorld, the file must be saved as HelloWorld.java. A mismatch causes a compiler error.
Anatomy of main()
Let us break down every keyword on that main line, because each one is required:
public— the JVM launcher must be able to call this method from outside the class, so it must be public.static— the JVM callsmainbefore any objects exist.staticmeans the method belongs to the class itself, not to an instance.void—maindoes not return a value to the JVM.main— the exact name the JVM looks for as the entry point of every Java program.String[] args— an array ofStringvalues passed in from the command line. You can ignore this for now, but the signature must include it.
Inside main, System.out.println(...) prints a line of text to the console. System is a built-in class, out is a static field of type PrintStream, and println is a method that prints the argument followed by a newline.
Compiling and Running
Open your terminal, navigate to the folder where you saved HelloWorld.java, and run these two commands:
You should see:
java HelloWorld.java. This is handy for quick experiments, but under the hood the JVM still compiles to bytecode — it just does it in memory. For any real project, always use javac explicitly.
What Happens Step by Step
javac HelloWorld.javareads your source, checks for syntax errors, and writesHelloWorld.class(bytecode).java HelloWorldstarts the JVM, loadsHelloWorld.class, finds thepublic static void main(String[] args)method, and calls it.- The JIT compiler inside the JVM converts the most-used bytecode to native machine instructions on the fly for performance.
System.out.printlnwrites to standard output, and you see the text in your terminal.
Recommended IDEs
While you can write Java in any text editor, a proper IDE makes development much faster. The two most popular choices are:
- IntelliJ IDEA Community Edition — free, arguably the best Java IDE. Auto-completes, highlights errors in real time, and runs your program with a single click.
- VS Code + Extension Pack for Java — lightweight alternative if you prefer VS Code.
Both options detect your installed JDK automatically. From now on, lessons will assume you are using either the terminal or an IDE — both approaches work identically.
Summary
You installed the JDK, confirmed it works with java -version, learned that javac compiles and java runs, wrote a HelloWorld class, and dissected every keyword in the main method signature. This two-step compile-then-run workflow is the foundation of everything you will do in Java.