← 講座に戻る: Java from scratch

Install the JDK and write your first program

動画は近日公開

Java is one of the most widely used languages in the world: it runs on servers, in business applications, on Android and in many banking systems. In this eight-week course you will start from zero and end up building a small console system. We will use Java 21, a long-term support (LTS) version, which means it gets updates for many years and is a safe choice for learning.

What you need to install

You need the JDK (Java Development Kit). It includes the javac compiler, the java virtual machine (JVM) that runs programs, and the standard libraries. Do not confuse the JDK with the JRE: the JRE can only run programs, while the JDK can also create them. To write code, always install the JDK.

  • Download JDK 21 from a free distribution, for example Eclipse Temurin (adoptium.net) or OpenJDK.
  • Run the installer and accept the default options; tick the option that adds Java to the PATH if it appears.
  • Install an editor: IntelliJ IDEA Community or Visual Studio Code with the "Extension Pack for Java". Even Notepad works to start.
  • Open a new terminal and check the installation with the commands below.
java
java -version
javac -version

If both commands show a version number (for example 21.0.x), you are ready. If the terminal says it does not recognize "java", close and reopen the terminal; if it still fails, the JDK bin folder is not in the PATH variable and you will need to add it.

Your first program

Create a folder for the course and inside it a file called Hello.java. In Java, all code lives inside a class, and the name of the public class must match the file name, respecting upper and lower case.

java
public class Hello {
    public static void main(String[] args) {
        // Print a greeting to the screen
        System.out.println("Hello, Java!");
    }
}

Let us look at each part. "public class Hello" declares the class. The main method is the entry point: the JVM looks for it and starts running from there. "public static void" means it is accessible from outside, it belongs to the class and not to an object, and it returns nothing. "String[] args" receives the command-line arguments. System.out.println prints a text and moves to a new line. Every statement ends with a semicolon, and braces delimit blocks.

Compile and run

Java works in two steps. First javac translates your source code into bytecode and creates a Hello.class file. Then java runs that bytecode on the JVM. Thanks to this, the same .class works on Windows, Linux and macOS. Since Java 11 you can also run a source file directly, without creating the .class, which is very handy for practice.

java
javac Hello.java
java Hello

java Hello.java

Typical errors

  • "class Hello is public, should be declared in a file named Hello.java": the file name does not match the class.
  • "';' expected": a semicolon is missing at the end of a statement.
  • "cannot find symbol": you misspelled a name, for example system.out in lowercase.
  • "Could not find or load main class": you ran from another folder or misspelled the name.

Always read the full error message: the compiler tells you the file, the line number and an arrow under the problem. Learning to read these messages is as important as writing code. Finally, remember that Java is case sensitive: Main and main are different names.

✎ マイノート

このブラウザにのみ保存されます。ログインするとアカウントに保存できます。0/5000 文字

次へ →