Learn Java the Hard Way (Second Edition)

Exercise 0: The Setup

This exercise has no code but do not skip it. It will help you to get a decent text editor installed and to install the Java Development Kit (JDK). If you do not do both of these things, you will not be able to do any of the other exercises in the book. You should follow these instructions as exactly as possible.

You are going to need to do three things no matter what kind of system you have:

  1. Install a decent text editor for writing code.
  2. Figure out how to open a terminal window so we can type commands.
  3. Install the JDK (Java Development Kit).

    And on Windows, you’ll need to do a fourth thing:

  4. Add the JDK to the system PATH.

(The JDK commands are automatically added to the PATH on Apple computers and on Linux computers.)

I have instructions below for Windows, then for the Mac OS, and finally for Linux. Skip down to the operating system you prefer.

 

Windows

Installing a Decent Text Editor (Notepad++)

  1. Go to notepad-plus-plus.org with your web browser, download the latest version of the Notepad++ text editor, and install it. You do not need to be an administrator to do this.
  2. Once Notepad++ is installed, I always run it and turn off Auto-Completion since it is bad for beginners. (It also annoys me personally.) Open the “Settings” menu and choose “Preferences”. Then click on “Auto-Completion” about halfway down the list on the left-hand side. Finally uncheck the box next to “Enable auto-completion on each input” and then click the “Close” button.
  3. Finally while Notepad++ is still running I right-click on the Notepad++ button down in the Windows taskbar area and then click “Pin this program to taskbar.” This will make it easier to launch Notepad++ for future coding sessions.

Opening a Terminal Window (PowerShell)

  1. Click the Start button to open the Start Menu. (On Windows 8 and newer, you can open the search box directly by pressing the Windows key + S.) Start typing “powershell” in the search box.
  2. Choose “Windows PowerShell” from the list of results.
  3. Right-click on the PowerShell button in the taskbar and choose “Pin this program to taskbar.”
  4. In the Powershell/Terminal window, type
[:::]

You will probably get an error in red text that says something like “The term ‘javac’ is not recognized as the name of a cmdlet….”

This just means that the JDK isn’t installed and added to the PATH, which is what we expect at this point.

Installing the Java Development Kit (JDK)

  1. Go to Oracle’s Java SE downloads page with your web browser.
  2. Click the big “Java” button on the left near the top to download the Java Platform (JDK) 8u102. Clicking this will take you to a different page titled “Java SE Development Kit 8 Downloads.”
  3. On this page you will have to accept the license agreement and then choose the “Windows x86” version near the bottom of the list. Download the file for version 8u102 or any newer version.

    If you know for sure that you are running a 64-bit version of Windows, it is okay to download the “Windows x64” version of the JDK. If you’re not sure, then you should download the “x86” (a.k.a. 32-bit) version, since that version will work on both 32-bit Windows and on 64-bit Windows.

    You do not need to download the “Demos and Samples”.

  4. Once downloaded, run jdk-8u102-windows-i586.exe to install it. After you click “Next >” the very first time you will see a screen that says Install to: C:\Program Files (x86)\Java\jdk1.8.0_102\ or something similar. Make a note of this location; you will need it soon.
  5. Just keep clicking “Next” until everything is done. Unless you really know what you’re doing it’s probably best to just let the installer do what it wants.

Adding the JDK to the PATH

  1. Now that the JDK is installed you will need to find out the exact name of the folder where it was installed. Look on the C: drive inside the Program Files folder or the C:\Program Files (x86) folder if you have one. You are looking for a folder called Java. Inside that is a folder called jdk1.8.0_102 that has a folder called bin inside it. The folder name must have jdk1.8 in it; jre8 is not the same. Make sure there’s a bin folder.
  2. Once you have clicked your way inside the bin folder, you can left-click up in the folder location and it will change to something that looks like C:\Program Files (x86)\Java\jdk1.8.0_102\bin. You can write this down or highlight and right-click to copy it to the clipboard.
  3. Once the JDK is installed and you know this location open up your terminal window (PowerShell). In PowerShell, type this:
[Environment]::SetEnvironmentVariable("Path",
      "$env:Path;C:\Program Files (x86)\Java\jdk1.8.0_102\bin", "User")

Put it all on one line, though. That is:

Type or paste [Environment]::SetEnvironmentVariable("Path", "$env:Path;

Don’t press ENTER yet. You can paste into PowerShell by right-clicking.

Then type or paste the folder location from above. If you installed the x86 (32-bit) version of JDK version 8u102, it should be

C:\Program Files (x86)\Java\jdk1.8.0_102\bin

(Still don’t press ENTER.)

Then add ", "User") at the end. Finally, press ENTER.

If you get an error then you typed something incorrectly. You can press the up arrow to get it back and the left and right arrows to find and fix your mistake, then press ENTER again.

Once the SetEnvironmentVariable command completes without giving you an error, close the PowerShell window by typing exit at the prompt. If you don’t close the PowerShell window the change you just made won’t take effect.

Making Sure the JDK is Installed Correctly

  1. Launch PowerShell again.
  2. Type javac -version at the prompt.
[:::]

You should see a response like javac 1.8.0_102.

  1. Type java -version at the prompt.
[:::]

You should see a response like java version "1.8.0_102".

Make sure they both report the same version number! If not, you might have two different (incompatible) versions of Java installed and you will have trouble completing the exercises in this book.

If the version numbers don’t match, go into the Control Panel and Add/Remove Programs. Remove all programs related to Java, the JDK, the JRE. Remove Eclipse if it is installed. Remove everything Java related and start again.

However, if both commands worked and you didn’t get any errors and the version numbers did match, then congratulations! Getting all that to work is pretty hard and you probably have what it takes to finish the rest of the book!

Don’t move on to the next exercise yet, though! We should get used to navigating using the command-line, since that’s how you will be running the programs you write in this book.

Navigation in the Command-Line (PowerShell)

You should create a new folder to put all your code in. After finishing this book, you will have at least 100 new files and it will be better if they are all in one place.

[:::]

Type ls then press ENTER. (That’s an “L” as in “list”.) This command will list the contents of the current folder/directory.

[:::]

The cd command means “change directory” and it will move you into the “Documents” folder so that future commands will take effect there.

Notice that your prompt will change to show that you are now inside a new folder. For example, when I first open PowerShell on my Windows 7 machine, my prompt is

PS C:\Users\Mitchell>

Then once I change into the “Documents” directory the prompt changes to

PS C:\Users\Mitchell\Documents>

You should type ls again once you get in there to see the contents of your Documents directory.

[:::]

mkdir means “make directory” and will create a new folder in the current location. Typing ls afterward should show you that the new directory is now there. You can call the folder something different than “javahard” if you want to. You will only need to create this folder once per computer.

[:::]

Change into the javahard folder. Afterward, type ls and it should list nothing. (The directory is empty, after all.)

[:::]

This is how you use the cd command to back out one level. After you type it you will be back in just the “Documents” directory, and your prompt should have changed to reflect that.

Issue the command to get back into the “javahard” folder again.

Now, use the mouse to open the text editor you installed earlier. Type a sentence or something and then save the file as test.txt. Save it into the “javahard” folder you just created.

Go back to the terminal window and issue the ls command to see the file you just created.

If you’re feeling fancy, you won’t have to use the mouse to switch back to the terminal; you can just press Alt + Tab on Windows or Linux or press Command + Tab on a Mac to switch applications.

Press and hold the Alt key. Keep it pressed. Then press and release the Tab key a single time. While still holding the Alt key, press Tab several more times until your terminal window is selected, then let go of the Alt key to make the switch.

If you just quickly press Alt+Tab and let go of both keys right away, it usually takes you back to the previous application. I do this a lot. When I’m in the text editor I press Alt+Tab to get back to the terminal, then when I’m done in the terminal I press Alt+Tab again to get back to my text editor. It’s very fast once you get used to it.

You should skip down to the bottom of this chapter and read the “Warnings for Beginners”, but otherwise you’re done with the setup and you are ready to begin Exercise 01 on Windows! Nice job.

 

Mac OS X

Installing a Decent Text Editor (TextWrangler)

  1. Go to barebones.com with your web browser. Download the Disk Image for TextWrangler version 5.0 or any newer version.
  2. Run the disk image, then open the Appliciations Folder and drag the icon over to it as indicated. You may have to authenticate with the administrator username and password.
  3. Once installed, launch TextWrangler and add it to the dock if that doesn’t happen automatically.

Opening a Terminal Window (Terminal)

  1. Minimize TextWrangler and switch to Finder. Using the search (Spotlight), start searching for “terminal”. That will open a little bash terminal.
  2. Put your Terminal in your dock as well.
  3. In Terminal window, type
[:::]

You should probably get an error that tells you that “javac” is an unknown command. (Feel free to email me a screenshot of the error message so I can update this paragraph.)

This just means that the JDK isn’t installed, which is what we expect at this point.

Installing the Java Development Kit (JDK)

  1. Go to Oracle’s Java SE downloads page with your web browser.
  2. Click the big “Java” button on the left near the top to download the Java Platform (JDK) 8u102. Clicking this will take you to a different page titled “Java SE Development Kit 8 Downloads.”
  3. On this page you will have to accept the license agreement and then choose the “Mac OS X x64” version in the middle of the list. Download the file for version 8u102 or any newer version.

    You do not need to download the “Demos and Samples”.

  4. Once downloaded, run jdk-8u102-macosx-x64.dmg to install it.
  5. Just keep clicking “Next” until everything is done. Unless you really know what you’re doing it’s probably best to just let the installer do what it wants.

Adding the JDK to the PATH

You get to skip this part, because the JDK installer does this for you on Apple computers. You might need to close the terminal and open it again, though, for the change to take effect.

Making Sure the JDK is Installed Correctly

  1. Launch Terminal again.
  2. Type javac -version at the prompt.
[:::]

You should see a response like javac 1.8.0_102.

  1. Type java -version at the prompt.
[:::]

You should see a response like java version "1.8.0_102".

Make sure they both report the same version number! If not, you might have two different (incompatible) versions of Java installed and you will have trouble completing the exercises in this book.

If the version numbers don’t match, uninstall all programs related to Java, the JDK, the JRE. Remove Eclipse if it is installed. Remove everything Java related and start again.

However, if both commands worked and you didn’t get any errors and the version numbers did match, then congratulations! Getting all that to work is pretty hard and you probably have what it takes to finish the rest of the book!

Don’t move on to the next exercise yet, though! We should get used to navigating using the command-line, since that’s how you will be running the programs you write in this book.

Navigation in the Command-Line (Terminal)

You should create a new folder to put all your code in. After finishing this book, you will have at least 100 new files and it will be better if they are all in one place.

[:::]

Type ls then press ENTER. (That’s an “L” as in “list”.) This command will list the contents of the current folder/directory.

[:::]

The cd command means “change directory” and it will move you into the “Documents” folder so that future commands will take effect there.

Notice that your prompt will change to show that you are now inside a new folder. For example, when I first open Terminal, my prompt is

localhost:~ mitchell$

Then once I change into the “Documents” directory the prompt changes to

localhost:Documents mitchell$

You should type ls again once you get in there to see the contents of your Documents directory. Now we are ready to create the folder.

[:::]

mkdir means “make directory” and will create a new folder in the current location. Typing ls afterward should show you that the new directory is now there. You can call the folder something different than “javahard” if you want to. You will only need to create this folder once per computer.

[:::]

Change into the javahard folder. Afterward, type ls and it should list nothing. (The directory is empty, after all.)

[:::]

This is how you use the cd command to back out one level. After you type it you will be back in just the “Documents” directory, and your prompt should have changed to reflect that.

Issue the command to get back into the “javahard” folder again.

Now, use the mouse to open the text editor you installed earlier. Type a sentence or something and then save the file as test.txt. Save it into the “javahard” folder you just created.

Go back to the terminal window and issue the ls command to see the file you just created.

If you’re feeling fancy, you won’t have to use the mouse to switch back to the terminal; you can just press Command + Tab on a Mac or press Alt + Tab on Windows or Linux to switch applications.

Press and hold the Command key. Keep it pressed. Then press and release the Tab key a single time. While still holding the Command key, press Tab several more times until your terminal window is selected, then let go of the Command key to make the switch.

If you just quickly press Command+Tab and let go of both keys right away, it usually takes you back to the previous application. I do this a lot. When I’m in the text editor I press Command+Tab to get back to the terminal, then when I’m done in the terminal I press Command+Tab again to get back to my text editor. It’s very fast once you get used to it.

You should skip down to the bottom of this chapter and read the “Warnings for Beginners”, but otherwise you’re done with the setup and you are ready to begin Exercise 01 on Mac OS X! Nice job.

 

Linux

There are a lot of different versions of Linux out there, so I am going to give instructions for the latest version of Ubuntu. If you are running something else, you probably know what you are doing well enough to figure out how to modify the directions for your setup.

Installing a Decent Text Editor (gedit)

  1. On Ubuntu, gedit is already installed by default. It’s called “Text Editor”. If you search for it in the Dash, you’ll find it with “gedit” or “text”.

    If it’s not installed on your Linux distro, use your package manager to install it.

  2. Make sure you can get to it easily by right-clicking on its icon in the Launcher bar and selecting “Lock to Launcher”.
  3. Run gedit so we can change some of the defaults to be better for programmers:
    1. In the menu bar, open the “Edit” menu then choose “Preferences”.
    2. In the “View” tab, put a check mark next to “Display line numbers”
    3. Make sure there’s not a check mark next to “Enable text wrapping”
    4. Switch to the “Editor” tab and change Tab width: to 4.
    5. Put a check mark next to “Enable automatic indentation”

Opening a Terminal Window (Terminal)

  1. Minimize your text editor and search for “Terminal” in the Dash. Other Linux distributions may call it “GNOME Terminal”, “Konsole” or “xterm”. Any of these ought to work.
  2. Lock the Terminal to the Launcher bar as well.
  3. In Terminal window, type
[:::]

You should get an error message that says “The program ‘javac’ can be found in the following packages” followed by a list of packages.

This just means that the JDK isn’t installed, which is what we expect at this point.

Installing the Java Development Kit (JDK)

  1. One of the nice things about Linux is the package manager. You can manually install Oracle’s “normal” version of Java if you want, but I always just use the OpenJDK release:
[:::]

That’s pretty much it. Everything in this book works fine using OpenJDK. (In fact, I use Linux for most of my day-to-day work and the exercises in this book were actually written and tested using OpenJDK!)

If, however, you’re determined to have to install something like Windows and Mac users have to, you can download it from Oracle’s Java SE downloads page.

You’re on your own for installing it, though. Seriously. Just use the version provided by your package manager.

Adding the JDK to the PATH

You get to skip this part, because this is already done for you on Linux computers. You might need to close the terminal and open it again, though, for the change to take effect.

However, on my computer running any Java tool prints an annoying message to the terminal window:

Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar

This is because Eclipse doesn’t work right without this JAR file. But we aren’t going to be using Eclipse, and this message annoys me, so you need to add a line to the end of a hidden file called .profile. (The filename starts with a dot/period, which is why it’s hidden.)

  1. Launch your text editor. Click “Open”.
  2. Make sure you’re in the “Home” directory.
  3. Right-click anywhere in the “Open” window and put a checkmark next to “Show Hidden Files”.
  4. Open the file called .profile.
  5. Add the following line at the bottom of the file:

unset JAVA_TOOL_OPTIONS

Save the file and close it. You might want to click “Open” again and remove the checkmark next to “Show Hidden Files”.

Making Sure the JDK is Installed Correctly

  1. Launch Terminal again.
  2. Type javac -version at the prompt.
[:::]

You should see a response like javac 1.8.0_91.

  1. Type java -version at the prompt.
[:::]

You should see a response like openjdk version "1.8.0_91".

Make sure they both report the same version number! If not, you might have two different (incompatible) versions of Java installed and you will have trouble completing the exercises in this book.

If the version numbers don’t match, uninstall all programs related to Java, the JDK, the JRE. Remove Eclipse if it is installed. Remove everything Java related and start again.

However, if both commands worked and you didn’t get any errors and the version numbers did match, then congratulations! Getting all that to work is pretty hard and you probably have what it takes to finish the rest of the book!

Don’t move on to the next exercise yet, though! We should get used to navigating using the command-line, since that’s how you will be running the programs you write in this book.

Navigation in the Command-Line (Terminal)

You should create a new folder to put all your code in. After finishing this book, you will have at least 100 new files and it will be better if they are all in one place.

[:::]

Type ls then press ENTER. (That’s an “L” as in “list”.) This command will list the contents of the current folder/directory.

[:::]

The cd command means “change directory” and it will move you into the “Documents” folder so that future commands will take effect there.

Notice that your prompt will change to show that you are now inside a new folder. For example, when I first open Terminal, my prompt is

mitchell@localhost:~$

Then once I change into the “Documents” directory the prompt changes to

mitchell@localhost:~/Documents$

You should type ls again once you get in there to see the contents of your Documents directory. Now we are ready to create the folder.

[:::]

mkdir means “make directory” and will create a new folder in the current location. Typing ls afterward should show you that the new directory is now there. You can call the folder something different than “javahard” if you want to. You will only need to create this folder once per computer.

[:::]

Change into the javahard folder. Afterward, type ls and it should list nothing. (The directory is empty, after all.)

[:::]

This is how you use the cd command to back out one level. After you type it you will be back in just the “Documents” directory, and your prompt should have changed to reflect that.

Issue the command to get back into the “javahard” folder again.

Now, use the mouse to open the text editor you installed earlier. Type a sentence or something and then save the file as test.txt. Save it into the “javahard” folder you just created.

Go back to the terminal window and issue the ls command to see the file you just created.

If you’re feeling fancy, you won’t have to use the mouse to switch back to the terminal; you can just press Alt + Tab on Windows or Linux or press Command + Tab on a Mac to switch applications.

Press and hold the Alt key. Keep it pressed. Then press and release the Tab key a single time. While still holding the Alt key, press Tab several more times until your terminal window is selected, then let go of the Alt key to make the switch.

If you just quickly press Alt+Tab and let go of both keys right away, it usually takes you back to the previous application. I do this a lot. When I’m in the text editor I press Alt+Tab to get back to the terminal, then when I’m done in the terminal I press Alt+Tab again to get back to my text editor. It’s very fast once you get used to it.

You should read the “Warnings for Beginners” below, but otherwise you’re done with the setup and you are ready to begin Exercise 01 on Linux! Nice job.

Warnings for Beginners

You are done with the first exercise. This exercise might have been quite hard for you depending on your familiarity with your computer. If it was difficult and you didn’t finish it, go back and take the time to read and study and get through it. Programming requires careful reading and attention to detail.

If a programmer tells you to use vim or emacs or Eclipse, just say “no.” These editors are for when you are a better programmer. All you need right now is an editor that lets you put text into a file. We will use gedit, TextWrangler, or Notepad++ (from now on called “the text editor” or “a text editor”) because it is simple and the same on all computers. Professional programmers use these text editors so it’s good enough for you starting out.

A programmer will eventually tell you to use Mac OS X or Linux. If the programmer likes fonts and typography, he’ll tell you to get a Mac OS X computer. If he likes control and has a huge beard, he’ll tell you to install Linux. Again, use whatever computer you have right now that works. All you need is an editor, a terminal, and the Java Development Kit.

Finally, the purpose of this setup is so you can do three things very reliably while you work on the exercises:

Anything else will only confuse you, so stick to the plan.


“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell