In Chapter 1 you will run your first Java program by typing one line in
a terminal: java Hello.java. That line works only if the terminal is
in the folder where Hello.java is saved. So before any Java, two
things need to be solid: how files and folders are named, and how to
move between folders in a window where you type instead of click.
A file is one saved thing: a photo, a song, a page of notes. Its
name usually ends in a dot and a few letters, like .jpg or .txt.
That ending is called the extension, and it tells you what kind of
file it is. Java source files end in .java. The ending is part of the
name, so Hello.java and Hello.txt are two separate files. Java's
tools only accept the one ending in .java.
A folder is a container for files. Folders can hold other folders, like boxes packed inside boxes. Some tools say directory instead; it means the same thing.
A path is a file's address, written as the list of folders you open
to reach it. Tomas keeps his Java work on a Mac at
/Users/tomas/java-course/ch01/Hello.java. Read it left to right: the
Users folder, then Tomas's folder, then java-course, then ch01, and
there is the file. Macs and Linux put a forward slash (/) between the
names. Windows uses a backslash (\) and starts with a drive letter,
as in C:\Users\tomas\....
A full address like that is absolute. It works from anywhere, like
a complete postal address. A short address, like ch01/Hello.java, is
relative. It's more like "second room on the left": it only makes
sense once you know where you're standing.
The folder you're standing in is called the working directory. Two
dots (..) mean "the folder one level up." A wavy line (~) means
"my home folder," the one with your user name on it.
A file is a named block of data stored on a disk. Most file names
end in an extension: a dot followed by a few letters, such as
.java, .txt, or .class, which tells programs and people what kind
of content the file holds. The extension is part of the name, so
Hello.java and Hello.txt are two different files that can sit side
by side. Java tools rely on it: the Java compiler accepts Hello.java
and rejects the same text saved as Hello.txt.
A folder (also called a directory) holds files and other
folders. Because folders nest, everything on a disk forms one tree. A
path is the address of one file or folder in that tree: the folder
names you pass through to reach it, joined by a separator. macOS writes
/Users/tomas/java-course/ch01/Hello.java, and Linux writes
/home/tomas/java-course/ch01/Hello.java. Windows uses backslashes and
a drive letter: C:\Users\tomas\java-course\ch01\Hello.java. Those are absolute paths: they start at the top of
the tree (or at a drive), so they name the same place no matter where
you are.
A relative path, such as ch01/Hello.java, has no starting point of
its own. It is read from the working directory, the folder a
program or a terminal is "in" at that moment. Three short names help:
. is the working directory itself, .. is the folder that contains
it, and ~ is your home folder, the personal folder every account has.
Works anywhere, or depends where you are?
Sort each path: does it name the same place from any folder, or does its meaning depend on the working directory?
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
/ or a drive letter like C:\) or at your home folder (~) names one fixed place. ch01/Hello.java, Hello.java, .., and ../ch00 are all read from whatever folder you are in.Worked example
Following a relative path
-
1
Tomas's working directory is
/Users/tomas/java-course. He asks forch01/Hello.java. -
2
A relative path is read from the working directory, so join the two with a slash:
/Users/tomas/java-course+/+ch01/Hello.javagives/Users/tomas/java-course/ch01/Hello.java. The file is there. -
3
Now he moves into
ch01and asks forch01/Hello.javaagain. Joined, that is/Users/tomas/java-course/ch01/ch01/Hello.java. There is noch01folder insidech01, so the file is not found. -
4
The words were the same; the starting point moved. From inside
ch01the right relative path is justHello.java. The absolute path would have worked from both places.
Worked example
Going up with ..
-
1
Tomas is in
/Users/tomas/java-course/ch01and wants thech00folder, which sits besidech01, not inside it. -
2
..means one folder up. Fromch01, that isjava-course. -
3
So
../ch00reads as: up tojava-course, then down intoch00. It lands on/Users/tomas/java-course/ch00. -
4
On Windows the same trip is usually written
..\ch00. PowerShell, the Windows terminal program, also accepts forward slashes, so../ch00works there too.
Check your understanding (counts toward your progress)
On Windows, what separates the folder names in a path such as C:\Users\maya\java-course?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Forward slashes are what macOS and Linux use. PowerShell accepts them, but Windows itself writes backslashes.
A dot separates a file's name from its extension.
A space would split the path into pieces when typed as a command.
Why
Windows writes paths with a drive letter and backslashes. macOS and Linux start at / and use forward slashes.
What does .. mean in a path?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That's a single dot, ..
Your home folder is written ~.
The top of the tree is a leading / on macOS and Linux, or a drive letter on Windows.
Why
.. is the folder that contains the one you are in: one level up.
Sort each path: does it name the same place from any folder, or does it depend on the working directory?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
A leading /, a drive letter such as D:\, or a leading ~ fixes the starting point. ch02/Notes.java, ../ch01, and Report.java are read from wherever you are.
Correct answer
Also accepted:
Why
Does the path start from the top of the tree, a drive letter, or your home folder?
Maya's working directory is /home/maya/java-course/ch02/drafts. How many times must she type cd .. to reach /home/maya? Type the number.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
drafts to ch02 is one, ch02 to java-course is two, java-course to maya is three.
Correct answer
Also accepted:
Why
Each cd .. removes the last folder name from the path.
Practice: Files, folders, and paths (5 problems · doesn’t affect your progress)
What is the difference between an absolute path and a relative path?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Length is a side effect. /a is absolute and short; what matters is where it starts.
Either kind can name a file or a folder.
macOS and Linux use absolute paths starting with /.
Why
An absolute path names the same place from anywhere. A relative path depends on which folder you are in.
Absolute starts at a fixed place; relative starts where you are
Which of these is a relative path?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
It starts with /, the top of the tree, so it's absolute.
A drive letter fixes the starting point, so it's absolute.
~ always means your home folder, so the starting point is fixed.
Why
labs/Timer.java has no fixed starting point. It is read from the working directory.
labs/Timer.java
Ravi's working directory is /home/ravi/labs. Type the full path, starting with /, that week2/Timer.java leads to from there.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
Join the working directory and the relative path: /home/ravi/labs + / + week2/Timer.java.
Correct answer
Also accepted:
Why
Ravi is in /home/ravi/labs/week2. Type a relative path that names /home/ravi/labs/week1/Alarm.java.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
.. climbs from week2 to labs, then week1/Alarm.java goes down to the file: ../week1/Alarm.java.
Correct answer
Also accepted:
Why
week1 sits beside week2, not inside it. Go up one level first.
Ravi's instructions say the file is at week2/Timer.java. He types that path while already inside week2 and gets "not found". Type the relative path that works from where he is.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
A relative path is read from where you are. From inside week2, week2/Timer.java means week2/week2/Timer.java, which doesn't exist. Just Timer.java is right.
Correct answer
Also accepted:
Why
Open a terminal and move around
The terminal is a window where you give the computer instructions by typing them. Each instruction is one line, called a command. You type it, press Enter, and the computer does it. The program inside the window that reads your typing is called the shell.
Every system has one. Windows has an app called Windows Terminal, and the shell inside it is PowerShell. A Mac has an app called Terminal. Linux desktops have one too, usually with "Terminal" in its name. They look a little different, but they work the same way.
A command starts with a verb. Words after it say what to act on; those
extra words are called arguments. In mkdir ch01, the verb is
mkdir, short for "make directory." The argument ch01 is the name of
the new folder.
The terminal is always standing in one folder, its working directory. A brand-new terminal stands in your home folder. Four commands do almost everything you need at first:
pwdanswers "where am I?" by printing the folder's full address. The letters stand for "print working directory."lsanswers "what's in here?" by listing files and folders. On Windows,dirdoes the same job.mkdirmakes a new folder.cdmeans "change directory." It moves you into another folder.cd ..moves up one level, andcd ~takes you home.
One surprise: when a command works, it often prints nothing at all. Silence means success. When something goes wrong, you get a message.
A terminal is a window where you work with the computer by typing
instead of clicking. Inside it runs a shell, a program that reads
each line you type, carries it out, and prints any result. Each line is
a command: a command name, then (separated by spaces) any
arguments, the extra words that tell the command what to act on. In
mkdir ch01, mkdir is the command and ch01 is its argument. The
default shells are PowerShell on Windows (inside the Windows Terminal
app), zsh on macOS, and usually bash on Linux.
Four commands cover what this course needs at first, and all four work in all three shells:
pwdprints the working directory as an absolute path.lslists what the working directory contains (PowerShell also acceptsdir).mkdir namemakes a new folder inside the working directory.cd pathchanges the working directory topath, absolute or relative;cd ..moves up one level andcd ~goes home.
A new terminal starts in your home folder. Commands that succeed often print nothing; a command that fails prints an error message. Nothing a terminal does is hidden: it acts on the same files and folders your file browser shows.
Press the Start key, type Terminal, and press Enter. Windows Terminal opens with a PowerShell prompt. (On an older Windows 10 machine without it, search for PowerShell instead.) Type these one at a time, pressing Enter after each:
cd ~
mkdir java-course
cd java-course
pwd
~ is your home folder, C:\Users\<your name>, so the path pwd
prints should end in \java-course. PowerShell's mkdir prints a
short table describing the folder it made. That is normal output, not
an error.
Press Cmd+Space, type Terminal, and press Enter. Type these one at a time, pressing Enter after each:
cd ~
mkdir java-course
cd java-course
pwd
~ is your home folder, /Users/<your name>, so the path pwd prints
should end in /java-course.
Open your applications menu and search for Terminal (on many desktops Ctrl+Alt+T opens one directly). Type these one at a time, pressing Enter after each:
cd ~
mkdir java-course
cd java-course
pwd
~ is your home folder, /home/<your name>, so the path pwd prints
should end in /java-course.
On any system, ls now prints nothing, because the new folder is
empty. In Chapter 1 you'll make a ch01 folder inside it, save
Hello.java there, and ls will list it.
Which question does each command answer?
Sort each command by the job it does.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
pwd reports where you are. ls and dir list what is there. cd with any path, including .. and ~, moves you. mkdir makes a folder and leaves you where you were.Worked example
Where am I, and what's here?
-
1
Open a new terminal and type
pwd. It prints your home folder, because every new terminal starts there. -
2
Type
cd java-course. Nothing is printed, which means it worked. Typepwdagain: the path now ends injava-course. -
3
Type
mkdir ch01, thenls. The listing showsch01.mkdirmade the folder but did not move you into it, sopwdstill ends injava-course. -
4
Type
cd ch01and thenpwd: now you are inside.cd ..takes you back up tojava-course.
Worked example
When cd can't find the folder
-
1
Tomas opens a new terminal and types
cd ch01straight away. Instead of silence he gets an error saying the folder can't be found. (The exact wording differs between PowerShell, zsh, and bash.) -
2
ch01is a relative path, so it is read from the working directory. A new terminal starts in the home folder, andch01is not in the home folder; it is insidejava-course. -
3
Two ways out:
cd java-courseand thencd ch01, or the whole route from home in one command,cd java-course/ch01. -
4
When
cdfails, runpwdandlsbefore trying anything else. They show where you really are and what is really there.
Set up a folder for Chapter 1
Put the commands in order: start from your home folder, go into java-course, make a folder called ch01 there, go into it, and check where you ended up.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
cd ~ puts you at home. You have to be inside java-course before mkdir ch01, so the new folder lands there. cd ch01 enters it, and pwd confirms the path ends in java-course/ch01.Professional habit: name folders and files without spaces, as in
java-course or GradeReport.java. A space separates a command's
arguments, so a folder called java course has to be typed in quotes,
cd "java course", every time.
Where this goes next
Chapter 1 §4 has you open this same terminal and type java -version
to check that Java installed correctly. Chapter 1 §5 runs
java Hello.java from inside your ch01 folder. It finds the file only
because Hello.java is a relative path, read from the working
directory. Chapter 1 §6 runs two commands in that folder. Then ls
shows the new file the first one made.
Check your understanding (counts toward your progress)
Sort each word by the job it does in the command shown with it: is it the command, or an argument?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
A command line starts with the command's name; the words after it are arguments that say what to act on. mkdir, cd, and pwd are commands. ch03, java-course, and .. are arguments. pwd needs no argument at all.
Correct answer
Also accepted:
Why
The first word of a line is the one the shell runs.
Which of these is a terminal?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That's a source file, something you open in an editor.
Java is installed in a folder, but you reach it by typing in a terminal.
Error messages can appear in a terminal, but the terminal is the window itself.
Why
A terminal is a text window: you type a command, press Enter, and the computer carries it out and prints any result.
Think of where you typed pwd and cd in this section.
Which command lists the files in the working directory, on Windows, macOS, and Linux alike?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
pwd prints where you are, not what is there.
cd moves you; it lists nothing.
mkdir makes a folder and lists nothing.
Why
ls lists files and folders in all three shells. PowerShell on Windows also accepts dir.
You want a listing of contents, not a location.
Someone named a folder java course, with a space in the middle. Complete the command so it moves into that folder.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
The shell splits a command at spaces, so cd java course looks like two arguments. Quotes keep the name together: cd "java course". Having to do that every time is why programmers name folders like java-course.
Correct answer
Also accepted:
Why
Typed bare, java course looks to the shell like two separate arguments. How do you keep it as one?
Practice: Move around in the terminal (5 problems · doesn’t affect your progress)
What is the working directory of a terminal?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Java's location doesn't decide where the terminal is.
Where the app is installed has no effect on which folder you're working in.
A new terminal starts at home, but cd changes that.
Why
The working directory is where the terminal currently stands. pwd prints it and cd changes it.
The folder the terminal is in now
You want to see what's inside the folder you're in. Which command?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
pwd says where you are, not what's there.
mkdir makes a folder; it doesn't list anything.
cd ~ takes you home. It doesn't list anything.
Why
ls lists the files and folders in the working directory. dir also works in PowerShell.
ls
You're in your home folder. Go into java-course, make a folder ch00 there, enter it, and check where you are.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
Enter java-course first so ch00 is made there, then enter it and confirm with pwd.
Correct answer
Also accepted:
Why
You're in java-course/ch01 and want to go up one level. Type the command.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
cd .. moves the terminal to the folder that contains the one you're in, here java-course.
Correct answer
Also accepted:
Why
.. means one folder up.
You're in java-course/ch01. Get to a new folder java-course/ch02 and confirm. Put the commands in order. One command doesn't belong.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Why
cd .. puts you in java-course, where ch02 belongs. cd ~ would take you home, and mkdir ch02 there would make the folder in the wrong place.
Correct answer
Also accepted:
Why
Where should you be standing when you make ch02?