Before downloading, installing, and setting up software, there are
a few things you should do to make your environment more suitable
for programming.
Switch to a Mono-Spaced Font
You'll find as you program that it's difficult
to tell certain characters from one another, so
a good mono-spaced font
meant for programming will help. Read the notes
on Fonts and choose
a font to download and install (instructions included).
Using a mono-spaced font for coding will also be helpful
when you want to create formatted output, as all the characters
will be the same width.
Show File Extensions
For some reason, it's the default in Windows to not show file extensions.
This is a huge pain for developers because application source and bytecode files
have the same names, but different extensions. Hovering over a file will usually
tell you the file type but many editors also use unrecognized extensions for
their backups, and these might not have any meaningful information in the
hovertext. Therefore, all developers turn on the file extensions so you can
quickly differentiate between the different file types:
Use the Windows 10 search: search for "file explorer options"
You should see an entry for "File Explorer Options" and the words "Control Panel"
below it in smaller letters. Click File Explorer Options.
In the File Explorer Options dialog box, click the VIEW tab.
Look for the check box labeled "Hide extensions for known file types"
(it's about 8 or 10 items down) and make sure it's NOT checked.
Click the OK button.
Create Some Folders/Directories
First, make sure you've created a directory/folder on your
hard drive for these tutorials.
DO NOT create this directory/folder in your Program
Files/Java directory!!! In fact, you should never store
any of your own files inside any of the Program Files folders/directories.
The Program Files (and Program Files(x86)) directories have special
permissions that disallow the creation and editing of other files
except by certain applications. Saving your Java programs in this
location will cause problems: they may not run and they may not be
accessible to your editor or other prorgrams you create.
Nor should you ever store your files directly on the desktop: The items
on the desktop are not included in a Windows System Restore, so if you ever
have to restore your computer after something bad happens, you'll lose all the
files stored on the desktop. Most file backup applictions also ignore the desktop
files and won't back these up for you.
A good place to store your Java files is in your Documents
directory/folder: create a sub-folder/sub-directory with a name
that works for you (e.g. JavaTutorials or wendijava or just
"java" - whatever you like).
After creating a folder for these tutorials,
you might eventually wish to create sub-folders/sub-directories
for each lesson, or each unit. It's up to you, but make sure
you keep things organized and easy to find: there might be
lessons where you're asked to go back
and edit programs you've written before in other lessons.
Bookmark the Java Documentation
The Java API documentation is useful as you're learning about the different classes
that make up the Java language. In many lessons we'll be looking at this documentation,
so you should have it bookmarked. Your instructor or professor will let
you know which version of Java you'll be using.
Using your preferred browser, go to the appropriate link below
and bookmark it or add it to your "Favourites".
Tip: in some browsers you can add frequently used bookmarks
to one of the button bars at the top of your browser,
or to the set of favourite links that appears when
you open a new browser tab/window.
Recall in an earlier lesson we talked about what
the Java SE contains:
The Java development environment, or JDK, which is needed by programmers to write
and debug Java programs. It also contains the JRE, or Java Runtime Environment.
The JRE contains the JVM (Java Virtual Machine) and other supporting files needed
to run Java programs.
For these tutorials, we will download and install the latest
version of Java SE, but if you've
already done some development with Java and have an older version,
that's fine. However, if your current Java version is older than 11,
I strongly recommend you get the latest version today. Note that
if you have a version earlier than 15, you'll have to
perform some extra setup steps if you
want to use a basic editor like Notepad++.
But otherwise, you won't be doing anything in these tutorials that
is significantly different between the Java versions mentioned above.
Java is updated regularly, so keep in mind that the
version numbers or "Update" number you see in the examples below may
not be the same. As long as you have the
latest update, that's all that matters.
Go to the Java
SE Download Page.
The current version of Java when these notes were last updated is version
17, so that's the version that will be used here. If there is a newer version,
go ahead and get that version, instead.
Scroll down the page until you find the Java SE Development Kit Downloads
table. Select the tab for your operating system, then click the link
to download the JDK. If you're new to installing software, select an
executable installer (it has the extension .exe) - it's the easiest
to deal with.
Your browser should show the download progress
near the bottom of the window, and when it's done, you can click to
open the downloaded file. If you don't see this, go to the
browser's downloads directory and look for a file with a name similar to
"jdk-17_windows-x64_bin.exe".
Depending on the speed of the network and the number of connections,
it make take some time for the file to download. Once you are able to
open the file, the installation will begin.
Installing Java
The JDK is a collection of tools, utilities, and packages that allow you
to write programs in the Java language.
It does not come with a development environment, so
you will also need an editor or IDE, which we'll do in the
next step.
Installing the JDK
After the download has finished, run the installer executable file
if it's not already running.
This begins the installation of Java. Most of the default options in each dialog are fine,
but you're welcome to change them as long as you know what you're doing. Otherwise,
leave the various options as is or as instructed in these notes.
Otherwise, just click NEXT on each screen until you get to the last one.
When the installation is complete, you'll see the screen that indicates installation is complete.
All you need to do is click the CLOSE button.
IDE Installtion/Setup
There are so many choices for the editor/IDE you might choose
for coding Java programs. I will suggest two here, take your
pick based on the information below, but of course if you're
already more comfortable with a different editor/IDE, use it!
The two choices I have instructions for are Notepad++ and IntelliJ IDEA.
Here's some information that should help you choose:
Both choices have intellisense ("helpers" that pop up as you type)
and syntax highlighting (which makes the code easer to read).
Software
Notepad++
IntelliJ IDEA
Description
simple editor
fully featured IDE
Advantages
Doesn't have a lot of "stuff" (tabs, windows) all over the UI.
Easy to learn to use.
Has debugger, project management features, etc. for large projects.
Easy to compile/run code.
Disadvantages
Doesn't have a debugger or project management features, so not good for large,
complex projects.
If you want to compile and run using the editor, there's a tedious amount
of settup (it has to be done with a separate plugin that needs to be
configured).
If you don't install the Java plugin, you still have to drop to the
command-line to compile and run programs.
As a fully-featured IDE, there are a lot of tabs and windows and
the UI can be a bit overwhelming for new programmers.
Instructions for downloading, installing, and setting up both
Notepad++ and IntelliJ can be found below, but you might also
consider these other popular options, instead:
There are several choices for editors and IDEs (Integrated Development Environments) that
you can use. For the first Java course in your program, we're going to use a very basic
and simple-to use editor called Notepad++.
Download/Install Notepad++
These instructions will take you through the installation and setup of Notepad++.
Download the most recent Notepad++ version by going to Notepad++ Downloads and
choosing the most recent version
(the version numbers may
vary from what is shown in the screen shot).
Clicking the appropriate link will give you a download page.
Scroll down to the section titled "Download 64-bit x64" and click on the link that says
"Installer". This will prompt you to save the installer file. Put it in your Downloads
directory or some other directory where you can find it later.
Once the file has downloaded, run it. This begins the installation.
You'll first be asked to pick a language for the installation. Choose whatever language you
are most comfortable with (this is just for the installation process, it doesn't change
the language used in the editor). Then click OK.
The welcome screen appears and lets you know that you are about to configure your
installation.
Click the NEXT button.
The license agreement screen asks you to agree to the licensing for this software.
This is typical and quite common. Click I AGREE to continue.
The next screen asks you to choose a location where Notepad++ should be installed.
The default value here is fine, and you shouldn't change it unless you know what you're
doing.
Click the NEXT button.
The next screen gives you an opportunity to choose which parts of the program you want to
install or not install. The defaults here are fine, so you can leave them alone.
Click the NEXT button.
The next screen asks for a few more options. Check the box that says "Create Shortcut on
Desktop"
and leave the other box unchecked.
Click the INSTALL button to start installing Notepad++.
The last screen lets you know that the installation was completed successfully.
Make sure the box "Run Notepad++..." is checked and then click FINISH.
Notepad++ starts and the change log file is loaded. You can close this file, we don't need it for
anything
(it's only interesting to read if you've used Notepad++ before and want to know what's different
in this newest version).
Some of you may wish to change the font size right away before you do anything else, so you can
more easily read the Notepad++ screen. To edit your font size, go to the menu bar at the top
and select Settings > Style Configurator. On the right side
of the screen is a drop-down where you can change the font size. If you also decide to change
the font name, make sure you stick with a mono-spaced or
fixed-width font like Courier New.
(Personally, I prefer Ubuntu Mono or Fira Code)
Setup Notepad++
The last set of steps we need to complete today is to set up commands that will allow us to use
Notepad++
to compile and execute Java programs we write.
This isn't really mandatory, but it's very convenient, so we'll do it.
First, we need to install a special plugin in Notepad++ that allows us to run other programs from
within Notepad++.
In the menu bar at the top, select Plugins > Plugins Admin.
This opens the Plugins Admin window and shows you a list of available plugins.
To make it easier to find the one we want, type nppexec in the "Search" field.
You should see the plugin titled "NppExec" come into view.
Check it off and then click the INSTALL button.
Next, you'll receive a warning that Notepad++ needs to restart. Click YES.
Notepad++ should restart on its own. Once it restarts, go to the Plugins menu again.
There should be a new item in the Plugins menu called "NppExec".
If you don't see it, go back through the instructions above and make sure you read and
followed them correctly.
We are now going to add two simple scripts that compile and execute Java programs that we
write.
In the menu bar, select Plugins > NppExec > Execute
You should now see the Execute window. This is where you can add/modify scripts and even
execute them, if you wanted to.
The first script we'll add is the script that compiles a program.
The first command in this script will move to the current working directory where our
source code is saved and the second command will run the compiler to compile the source
code.
In the big editor area of the window, enter the following:
cd $(FULL_CURRENT_PATH)
javac $(FILE_NAME)
Make sure you add the line break between the two lines of code (press the ENTER key after
typing the first line).
The first command changes the directory to the current one where your source file has been
saved.
The second comman runs the Java compiler program (javac.exe) on your current source code
file.
This will compile your code and show you any syntax/compiliation errors.
If you have no errors, it will create the bytecode file.
Save your script by clicking the SAVE button.
In the text field that appears, type Compile and then click the SAVE button under the text
field.
The bytecode file is the file that you can execute by telling the Java Virtual
Machine (JVM) to interpret and execute each line of bytecode.
This job is done with the Java interpreter program (java.exe).
Let's now repeat the process to create a script that runs the Java interpreter program.
Use the drop-down box at the bottom of the Execute window and select "<temporary
script>".
The editor is now blank (if not, delete the text that appears). Enter the code:
cd $(FULL_CURRENT_PATH)
java $(NAME_PART)
FOLLOW THE INSTRUCTIONS: this code is not the same as the
code for the compile command!!
The first line does the same thing as the first line of the previous script.
It's probably not necessary most of the time, but if you compile a program, and then come
back later
to run it, your system directory might have changed, so it's a good idea to execute it
anyway.
The second line runs the interpreter program (java.exe) on the bytecode that was created
from compiling the current file.
Press the SAVE button to save this script.
As before, in the text field, enter the script name. This one is called "Execute".
Then click the SAVE button under the text field.
You're done, so close the Execute window by clicking the X in the top-right or clicking the
Cancel button.
We now have two scripts that can comple and execute Java programs we write.
However, to run these scripts, we'd have to come back to the Execute window each time.
It's more convenient to add menu items and keyboard shortcuts to run these two scripts.
Let's do this next.
Setting Up Macros
Using the menu bar again, go to Plugins > NppExec > Advanced Options
This dialog is where we'll add the menu items for our two scripts.
Near the top-left of the dialog is the heading "Menu Items *".
Under this is a checkbox labeled "Place to the Macros submenu". Check this box.
Don't close the dialog yet, we still have 2 more things to do.
Next, we'll add the two submenu items for Compile and Execute.
In the same dialog, near the bottom on the left side, you'll see an area where
you can add a menu item for a specific script you created.
In the "Item Name" field, enter Compile Java.
In the Associated Script drop-down, select your Compile script.
Then click on the Add/Modify button.
You'll see your compile item appear in the box above: those are the menu items that are
going to be added to Notepad++'s Macro menu.
Now let's repeat the process to create an Execute Java menu item that runs the Execute
script:
Go to the bottom area of the dialog and change the "Item Name" field to Execute Java.
In the Associated Script drop-down list, select your Execute script.
Click on the Add/Modify button to add your Execute Java item to the list above.
You should now see the two items to compile and execute Java programs in the list.
Click OK to save your changes.
You'll get a warning that you need to restart Notepad++ to apply your changes.
Click OK in the dialog, and then shut down Notepad++ and restart it.
Once Notepad++ has restarted, go to the Macros menu in the menu bar, you'll see 2 new items:
Compile Java and Execute Java.
For convenience, it would be nice to associate shortcut keys with these 2 items.
You don't have to if you don't want to, but it's definitely faster to use shortcut keys once
you
memorize them and get used to them.
In the menu bar, select Settings > Shortcut Mapper
In the Shortcut Mapper dialog, select the "Plugin Commands" tab along the top.
To make it easier to find your menu items, type "java" in the "Filter" text field near
the bottom of the dialog box.
You should see both your menu items appear in the list.
If you don't see them, go back and make sure you've read and followed all the instructions
correctly.
Select the Compile Java item in the list and then select the MODIFY button (or you can
double-click the item).
We'll set the shortcut for Compile Java to Ctrl-Shift-C
In the little dialog, check off the boxes for CTRL and SHIFT, then select the letter "C" in
the drop-down list.
When you're finished, click OK.
Make sure the message near the bottom of the screen says "No shortcut conflicts for this
item".
If it says anything else, you'll need to change the shortcut to something else (use whatever
works for you as long as it's not an existing shortcut).
Repeat for the Execute Java item: Select it and click MODIFY, or double-click the item.
We'll set the shortcut to Ctrl-Shift-E, so check off the boxes for CTRL and SHIFT,
then select the letter "E" in the drop-down list.
When you're finished, click OK.
You now have menu items along with keyboard shortcuts that will allow you to compile and
execute Java programs from inside Notepad++.
Click the CLOSE button on the Shortcut Mapper window.
Check your Macro menu now: you can see both menu items and their shortcut keys.
Let's test everything out and see if it worked.
Testing
You should already have a new file open in Notepad++, but if not, just use the NEW icon in the
button
bar (it's the first button) or use the File > New menu items.
In your new, empty file, copy and paste the following code:
public class Testing {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Use the SAVE option (File > Save or File > Save As, or you can use the SAVE button
in the button bar (it's the third button)) to save your file as Testing.java
The file name is case-sensitive: the T must be upper-case and everything else must be lowercase.
You must include the lowercase .java file extension.
DO NOT save your file in the Notepad++ directory, and DO NOT save it to your Desktop!!
Take the time to go and create a directory for this course and add a "projects"
subdirectory to it, and then save your file there.
You will be creating several files every lesson, and it's important to keep them
organized so that you can easily find them at any time.
NOTE: Changing the console font:
If you find the font in the console area is too small, you can make it larger (and even
change the actual font name/family. In the Notepad++ menu, select Plugins > NppExec > Change
Console Font.
The default font "System" has a limited size on some systems, so if you want a larger font size,
just change
to a different mono-spaced/fixed-width font such as Courier New, Lucida Console, Ubuntu Mono, or
Fira Code.
Then you can change to a larger size.
Now try your menu items:
First, use Macro > Compile Java
or use the Ctrl-Shift-C keyboard shortcut.
This should compile the code you were given. The code has no errors, so you shouldn't
get any (if you do, perhaps you hit a key by accident - copy and paste the code again,
and don't forget to save your file before you compile).
You should see a little console window open along the bottom of your screen,
and it should contain text such as:
If you see an error such as:
It might mean that you are using an older version of Java and you need
to set up some environment variables.
If you're absolutely sure that you've installed the current version of Java
and you see this error, ask your professor.
If you see anything else that doesn't match the successful compile output above,
ask your instructor or professor. If you're not in class and need to contact your instructor
or professor via email, be sure to send them a paste of all the output in your console
window
along with the code you're trying to compile. If you're receiving error messages in a dialog
box,
take a screen shot and attach that to your message.
There are several other problems that could occur, and if you don't show your
instructor or
professor exactly what happened, they can't help you solve the problem.
If your program compiled successfully, it means you now have a bytecode file that
you can execute using the Java interpreter.
If you like, you can use a file management tool to browse to your current directory and see
that
you now have a Testing.java and a Testing.class file.
To execute your bytecode file, we can use the Execute Java menu item:
Use Macro > Execute Java or use the Ctrl-Shift-E keyboard shortcut.
You should see a successful execute in the console window, including the text "Hello,
World!"
Your execute output should look something like this:
Installing IntelliJ IDEA by JetBrains is optional. If you've never
programmed before, you might want to skip this for now and
wait until you're more comfortable. You'll be using an IDE
such as IntelliJ or NetBeans in term 2, so there's no rush
unless you want to try something more challenging. You can
always come back later in the term and install it then if you
feel like you want to give it a try.
IntelliJ is an IDE (Integrated Development Environment, rather than
just a simple editor like Notepad++. It has many more features
and tools that are common to more intermediate and advanced
application development. This is a very popular IDE for students
learning Java: it's project-based, yet also very simple
to use compared to more professional IDEs such as NetBeans or
Eclipse.
Follow the instructions to first download, and then
install IntelliJ IDEA to your machine.
Click the download button under the "Community" edition.
Save the file to your computer.
Once the download is complete, locate the downloaded
file and run it. If you are asked anything such as
"Do you want to allow this app to make changes to your device?"
just agree.
Click NEXT on the opening welcome screen.
The next screen confirms the location of your installation.
The default is fine: you don't need to change anything.
Click NEXT to continue.
The next screen asks you to configure a couple of options
for your installation:
Create Desktop Shortcut: unless your computer is
really old, select "64 bit launcher"
(if your computer is that old, you probably won't be
able to use it in term 2 and should start thinking
about getting a new one).
Create Associations: select ".java". This just
means that when you double-click any .java file on
your computer, it will automatically open with IntelliJ
IDEA. Feel free to leave this unchecked if you prefer
and you know what you're doing.
Once you've set your options, click the NEXT button.
The next screen asks you to choose a name for the folder in
your Windows Start menu for IntelliJ IDEA. You can
leave the default or change it if you want, it's up to you.
As long as you know where it is in future, it doesn't
matter. Click INSTALL.
The installation will take some time. You can watch the
progress window while the software is installing.
The last window confirms that your installation is finished
and asks if you'd now like to run the IntelliJ IDEA program.
If you're in class, you probably want to check the box.
If you're at home and doing this on your own, it's up to
you if you want to run IntelliJ now or not. Either way,
click the FINISH button.
Running IntelliJ for the First Time
When you run IntelliJ for the first time, you'll be asked
whether or not you want to import any settings from
a previous installation. It's assumed you don't have a
previous install, so just make sure "Do not import settings"
is selected and click OK.
Next, you'll be asked to agree to the JetBrains privacy
policy. This is standard. Scroll down to the bottom of
the policy text (the ACCEPT button will remain disabled
until you do so). Click the ACCEPT button once it has
become enabled.
The next screen asks if you would like to share your usage
stats with JetBrains (this is common for many companies where they
want to track how/when users use their software). Personally,
I never agree to these things, but it's really up to you.
Click either button, depending on your preference.
Finally, IntelliJ IDEA starts up and you are asked to choose a
default theme (you can change or customize this in the settings,
later). Choose whether or not you want the darker theme or
the lighter theme. You won't need to change any of the other
options they're going to ask you about, so once you've made
a choice, click "Skip Remaining and Set Defaults"
on the bottom-left of the screen.
The splash screen appears while the program loads.
When it disappears, you'll see the Welcome screen.
This screen asks you what you'd like to do. We might as
well start a new project!
You can create several different kinds of projects in IntelliJ.
For this course, we'll primarily be creating simple Java projects.
A Java project consists of one or more source code
files that are then compiled into bytecode
files. Bytecode files can then be run by any
Java virtual machine.
After choosing to create a new project, you'll be asked
what kind of project you want to create. Make sure Java
is selected in the list on the left side of the screen.
To the right is the Project SDK field. This asks you
to choose which Development Kit you want to use for your project.
If it says "No SDK", you'll have to select the one you installed
earlier. Click the NEW button.
A dialog opens asking you to choose the location of your SDK.
You should see your C: drive listed there, so use the little tiny
arrow on the left to expand it and browse to your Program Files / Java
directory on your system.
In the Program Files / Java directory, you should see
your JDK folder, e.g. jdk-10.0.2 but the numbers might
be different for you. Select that directory and then click
the OK button.
Now you're back in the New Project dialog box.
Ignore the bottom part of the screen where it asks
about libraries. Click the
NEXT button.
The next screen asks what template you want to use. We
won't be using templates at this time so just ignore this
screen. Click NEXT.
The last screen asks you to name your project and choose
a project location. Earlier today you created directories/folders
for your courses, including a java1/projects folder (or you
might have called it prog10082 instead of java1, it doesn't
matter).
This is where you should store all of your projects. A single
project contains multiple files, so when you make a project
in IntelliJ, it will create a new directory/folder to contain
all those files (this is often called the project's root).
The name of your project root is the same as the name of your
project.
Next, we create a name for our project. The project name is
important, since that's how you'll be able to tell one project
from another. Project names should be "self-documenting" (you should
be able to accurately guess what the project is for just by
reading the project name). In the example below, I called the project
"FirstJavaProject". Enter an appropriate project name for your
new project.
Next, we choose a location for our project files.
Each project you create goes inside its own directory/folder.
For example, if you create a project called "MyFirstProject", and
you store it in your java1/projects folder, your project
files should be located in the directory
java1/projects/MyFirstProject
directory.
The "Project Location:"
field determines where your project and its files will be stored.
By default, your Project Location may be listed as some
other directory, but wee need to make sure we set it to
our projects directory we created earlier. Also, since the
project's files should go inside a FirstJavaProject directory,
we'll need to create that directory, too.
Beside the "Project Location:" field, there's a small button with
3 dots (ellipsis) on it. Click that button to open the file
browser.
Now you have a file browser dialog open. Browse to your
prog10082/projects directory and select it.
Then click the "New Folder" button in the button bar at the top.
Now a smaller dialog opens, asking you to enter a new folder name.
For this example, I typed "FirstJavaProject" since that's the name
I chose for this project. After you enter a folder name, click the
OK button.
Now your file browser shows the new project directory inside your
/projects directory. Make sure it's selected and then click OK.
You should now be back at the original new project dialog, and the
Project Location field has been updated to your newly created project
directory.
Note If you currently have another project open when you create a new project,
you'll see the following dialog appear:
Each project you are working on in IntelliJ IDEA runs in its own instance
of IntelliJ. In otherwords, if you have two projects open at the same time,
you'll have two IntelliJ IDEA windows: one for each project (the program is
running twice at the same time).
It's up to you whether or not you want to open your new project in the
current window, or a new one. If you want to see both projects at the same
time, then choose "New Window". If you only want the new project to be
open, then choose "This Window". You will be prompted to save any unsaved
work before the old project is replaced with the new one. (Don't worry, your
old project is still on your computer, it's just not open anymore. You
can always open it again later.)
Once your new project is created and loaded, you'll see the Tip
screen. You can close it, view the next tip, choose not to see
tips on startup, etc.
Setting IntelliJ Preferences
As you're learning to work in IntelliJ, you might wish to
change some of your preferences such as colours/fonts, markers,
etc. To find the settings, go to the File menu
and choose Settings.
Right Margin Marker
By default there should be a vertical line in your editor
that shows you where column 80 is. It is standard in Java
programming to make sure that lines of code do not go
beyond column 80, so we need to make sure there's a visual
guide there to let us know when we should press the RETURN
key on a long line of code.
First, we need to make sure that guide is in fact at
column 80 (often the default is set to something else).
In the settings, go to Editor > Code Style (just click on
Code Style, don't expand it).
Change the following settings:
Hard Wrap at: 80
Visual Guides: 80
Wrap on typing: CHECKED
If you have trouble seeing the visual guide at column
80, you can change its
colour by going to Editor > Colour Scheme > General. In the
list in the top half of the screen, expand Editor > Guides
and select "Visual Guides". To the right, you can then click
the text field beside the "Foreground" check box to change the
colour of the right margin marker. Unfortunately you can't change
the thickness like you can in some other editors.
Theme, Fonts, and Colours
To change your default theme,
go to Editor > Colour Scheme. Under Colour Scheme you'll also
see several screens that allow you to customize the theme.
To change or enlarge the font, go to Editor > Font.