Terminal Basics¶
1. What is the terminal and why learn it¶
The terminal (also called command line, command prompt, or shell) is how you give instructions to the computer using text instead of clicking icons.
For Java developers, the terminal is essential for:
- Compiling and running Java without opening an IDE
- Using Maven / Gradle to build projects
- Running servers, checking logs, managing processes
- Using Git to manage source code
You don't need to memorize hundreds of commands. This lesson focuses on the 10 most-used commands you will type every day.
2. Opening the terminal¶
Windows has two main terminals:
Command Prompt (CMD) — the classic terminal:
- Press Windows + R, type cmd, press Enter.
- Or: search "Command Prompt" in the Start Menu.
PowerShell — more modern, supports most Unix commands:
- Press Windows + X, choose Windows PowerShell or Terminal.
- Or: search "PowerShell" in the Start Menu.
Recommendation: use Windows Terminal
Install Windows Terminal from the Microsoft Store — supports tabs, a clean interface, and integrates both CMD and PowerShell. Search "Windows Terminal" in the Microsoft Store and install for free.
Terminal is built in:
- Press Cmd + Space, type Terminal, press Enter.
- Or: Finder → Applications → Utilities → Terminal.
The default shell on macOS is Zsh (since macOS Catalina).
iTerm2 — a better terminal for macOS
iTerm2 provides more features than the default Terminal: search, split panes, rich color themes. Free download at iterm2.com.
- Ubuntu:
Ctrl + Alt + T - GNOME: search "Terminal" in the launcher
- KDE: search "Konsole"
3. Understanding the prompt¶
When you open the terminal, you see a line like this:
4. Navigating directories¶
See your current directory¶
List files and directories¶
Move into a directory¶
Example — go into Documents:
Go into a nested directory:
Go up one level to the parent directory:
Go back to the home directory:
Absolute vs relative paths¶
| Type | Example | Meaning |
|---|---|---|
| Absolute | cd /Users/yourname/Documents |
Starts from the root, works from any location |
| Relative | cd Documents |
Relative to your current directory |
5. Managing files and directories¶
Create a new directory¶
Create multiple levels at once:
Delete a file¶
Copy and move files¶
6. Read file contents¶
7. Compile and run Java from the terminal¶
This is the most important part of this lesson.
Example directory structure¶
Create the Java file¶
Create the directory and navigate into it:
Use a text editor to create HelloWorld.java with this content:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The filename must match the class name
A file named HelloWorld.java must contain public class HelloWorld. A mismatch causes javac to report an error.
Compile¶
If there are no errors, this produces a HelloWorld.class file in the same directory.
Run¶
Do not add .class when running
The correct command is java HelloWorld, not java HelloWorld.class.
Output:
The full workflow¶
8. Other useful commands¶
Clear the screen¶
View command history¶
Press the ↑ key to repeat the previous command — no need to retype it.
Stop a running program¶
Press Ctrl + C to stop any currently running command (very useful when running a server).
Search inside a file¶
9. Essential command reference¶
| Action | Windows CMD | macOS / Linux |
|---|---|---|
| Show current directory | cd |
pwd |
| List contents | dir |
ls |
| Enter a directory | cd dirname |
cd dirname |
| Go up one level | cd .. |
cd .. |
| Create directory | mkdir name |
mkdir name |
| Delete file | del file.txt |
rm file.txt |
| Delete directory | rmdir /s /q name |
rm -rf name/ |
| Copy file | copy a.txt b.txt |
cp a.txt b.txt |
| Move/rename | move a.txt b.txt |
mv a.txt b.txt |
| Read file | type file.txt |
cat file.txt |
| Clear screen | cls |
clear |
| Stop program | Ctrl + C |
Ctrl + C |
| Compile Java | javac File.java |
javac File.java |
| Run Java | java ClassName |
java ClassName |
10. Tips for working efficiently in the terminal¶
- Tab to autocomplete: type the first few letters of a file or directory name and press
Tab— the terminal fills in the rest. - ↑ ↓ to browse history: no need to retype previous commands.
- Ctrl + C to exit: when any command gets stuck or runs too long — press
Ctrl + Cto stop it immediately. - Ctrl + L to clear the screen: shortcut for
clear/cls. - Drag and drop paths: drag a file or folder from Finder (macOS) or Explorer (Windows) into the terminal window — it automatically pastes the full path.
Next: Git and GitHub — learn how to save your code history and collaborate with others.