foreach-ui logo
codeLanguages
account_treeDSA

Quick Actions

quizlock Random Quiz
trending_uplock Progress
  • 1
  • 2
  • 3
  • 4
  • quiz
Java
  • Understand what Java is and why it's widely used
  • Differentiate between JVM, JRE, and JDK
  • Learn how Java's 'write once, run anywhere' principle works

What is Java?

Why does Java still matter ?
Because it solves a practical problem: write code once and run it on many machines without rewriting it for every operating system.
If you've ever shipped software that must run on servers, desktops, and embedded devices, you'll see why this design is powerful.

Imagine you built a small tool that runs on your Mac. Your colleague on Windows needs to run it too — but now you must rebuild or adapt it. Java avoids this by compiling your code into a platform-neutral format (bytecode) that any machine with a JVM can run. This is not magic; it's an engineering trade-off that makes distribution and long-term maintenance much easier.

The Java programming language was initiated in 1991 by a small team of engineers at Sun Microsystems, which was later acquired by Oracle Corporation in 2010

The simple flow (what actually happens)

  1. You write Java source files (.java).
  2. The Java compiler (javac) turns source into bytecode (.class).
  3. The Java Virtual Machine (JVM) executes that bytecode on the host platform.

This separation (source → bytecode → JVM) is what enables the "write once, run anywhere" promise.

JVM, JRE, and JDK — the three pieces explained plainly

  • JVM (Java Virtual Machine): the runtime that executes bytecode. Different OSes provide different JVM implementations, but they all understand the same bytecode.
  • JRE (Java Runtime Environment): the JVM plus the minimal libraries needed to run Java programs. If you only want to run apps, you need the JRE.
  • JDK (Java Development Kit): everything you need to develop Java programs — it includes the compiler (javac), the JRE, and development tools.

Quick analogy: JDK = full workshop (tools + workspace), JRE = the workspace to run finished tools, JVM = the machine that executes the plans.

Short runnable example — compile and run

This tiny example shows the full pipeline and is intentionally simple so you can reproduce it locally.

File: HelloWorld.java

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

Commands (terminal):

# compile
javac HelloWorld.java
# run
java HelloWorld

What happens:

  • javac produces HelloWorld.class (bytecode)
  • java launches the JVM which loads and executes that bytecode

A practical scenario — why bytecode helps

You're shipping a monitoring agent for a distributed system. Some machines run Linux, others run Windows. If you write the agent in Java and distribute the .jar file, any machine with a suitable JRE/JVM can run the same artifact — you don't need separate builds for each platform.

Common mistakes I see beginners make

  • "I compiled my .java file but get a ClassNotFoundException at runtime." — often caused by mismatched class names or package declarations. Make sure the public class name matches the filename and you run from the right directory.

  • "I used javac then tried java on the .class file name with .class extension and it failed." — you must run java HelloWorld without the .class extension.

  • "I installed a JDK but the java command is missing." — likely a PATH problem. Verify java -version and javac -version.

I see beginners struggle with classpaths and packages — it's confusing at first because the runtime search path doesn't match where you think files live.

I see beginners also assume Java is slow; modern JVMs use JIT compilation and optimizations that make long-running Java services very competitive.

Example errors and how to interpret them

  1. NoClassDefFoundError / ClassNotFoundException

    What it often means: the JVM couldn't find the class on the runtime classpath. Fix: check your -cp or run location and ensure your .class or .jar is where it should be.

  2. "javac: command not found"

    Meaning: JDK not installed or PATH not set. Fix: install a JDK distribution (OpenJDK, Temurin, etc.) and add its bin to your PATH.

  3. "Exception in thread "main" java.lang.NoSuchMethodError: main"

    Meaning: your public static void main(String[] args) method is missing or has the wrong signature.

Wrong way vs right way — a small example

Wrong: class name and filename mismatch

// in file Foo.java
public class Bar {
  public static void main(String[] args) { }
}

This will compile oddly or fail to run because public class must match the filename. Right: make the filename Bar.java or change the class to public class Foo.

What you should do right now (concrete next steps)

  1. Install a JDK (Temurin or OpenJDK). Quick commands for macOS / Linux (example using SDKMAN or package manager):

    # macOS with Homebrew
    brew install temurin
    # verify
    java -version
    javac -version
    
  2. Create HelloWorld.java and run the example above.

  3. Experiment: create a class in a package and compile/run it from the command line to see how packages map to directories.

  4. Read the next lesson: "Setting up your development environment" which walks through IDEs (VS Code, IntelliJ), PATH issues, and building a simple project.

Best practices (brief)

  • Keep public classes in files matching their names.
  • Use build tools (Maven/Gradle) for anything beyond small examples — they handle classpaths and dependencies for you.
  • For production services, use a recent JDK and test with the same JRE/JDK you deploy to.

Key takeaways

  • Java uses a two-step model: compilation to bytecode, then execution by a JVM.
  • JDK is for development; JRE is for running; JVM is the runtime engine.
  • Common issues are PATH problems, classpath errors, and wrong filenames vs public class names.
  • Use build tools early to avoid classpath confusion; practice with small examples to learn the rules.

© 2026 forEach. All rights reserved.

Privacy Policy•Terms of Service