Nearby lessons

7 of 125

Java - Environment Setup

📌 What You Will Learn
  • How to set up Java (PATH) on your computer
  • How to write, save, compile and run your first program
  • The meaning of every word in the main method
  • The rules about class name and file name
  • How to fix the common errors a beginner faces

Environment Setup is a core concept of the Java language. This lesson explains The Six Simple Steps, Setting the PATH (Why it is needed) and If You Have More Than One Java Version with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The Six Simple Steps

Preparing any Java application takes only six steps. Once you learn these six steps, you can run every program in this book:

  • Install Java software — install the JDK (Chapter 1 explained how).
  • Select a Java editor — Notepad, Notepad++, VS Code, or any editor where you type code.
  • Write the Java program — type your code in the editor.
  • Save the Java file — save with the exact name and the .java extension.
  • Compile the Java file — use the javac command.
  • Execute (run) the Java application — use the java command.

Setting the PATH (Why it is needed)

When you type javac or java in the command prompt, the computer searches for these programs. If it cannot find them, you get an error like 'javac' is not recognized. The PATH is a list of folders where the computer searches for programs. We must add the JDK's bin folder to the PATH.

  • Right-click on This PC (or My Computer) and choose Properties.
  • Click Advanced system settings, then click Environment Variables.
  • Under System variables, find Path, select it and click Edit.
  • Click New and paste the JDK bin folder, for example C:\Program Files\Java\jdk-21\bin.
  • Click OK on all windows. Open a fresh command prompt and test with java -version.
Trainer's Note: Many students find this PATH step very confusing, but it is a one-time setup. Once set, you never need to touch it again for this machine. If you use VS Code or Eclipse, the IDE can also find Java automatically — but as a serious student, learn the command-line way first.
In simple words: PATH is just a list of folders where the computer searches for programs. Add the JDK's bin folder to it once, and then java and javac work from any folder.

If You Have More Than One Java Version

Sometimes a computer has many Java versions (Java 8, 11, 17...). A common exam question: if all of them are in the PATH, which one wins?

Answer: the one that appears first in the PATH order. The command prompt takes the first matching java it finds.

To switch between versions easily, create small batch files (.bat) — one per version:

Example03
JCode Cell
1path=C:\Java\jdk1.6.0\bin;C:\Java\jdk1.7.0\bin;C:\Java\jdk1.8.0\bin;
2 
3C:\> java -version -> shows JDK 1.6.0 (first in the list)

If You Have More Than One Java Version

Example04
JCode Cell
1// java6.bat
2set path=C:\Java\jdk1.6.0\bin;
3 
4// java7.bat
5set path=C:\Java\jdk1.7.0\bin;
6 
7// java8.bat
8set path=C:\Java\jdk1.8.0\bin;
9 
10C:\> java6.bat -> now java -version shows 1.6
11C:\> java8.bat -> now java -version shows 1.8

How to Install Java (Modern Steps)

To start your journey, you need the JDK on your computer. Here are the modern, simple steps:

  • Open a browser and search for "Java JDK download" or go to the official site adoptium.net (free builds, recommended) or oracle.com/java. For college work, download the LTS version (Java 21 or Java 17).
  • Choose the installer for your operating system (Windows / Linux / macOS).
  • Download and run the installer. Click Next, Next and Finish. Default settings are fine.
  • Open a command prompt (press Windows key, type cmd, press Enter).
  • Type java -version and press Enter. If you see a version message, Java is installed correctly.
  • Now type javac -version. This checks that the compiler is also available.
Trainer's Note: If java -version works but javac is not found, then the PATH variable is not set. In the command prompt type set PATH=%PATH%;C:\Program Files\Java\jdk-21\bin (your folder name may differ), or set it permanently in Environment Variables. Do not worry — Chapter 2 explains this in detail.
Example05
JCode Cell
1C:\> java -version
2java version "21.0.1" 2023-10-17 LTS
3 
4C:\> javac -version
5javac 21.0.1

Editor vs IDE

An editor (Notepad, Notepad++, VS Code) is software to write and save programs. An IDE (Integrated Development Environment) is a complete toolkit — editor + compiler + debugger + project management together. Famous IDEs: Eclipse, IntelliJ IDEA, NetBeans.

ToolExamplesBest for
EditorNotepad, Notepad++, VS CodeLearning, small programs
IDEEclipse, IntelliJ IDEA, NetBeansReal projects, big applications
Trainer's Note: Trainer advice: while learning, use a simple editor and the command prompt. You will understand exactly what javac and java do — this knowledge stays with you forever. Shift to an IDE once you are comfortable, because it saves time in real projects.

More Useful javac Options

The compiler javac has handy options that the classic material teaches:

The -d option is special: when your file has a package statement, -d makes the compiler create the folder structure for the package automatically.

Trainer's Note: How many .class files are created? The number of .class files equals the number of classes, abstract classes, interfaces, enums and inner classes in the file. An inner class B$C becomes a file like B$C.class.
Example07
JCode Cell
1javac FirstApp.java // compile one file
2javac -d c:\abc FirstApp.java // send the .class files to folder c:\abc
3javac *.java // compile ALL java files in this folder
4javac Employee*.java // compile files starting with Employee
5javac -d c:\abc *Account*.java // compile matching files, output to c:\abc
📝 Key Takeaways
  • Six steps: install JDK → choose editor → write program → save file → compile with javac → run with java.
  • File name must match the public class name exactly, and must end with .java.
  • javac creates a .class file (bytecode); java runs it.
  • public static void main(String[] args) is the entry point of every program.
  • Comments (//, /* */, /** */) are ignored by the compiler and are used for documentation.
  • Most beginner errors are due to PATH, folder location, or spelling mistakes.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2