Getting started with Core Java means learning how Java code moves from a .java source file to a running program on the JVM. You should understand the JDK, write a small class, compile it with javac, run it with java, and recognize the common setup errors that beginners face.
This lesson walks through Java setup step by step, explains the first program line by line, and shows how to use both the terminal and an IDE without treating either one like magic.
Think of Java as a two-step system. You write source code in a .java file, the Java compiler converts it into portable bytecode in a .class file, and the JVM runs that bytecode on your operating system.
Before writing your first Java program, understand the three pieces people mention all the time: JVM, JRE, and JDK. They are related, but they do not mean the same thing.
For learning and development, install the JDK. The JDK includes the compiler and tools needed to create Java programs. If you only have a runtime, you may be able to run existing Java applications, but you will not be able to compile your own source code.
| Component | Full Name | Description |
|---|---|---|
| JVM | Java Virtual Machine | Runs compiled Java bytecode. The JVM is platform-specific, so Windows, Linux, and macOS each have their own JVM implementation. |
| JRE | Java Runtime Environment | JVM plus standard runtime libraries. It is enough to run Java programs, but not enough for development. |
| JDK | Java Development Kit | JRE plus development tools such as javac, java, jar, javadoc, and jshell. This is what beginners should install. |
Use a Long-Term Support version unless your course, company, or project asks for something specific. Java 21 is a strong default for modern learning because it is an LTS release and is widely supported by tools.
You can install the Oracle JDK, Eclipse Temurin, Amazon Corretto, Microsoft Build of OpenJDK, or another OpenJDK distribution. For beginner Core Java learning, the syntax and commands are the same across these distributions.
The exact installer differs by operating system, but the goal is the same: make the java and javac commands available in your terminal.
java -version
javac -version
Start with a simple folder, not a complex project. Create a folder named java-practice, then create a file named HelloWorld.java inside it.
Java is case-sensitive. HelloWorld, helloworld, and HelloWORLD are different names. If the class is public, the filename must match the public class name exactly.
// HelloWorld.java
public class HelloWorld { // 1. Class declaration
public static void main(String[] args) { // 2. Entry point
System.out.println("Hello, World!"); // 3. Print to console
}
}
Open a terminal inside the folder where HelloWorld.java is saved. First compile the file with javac. Then run the class with java.
Notice the difference: javac uses the filename with .java, but java uses the class name without .class.
| Step | Command | Result |
|---|---|---|
| Compile | javac HelloWorld.java | Creates HelloWorld.class bytecode if there are no syntax errors. |
| Run | java HelloWorld | Starts the JVM and runs the HelloWorld class. |
cd java-practice
javac HelloWorld.java
java HelloWorld
The main method looks strange at first because it contains several Java keywords. For now, memorize the form and understand the role of each part.
| Part | Meaning |
|---|---|
| public | The JVM can access this method from outside the class. |
| static | The JVM can call main without creating an object first. |
| void | The method does not return a value. |
| main | The special method name Java looks for when starting a program. |
| String[] args | Command-line arguments passed to the program. |
The args parameter stores values typed after the class name in the terminal. This is useful for small utilities and helps you understand how Java receives input from outside the program.
public class Greeting {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Please pass your name.");
return;
}
System.out.println("Hello, " + args[0] + "!");
}
}
javac Greeting.java
java Greeting Anil
Command-line arguments are passed when the program starts. Scanner is different: it lets your program ask questions and read values while it is running.
Scanner is beginner-friendly, but be careful when mixing nextInt() and nextLine(). nextInt() reads only the number and leaves the newline behind, which can surprise beginners.
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
scanner.close(); // always close the scanner
}
}
You can write Java using an IDE such as IntelliJ IDEA, Eclipse, VS Code, or NetBeans. An IDE gives you code completion, project navigation, formatting, debugging, and one-click run buttons.
Still, learn the terminal commands first. When you understand javac and java, IDE behavior becomes easier to debug because you know what the tool is doing behind the scenes.
Most early Java errors are setup, naming, or syntax errors. The error message usually tells you what went wrong, but you need to know where to look.
| Error | Common Cause | Fix |
|---|---|---|
| javac is not recognized | JDK bin folder is not on PATH. | Install the JDK and add %JAVA_HOME%\bin or the JDK bin path to PATH. |
| class HelloWorld is public, should be declared in a file named HelloWorld.java | Filename does not match the public class name. | Rename the file or rename the class so both match exactly. |
| Could not find or load main class | Wrong class name, wrong folder, package mismatch, or using .class in the java command. | Run from the correct folder and use java ClassName without .class. |
| ; expected | Missing semicolon at the end of a statement. | Add the semicolon and compile again. |
Do not try to learn every Java feature on day one. Build a strong path from syntax to object-oriented programming, then move into collections, exceptions, files, and multithreading.
A small project helps connect setup, input, variables, arithmetic, conditions, and output. The program below asks for marks, calculates the average, and prints a result.
import java.util.Scanner;
public class MarksCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter marks for subject 1: ");
int subject1 = scanner.nextInt();
System.out.print("Enter marks for subject 2: ");
int subject2 = scanner.nextInt();
System.out.print("Enter marks for subject 3: ");
int subject3 = scanner.nextInt();
int total = subject1 + subject2 + subject3;
double average = total / 3.0;
System.out.println("Total marks: " + total);
System.out.println("Average: " + average);
if (average >= 60) {
System.out.println("Result: Pass");
} else {
System.out.println("Result: Improve and try again");
}
scanner.close();
}
}
Install only a JRE
Install a full JDK
java HelloWorld.class
java HelloWorld
HelloWorld.java.txt
HelloWorld.java
public class HelloWorld saved in hello.java
public class HelloWorld saved in HelloWorld.java
Jump directly to Spring Boot or Android
Learn Core Java syntax and OOP first
No. You can start with a text editor and terminal. An IDE is useful later, but learning <code>javac</code> and <code>java</code> first makes Java setup much easier to understand.
Java 21 LTS is a good modern default. Java 17 LTS is also fine if your course or project uses it.
<code>javac</code> compiles source code into bytecode. <code>java</code> starts the JVM and runs the compiled class.
Common causes include running the command from the wrong folder, typing the wrong class name, using <code>.class</code> in the run command, or having a package declaration that does not match the folder structure.
Yes. Learn syntax, OOP, exceptions, collections, generics, and basic file handling before moving to Spring Boot. Frameworks are much easier when Core Java is clear.
Explore 500+ free tutorials across 20+ languages and frameworks.