Codectionary / Developer documentation / Java

Create Main.java, compile it with the JDK, run it from a terminal and understand each important part of the code.

Prepare a folder and a JDK

Install a Java Development Kit (JDK) for your operating system first. The JDK provides javac, which compiles code, and java, which launches it. Open a terminal and check java -version and javac -version. If either is missing, use the setup guide for your system before continuing.

Create a folder such as java-first-program, then open that folder in an editor and terminal. Keeping the source and terminal in one folder makes it easier to find the generated class file. On Windows, PowerShell can create and enter it with the first command below; on macOS or Linux use the second. If the folder already exists, just change into it.

PowerShell — create and enter a new folder

New-Item -ItemType Directory java-first-program
Set-Location java-first-program

macOS/Linux terminal — create and enter a new folder

mkdir java-first-program
cd java-first-program

Either terminal — verify the JDK

java -version
javac -version

Write Main.java

Create a plain-text file named Main.java inside your practice folder. Enter the exact code below and save it. The capital M matters: a public class named Main must be in a file named Main.java. An editor may add starter code; replace it rather than leaving two Main classes in one file.

Java uses braces to group the class and method bodies. Indentation makes nesting visible to readers, although the braces define the blocks. Each opening brace here has a matching closing brace.

Main.java

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Codectionary!");
    }
}

Understand the important syntax

public class Main declares a class called Main that can be accessed from outside its source file. public static void main(String[] args) declares the conventional entry point used by the java launcher in this class-based example. The String[] args parameter can receive command-line arguments; this program does not use them. static lets the launcher call main without first creating a Main object, and void means the method does not return a value.

System is a standard-library class. Its out field represents standard output, and println is a method that prints the supplied value followed by a newline. "Hello, Codectionary!" is a string literal because the characters are enclosed in double quotation marks. The parentheses contain the value passed to println; the semicolon ends the statement.

Compile the file

In the terminal, move into the folder containing Main.java and run javac Main.java. If compilation succeeds, the compiler writes Main.class in the same folder. The .class file contains bytecode, not editable Java source. An empty terminal response from javac usually means success; list the folder to confirm the new file.

If javac displays errors, read the first message and its line number. Fix a missing quote, brace, semicolon or filename mismatch in the source, save it, and compile again. A compiler error is feedback about the source, not output from a successfully running program.

Terminal — compile Main.java

javac Main.java

Run and inspect the output

Run java Main in the same folder. Use the class name without .java or .class for this workflow. The launcher starts a JVM, loads Main.class and calls main. System.out.println then writes Hello, Codectionary! to the terminal. That visible line is standard output.

If nothing appears, check that the class was compiled and contains the print statement. If the launcher cannot find Main, confirm your working folder and that Main.class was created. This two-command sequence is intentionally explicit so you can see the difference between compiling and running.

Terminal — launch the compiled class

java Main

Make a small change and test again

Change the string to "Hello from Java!", save Main.java, then repeat javac Main.java and java Main. The new terminal line should read Hello from Java!. The old output after a source edit usually means you ran the previous Main.class without recompiling.

Try another small change, such as declaring String name = "Sam"; before the print call and printing "Hello, " + name + "!". The plus operator joins these strings. Predict the result, compile, run and compare. This is the same feedback cycle used in larger projects.

A small variation inside main

String name = "Sam";
System.out.println("Hello, " + name + "!");

Why some Java tutorials look different

A class-based main method is a conventional starting point and works across many supported JDK releases. Newer Java releases also support simpler compact source files and source-file launching, so an IDE’s latest template may omit the class declaration or make compilation appear automatic. Those variations do not change the basic idea that Java source must be processed before the JVM can execute suitable code.

For this tutorial, keep the explicit Main.java and two-command workflow. Once you understand it, learn how your chosen IDE and build tools automate those steps.

Recognise common errors

“javac not found” means the compiler is unavailable in this terminal; install a JDK or fix the selected PATH. “Class Main is public, should be declared in a file named Main.java” means the public class and filename differ. “Could not find or load main class Main” usually means the class file is missing, the current folder is wrong or the class name differs.

A line-numbered compiler diagnostic is usually more useful than guessing. Fix one issue, save, compile and run again. Keep the first version small so each error has an understandable cause.

Syntax

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Codectionary!");
    }
}

Examples

Hello from a compiled Java class

Expected output after javac Main.java and java Main: Hello, Codectionary!

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Codectionary!");
    }
}
  1. The file is Main.java because it declares public class Main.
  2. The main method gives the launcher a starting point; the args array is unused here.
  3. System.out.println prints the quoted string and a newline.
  4. javac produces Main.class; java Main runs it. Save and recompile after changing the source.

Best practices

  • Work in the folder containing Main.java when using the two terminal commands.
  • Save and recompile after each source edit.
  • Read the first compiler diagnostic and compare the actual output with your prediction.

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

Specifications & further reading