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:
And on Windows, you’ll need to do a fourth thing:
(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.
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.
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”.
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.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.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.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.
javac -version
at the prompt.You should see a response like javac 1.8.0_102
.
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.
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.
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.
You do not need to download the “Demos and Samples”.
jdk-8u102-macosx-x64.dmg
to install it.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.
javac -version
at the prompt.You should see a response like javac 1.8.0_102
.
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.
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.
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.
If it’s not installed on your Linux distro, use your package manager to install it.
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.
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.
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.)
.profile
.
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”.
javac -version
at the prompt.You should see a response like javac 1.8.0_91
.
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.
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.
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