Getting Started Writing and Compiling Java Code

Since this is the first time that many of you are developing Java programs, this brief set of instructions should help you get started.  Although later in the semester, we will be using Eclipse for development, these instructions will assume that you are using Emacs as your text editor and compiling directly from the terminal.  You can use another text editor if you wish, but I recommend that you use one with syntax highlighting.

The CS computer lab (Park 231) is already set up for Java development, and is the only "officially" supported platform for development in this course. When developing, I strongly recommend that you reboot the computers into Linux instead of using Windows.  You'll notice that many computer scientists using Linux or Mac over Windows, and one of the main reasons is the ease of development in these operating systems.  However, you can develop in Windows if you choose.

I've included instructions below for each of the main operating systems, and these should be sufficient to get you started in developing Java programs.  However, since the department does provide a computing lab configured for the needs of this course, the instructor will not provide assistance for configuring and debugging installations on personal computers.  If you choose to install and develop on your own computer, then you are responsible for configuring it appropriately.

Linux

The Java SDK and Emacs are often installed by default under Linux.  However, you can download and install them from the following links if your system does not already have them installed:  Java SE SDK, Emacs.

Open up the Terminal application, yielding a command prompt.  You can use the cd (change directory) command to navigate around the file system.  The command cd ~ will put you back into your home directory.  Here's an example of setting up a new directory for the assignment:
> cd ~                            Change directory to your home directory
> mkdir cs206                Creates a new directory ~/cs206, which is under your home directory
> cd cs206            Moves into that directory
> mkdir assignment0     Creates a new directory ~/cs206/assignment0  
> cd assignment0          Moves into that directory

You can then create the new file using emacs by typing:
> emacs Assignment0.java &

The emacs text editor is a standard graphical editor and should be self explanatory.  By putting the & at the end of the calling line, you told Linux to launch emacs and then pass control back to the Terminal, allowing you to use both simultaneously.  You can use it without the &, in which case you won't be able to use the terminal until you've exited emacs.

Enter the code for HelloWorld or some other simple program.  Save and exit emacs to go back to the terminal.  You can see your file (and all others in the directory) by typing:
> ls
Assignment0.java


To compile and run it, just use the commands we discussed in class:
> javac Assignment0.java
> java Assignment0

Hello World!

Pressing the up/down arrows will allow you to scroll to choose previous commands without retyping them.  It is often convenient to compile and run a program in a single up-arrow/return button press, which you can do by:
> javac Assignment0.java;  java Assignment0

Mac OS X

Mac OS X is based on BSD Unix, which is a lot like linux, so everything stated above for Linux also applies to Macs, with the following modifications:

Microsoft Windows

If you use your own Microsoft Windows computer for development, you'll need to install everything and it will likely be a pain the first time.  First install the Java SE SDK and then Emacs.  From the Emacs website, click Download, then scroll down to the "Obtaining Emacs" section, choose the ftp or one of the mirrors, then navigate to the emacs/ folder, go into the emacs/windows/ folder, and then download the latest version of the Emacs binary according to the dates, which will be named emacs-XX.X-bin-i386.zip with the XX.X replaced with the latest version number.  (As of Sept 3, 2011, this is the current version, which you can download directly.)  Extract the archive, and then put the resulting emacs-XX.X directory somewhere you can find it easily, like in c:\Program Files.  You can then launch Emacs by running c:\Program Files\emacs-23.3\bin\emacs.exe.

The command prompt for Windows is available under Start -> Programs -> Accessories -> Command Prompt.  You can use the cd command to change directories and navigate around the filesystem, same as in Linux.  However, other commands are different:  use dir instead of ls, and the ~ is no longer a shortcut to your home directory.  Here is an overview of MS-DOS commands, which may help you use the command prompt in Windows.

For example, here is the equivalent setup for Windows as given above for Linux:
> c:
c:\>  cd "c:\Users\Eric\"
c:\Users\Eric\> mkdir cs206
c:\Users\Eric\> cd cs206
c:\Users\Eric\cs206> mkdir assignment0
c:\Users\Eric\cs206> cd assignment0


Launch Emacs from Start->Programs->emacs-XX.X if you're in the computer lab or via Windows Explorer at home (it might be a good idea to add a shortcut to the Emacs executable to your start menu or desktop).  Create a new file called Assignment0.java, edit and save it into the cs206\assignment0\ directory.

From the command prompt, ensure that you are in the cs206\assignment0\ directory (you should be able to see your file listed if you type dir at the prompt).  You can then compile and run it via:
> javac Assignment0.java
> java Assignment0

Hello World!

If the system cannot find javac, you need to specify the full path to javac (java should work fine).  For example:
>"c:\Program Files\Java\sdk7.x.x\bin\javac.exe" Assignment0.java
Be sure to use the path where your particular version of the sdk is installed.  This line above will not work verbatim!

Typing the full path to javac is rather cumbersome, so I recommend you set up your Windows path to tell it where to find the executable, which will allow you to run it just by typing javac at the terminal. 
  1. Choose Start, Settings, Control Panel, and double-click System. Look for PATH in the User Variables, System Variables, or Environment Variables, depending on which version of Windows you're running.  If you're not sure where to add the path, add it to the right end of the PATH in the User Variables. If you already have a PATH variable, just add this directory to the right of the others, separating it via a semi-colon (;).  A typical value for PATH is something like (you'll need to customize it to your own installation): 

    <bunch of other directories>; C:\Program Files\Java\sdk7.X.X\bin

    Capitalization doesn't matter. Click "Set", "OK" or "Apply".

    The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should only have one bin directory for a Java SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to sdk7.x.x.  Be sure to replace the sdk version number with the version you're running.

  2. The new path takes effect in each new Command Prompt window you open after setting the PATH variable.  So, you may need to close existing Command Prompt windows to have it take effect.