Tutorials Logic, IN info@tutorialslogic.com

Java Setup Install JDK Run First Program

Java Setup Install JDK Run First Program

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.

Mental Model

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.

JDK, JRE, and JVM

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.

Which Java Version Should You 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.

  • Install JDK 21 LTS for a modern, stable setup.
  • Use JDK 17 LTS if your school or older project specifically requires it.
  • Avoid mixing multiple Java versions until you understand how PATH and JAVA_HOME work.
  • Install the JDK, not just a JRE.

Install Java on Windows, macOS, or Linux

The exact installer differs by operating system, but the goal is the same: make the java and javac commands available in your terminal.

  • Windows: Install the JDK, set JAVA_HOME to the JDK folder, and add %JAVA_HOME%\bin to PATH.
  • macOS: Install a JDK package or use a package manager, then verify that /usr/libexec/java_home can find it.
  • Linux: Install an OpenJDK package from your distribution or download a JDK archive, then update PATH if needed.
  • After installation, open a new terminal window so environment variable changes are loaded.

Verify Java Installation

Verify Java Installation
java -version
javac -version
  • java runs compiled programs on the JVM.
  • javac compiles .java source files into .class bytecode files.
  • If javac is not found, you probably installed only a runtime or PATH is not configured.

Create Your First Java File

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.

  • Use a plain text editor or IDE; do not save the file as HelloWorld.java.txt.
  • Put one public class named HelloWorld in HelloWorld.java.
  • Write the main method exactly at first, then learn what each word means.

Hello World

Hello World
// 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
    }
}
  • The class name is HelloWorld, so the file must be HelloWorld.java.
  • The main method is where execution starts.
  • System.out.println prints text and moves to the next line.

Compile and Run from the Terminal

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.

Terminal Commands

Terminal Commands
cd java-practice
javac HelloWorld.java
java HelloWorld
  • If compilation succeeds, javac usually prints nothing.
  • After compiling, you should see a HelloWorld.class file in the folder.
  • Run java HelloWorld, not java HelloWorld.class.

Understand the main Method

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.

Run a Program with Command-Line Arguments

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.

Command-Line Arguments

Command-Line Arguments
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] + "!");
    }
}

Compile and Run with an Argument

Compile and Run with an Argument
javac Greeting.java
java Greeting Anil
  • args[0] contains Anil in this example.
  • args.length tells you how many arguments were passed.

Reading User Input with Scanner

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.

Reading User Input

Reading User Input
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
    }
}
  • Import Scanner before using it.
  • Use nextLine() for full-line text input.
  • Close Scanner at the end of small console programs.

Using an IDE

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.

  • Use the terminal for your first few programs.
  • Use an IDE once you start writing multiple classes.
  • Check which JDK your IDE is using if the terminal and IDE behave differently.
  • Learn to read compiler errors instead of only clicking Run again.

Common Beginner Errors

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.

Suggested Learning Order

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.

  • Start with variables, data types, operators, input, output, and control flow.
  • Then learn methods, arrays, strings, and basic problem solving.
  • Next learn classes, objects, constructors, encapsulation, inheritance, polymorphism, and abstraction.
  • After OOP, learn exception handling, collections, generics, file handling, Java 8 features, and multithreading.
  • Practice by writing small console programs before jumping into frameworks.

Mini Project: Student Marks Calculator

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.

Student Marks Calculator

Student Marks Calculator
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();
    }
}
  • Use 3.0 instead of 3 so Java performs decimal division.
  • This project uses input, variables, arithmetic, if-else, and output.
Key Takeaways
  • Install a full JDK and verify java -version and javac -version.
  • Create Java files with names that match public class names.
  • Compile with javac FileName.java and run with java ClassName.
  • Understand the basic role of public static void main(String[] args).
  • Read compiler errors carefully; they usually point to the line and type of mistake.
Common Mistakes to Avoid
WRONG Install only a JRE
RIGHT Install a full JDK
A JRE can run Java programs, but beginners need the JDK because it includes <code>javac</code> and other development tools.
WRONG java HelloWorld.class
RIGHT java HelloWorld
Use the class name without the <code>.class</code> extension when running a compiled Java program.
WRONG HelloWorld.java.txt
RIGHT HelloWorld.java
Make sure your editor does not secretly save the file with a <code>.txt</code> extension.
WRONG public class HelloWorld saved in hello.java
RIGHT public class HelloWorld saved in HelloWorld.java
For a public class, the filename must match the class name exactly, including uppercase and lowercase letters.
WRONG Jump directly to Spring Boot or Android
RIGHT Learn Core Java syntax and OOP first
Frameworks are much easier after variables, control flow, methods, classes, objects, exceptions, and collections are clear.

Practice Tasks

  • Write a program that prints your name, city, and favorite programming language.
  • Write a program that accepts two numbers and prints their sum, difference, product, and quotient.
  • Write a program that accepts age and prints whether the user is eligible to vote.
  • Modify the marks calculator to calculate grades A, B, C, and Fail.
  • Create two Java files in the same folder and compile/run both from the terminal.

Frequently Asked Questions

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.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.