Your first program

We'll start off by getting your machine set up to build and run Java programs. Then we'll go over the basic usage of the command prompt which is what we'll use to build our programs. And finally we'll create a very simple application which just spits some text out to the screen.

Software Installation

Note: All the software linked to in this section is free to download.

Java Development Kit (JDK)

The JDK is the software that you'll need to both create and run Java programs. Download the JDK by following this link and choosing the appropriate package for your operating system in the section labeled "J2SE ... SDK". It's important that you get the SDK and not the JRE. Install the package (it's a long process so be patient).

Crimson Editor

While any text editor will suffice, the Java support in the Crimson Editor will be a great aid in creating new Java programs. Download the Crimson Editor by clicking on this link. The program includes a help file if you want to familiarize yourself with it.

The Gimp

The Gimp is an image editor for creating graphics for your game. It isn't strictly necessary. You'll only need it if you want to alter the game's graphics. Download the Gimp by clicking on this link. Note that you will need to install both GTK+ and the Gimp itself. For some fun things you can do with Stakeout and the Gimp, see the Customization section on the prototype page.

Command prompt tutorial

Starting a command prompt

There are several ways to start a command prompt:
  • Start > Programs > Accessories > Command Prompt
  • Start > Run > type "cmd" > press "OK"
  • Create your own shortcut (recommended): right click on the "Command Prompt" shortcut in the Accessories menu (see above) and drag it onto the desktop. In the context menu that comes up, click "Copy Here"
The first thing you'll notice about the command prompt is that it lists the current directory (by default this is set to your user directory), followed by a ">". This is the prompt. It is prompting you to type something.

If you decide to create your own shortcut on your desktop or taskbar, you can customize it to make things easier for yourself. To get to the customization dialog, right click on the shortcut and select "Properties"
  • Shortcut Tab: You'll probably want to create a source directory somewhere on your hard drive where you're going to do all your development. It's best to make it someplace that's easily accessible (ex: "C:\mycode"). Set the "Start In" value to point to this folder. Whenever you click on the shortcut to start a command prompt, it will start in this directory.
  • Options Tab: I suggest turning on "QuickEdit mode" and "Insert mode". This will allow you to copy and paste text from and into the command prompt with the right mouse button.
  • Layout Tab: I always change the window width and height to something bigger than the default. Otherwise reading error messages can be more difficult

Moving around

  • To list the contents of the current directory type:
    > dir
  • To change the current directory type:
    > cd directoryName
    where "directoryName" can either be a relative directory or a full pathname like "C:\mycode\stakeout\First Program". Make sure that if the directory your changing to has spaces in its name, you put quotes around it (ex:
    > cd "C:\Documents and Settings\My Name\Desktop"
    ).
  • From any directory you can always refer to the parent directory as "..". So for example, to go up one directory in the path type:
    > cd ..

Running programs

  • If you get frustrated looking at the output of the "dir" command, type
    > explorer .
    This will open up Windows* Explorer in the current directory (don't forget the ".") to show a more familiar interface.
  • Starting Crimson Editor (assuming you installed it in the default location):
    > "C:\Program Files\Crimson Editor\cedit" filename.java

    Note: filename can be either the name of an existing file or the name of a new file that you'd like to create.
  • You can start any other program on your system by typing the full path to that application

Making it easier

Typing out long directory names can be painful, especially if you're a beginning typist. To make things easier you can use auto-completion. With auto-completion, you simply type the first part of a file name or directory and then press the "Tab" key. The first filename or directory that matches that prefix will then be completed for you on the command line (automatically adding quotes if necessary). To use auto-completion, download and open / run this script. Here's an example of something you can try:
> cd c:\
> Press [Enter] >
> cd doc
> press [Tab]
If there are multiple files or directories with the prefix you are using, pressing [Tab] multiple times will cycle through the available options.

Even with auto-completion, it's still a pain to have to type out the full path name to cedit every time we want to create a new file. We can make it much easier by putting cedit "in the PATH". The PATH is an environment variable that Windows* looks at every time you tell it to run a program without a full path name. It is a list of directories separated by the ";" character. Adding a directory to your PATH is similar to what you do with the environment variables in the Prototype setup, except that instead of replacing the variable value, you append it to the end. Make sure that when you add a directory to the end of the PATH that you add a semi-colon to separate it from the previous ones. So to make using the Crimson Editor easier, add it's installation directory to your PATH. Here's what my PATH looks like: "C:\bin;C:\Vim\vim61;C:\ant\bin;C:\Program Files\WinZip;%JAVA_HOME%\bin;C:\WiX;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Crimson Editor" Now if you open a new command prompt, all you have to do is type
> cedit filename.java
to create a new file.
Old command prompts lying around This will not work for command prompts that were started before you changed your PATH. You will have to close any open command prompts and restart them.
Note: The "%JAVA_HOME%" in my PATH is another environment variable. It's value is set to the directory where my Java is installed. The same should work for you if you set up JAVA_HOME according to the Prototype setup, but you can also just type out the full directory. Do this now as it will help greatly when we start compiling code and running programs (below).

Creating a program

Writing code

Create a new file with the Crimson Editor called "Stakeout.java" and copy the following text into the file:
public class Stakeout {
    public static void main (String args[]) {
        System.out.println ("Hello World");
    }
}
This is called source code. Ultimately source code is just a text file with a special extension to indicate what language the program is written in. In our case, we are writing Java code, so all our files will end in ".java". By default Windows* hides file extensions from you. This can cause some confusion, so I would suggest you tell Windows* that you want to see file extensions. To do so
Open a Windows* Explorer > Tools menu > Folder Options... > View tab > Uncheck "Hide extensions for known file types"
Source code is not a program. You can tell your computer to run a program but you can't tell it to run source code. So what's the point of source code? You use source code to create a program, because you can't create it yourself (at least most normal humans can't). To create a program, you have to use another program called a compiler. The compiler takes your source code and turns it into a bunch of cryptic instructions that the computer can understand. This is an overly simplified explanation in the case of Java, but it will suffice for now. And this brings us to our next topic...

Compiling code

The Java compiler is called "javac.exe". To compile your source code from the last section
open a command prompt > go to the directory where you created you source file >
> javac Stakeout.java
If you didn't put the JAVA_HOME\bin directory in your PATH before, you'll have to type out the whole path to get this to work.
javac not found You have to put your Java's bin directory in your PATH
*.java not found Make sure you are in the right directory and that you've typed the file name correctly (case matters).
some other error message You probably didn't copy the whole code section above. Every character matters.
If all goes well, you won't see any error messages. Type
> dir
You should see that a new file called "Stakeout.class" has been created. Type
> notepad Stakeout.class
. This is a java program. You'll be able to make out a few words, but most of it will look like garbage to you. Fortunately you're computer knows what to do with it.

Running the program

Unlike what is called a native program, Java programs can't be run directly by the operating system (you can identify a native program by the ".exe" extension). To run a java program you have to use a Java interpreter. The Java interpreter itself is a native program which is called "java.exe". It takes a Java program and turns it into a native program (more or less). To run your program type
> java Stakeout
. You should see "Hello World!" printed out on your command line. That's it! A pretty lame start, but a start nonetheless.
can't find the module You probably typed it incorrectly (it's case sensitive)

Follow-up exercise

Rename the program (file and class) with a new name for your game (but realize that I will be calling it "Stakeout" throughout these lessons). Change the message that it prints out to show a message welcoming the user to the game.


Rate this lesson: *****