Java 21 First Console App: Structure, Compile, Run
Learn the smallest useful Java console program: how source code becomes bytecode, how `public class Main` and `main` work, and how to print, parse text with `Scanner`, and read command-line arguments.
What you will learn
- After this lesson you can compile and run a simple Java program with
javacandjava. - After this lesson you can identify the role of a class, the
mainmethod, statements, comments, and packages. - After this lesson you can use
System.out,Scanner, andString[] argsin small console programs. - After this lesson you can explain the difference between JDK, JRE, bytecode, and JVM at a beginner level.
1. JDK, JRE, JVM, and bytecode in one flow
For beginner console apps, the important flow is: you write .java source code, javac compiles it into .class bytecode, and java launches that bytecode on the JVM.
The JDK is the developer kit: it includes tools such as javac and java. The JRE is the runtime part needed to run Java applications. The JVM is the virtual machine that executes Java bytecode.
In a terminal, a class-based program is usually compiled and run like this: - javac Main.java - java Main
Java can also run a single source file directly with the source-file launcher, for example java Main.java, which is useful for tiny experiments. For learning project structure, still practise the explicit javac then java flow.
2. The smallest useful Java program structure
A Java program is organized into classes. For a simple console app, the Java launcher starts by looking for a main method with this shape: public static void main(String[] args).
Inside main, you write statements. A statement usually ends with a semicolon. Comments are ignored by the compiler and are used to explain intent: // for one line, and /* ... */ for a block.
If a top-level class is public, the source file name must match it. So public class Main belongs in Main.java.
3. Printing, scanning text, and command-line arguments
Use System.out.println(...) to print a line to the console. Use System.out.print(...) when you do not want a newline after the text.
Scanner is a convenient beginner tool for splitting input into tokens such as words and numbers. In real console programs, you often construct it with System.in; in the runnable example below, it reads from a fixed string so the output is deterministic.
String[] args stores command-line arguments passed after the class name, such as java Main Alice 42. If no arguments are passed, args.length is 0.
4. Packages and common file layout
A package gives classes a namespace, such as com.codebeats.demo. If a file declares package com.codebeats.demo;, that declaration must be the first non-comment line.
The folder path should match the package path. For example, package com.codebeats.demo; commonly lives under com/codebeats/demo/Main.java when compiling directly, or under src/main/java/com/codebeats/demo/Main.java in many build-tool projects.
For your first few programs, it is fine to use no package and one file named Main.java. Once you create multiple classes, packages help avoid name clashes and make code easier to navigate.
Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// A statement: call println to write one line.
System.out.println("Java console app");
String data = "Asha 19";
Scanner scanner = new Scanner(data);
String name = scanner.next();
int age = scanner.nextInt();
System.out.println("Name: " + name);
System.out.println("Next year: " + (age + 1));
System.out.println("Argument count: " + args.length);
scanner.close();
}
}This is a complete class-based Java program. It prints text, reads tokens from a Scanner, does integer arithmetic, and shows that no command-line arguments were passed in this sandbox run.
javac Main.java
java MainThese commands compile Main.java to bytecode and then launch the Main class. They are shown as shell commands and are not executed as part of this lesson sandbox.
Common mistakes
✕ Naming the file differently from a
publicclass, for examplepublic class MaininsideHello.java.✓ Either name the file
Main.java, or change the public class name to match the file name.✕ Writing
mainwith the wrong signature, such aspublic void main()orstatic main(String args).✓ Use
public static void main(String[] args)so the Java launcher can find the program entry point.✕ Trying to use
Scannerwithout importing it.✓ Add
import java.util.Scanner;before the class, or use the fully qualified namejava.util.Scanner.✕ Putting
package com.example;at the top but compiling from the wrong folder.✓ Keep the folder structure aligned with the package, such as
com/example/App.java, and run from the classpath root.
Your challenge
Write a Java console program that reads fixed student records from strings formatted as name marks. Print name:PASS when marks are at least 40, otherwise print name:RETRY. Your program should print one line per test case.
Interview questions
What is the purpose of public static void main(String[] args)?
It is the entry point the Java launcher calls to start a class-based application. public makes it accessible, static allows it to run without creating an object, void means it returns no value, and String[] args receives command-line arguments.
What is the difference between javac Main.java and java Main?
javac Main.java compiles source code into bytecode, usually a .class file. java Main launches the JVM and runs the compiled class named Main.
When would you use Scanner in a beginner console app?
Use Scanner to parse simple text input into tokens such as words, integers, and doubles. For interactive input it is commonly constructed with System.in; for tests or examples it can parse a fixed string.
Why do packages matter in Java projects?
Packages create namespaces for classes, reduce naming conflicts, and give projects a predictable folder structure. A package declaration such as package com.example; should match the class location under com/example.
Practice
- Create a program named
Mainthat prints your name, city, and one learning goal on three separate lines. - Modify the runnable example so the
Scannerreads two integers from a string and prints their sum. - Write a class
Appin packageschool.demoand describe the folder path and commands needed to compile it.
Quick quiz
Key takeaways
- A class-based Java console app usually starts with
public static void main(String[] args). - Use
javac Main.javato compile andjava Mainto run the compiled class. System.out.printlnprints lines;Scannerparses text into values;argsholds command-line arguments.- If a class is
public, the file name must match the class name exactly. - Package names map to folder paths and should be introduced once your code grows beyond tiny examples.
References
- Oracle Java 21 javac command (docs.oracle.com)
- Oracle Java 21 java command (docs.oracle.com)
- Java SE 21 System API (docs.oracle.com)
- Java SE 21 Scanner API (docs.oracle.com)
Related YouTube lessons
See the full Java learning path