ITEC 120
First Java Program

Objectives

In this lab you will write, compile, and execute your first Java program. To do this you must first establish a development environment. You will also learn about your home directory (on your H: drive) and you will learn some basic commands to navigate the file system from a command line.

To successfully complete this lab you will:

Set Up Your Environment

to write, compile, and edit code. Lab machines have the java compiler installed, and jEdit to edit your code. On your own machine, if you haven't done so already, you will need to install Java and perhaps a text editor. Instructions here.

Create a Workspace

Every time you start a new assignment (e.g., lab, homework, etc) you should create a new directory (or folder) to store your files. You will create many files during the semester. Having separate directories for each assignment will keep your files organized and easier to work with. You will frequently want to refer to previous labs and assignments and you need to be able to find them.

On a lab machine, you will need to connect to your H: drive, which is a directory that only you have access to. You can connect to it from any machine on the RU network. Connect to your H: drive from Windows, or from a Mac.

You are probably familiar with graphical user interfaces (GUI) like Windows. In this class you will learn to work from a command line. In Windows, click on the Start button, type cmd in the search text box, and hit return. This will launch a command prompt window. On OS X (Mac), open the Terminal application to launch a command prompt window.

We will start with three simple DOS/OS X commands:

DOS

OS X

Description

dir

ls

list the contents of a directory (folder)

cd

cd

change directory to a different directory

mkdir

mkdir

make a new directory

Let's start by listing the files in your current directory. Enter dir at the command prompt and hit return. The dir command displays all of the files and directories in your current location.

   dir

Make a new directory named itec120 to store all of your files for this course. Note: itec120 is all one word – there are no spaces. It is not wise to use spaces in the names of files and directories. First, switch to your H: drive.

Then type the command below and hit return.

   mkdir itec120

Use the dir command again to list the contents of your current directory and you should see a directory named itec120.

Type the command below to change into the itec120 directory.

   cd itec120

Use the dir command to list the contents of your current directory. The directory should be empty because you just created it. If you are using the OS X Terminal, use the ls command instead of dir.

   dir

Use the mkdir command to create a directory named lab01. Once again, there are no spaces in the name of the directory. The lab01 directory will be your workspace for this lab.

   mkdir lab01

Use the cd command to change into the lab1 directory.

   cd lab01

Now that you have created a workspace for this lab, you are ready to start writing code.

Writing Code

Programmers write code in an editor. An editor is similar to Microsoft Word. Note: do not use Microsoft Word to write code. Word is not designed for writing code.

There are many editors to choose from. We recommend jEdit because jEdit runs on OS X and on Windows. If you are using Windows you may also use Notepad or Notepad++. If you are using OS X you may use TextEdit, but be sure to change the format to plain text.

jEdit is free to use. You may download jEdit here.

Here is a short video that shows how to use jEdit:

Your First Java Program

Type the program shown below into jEdit. Substitute your name as the author and today's date for the version. Save the file in your lab01 directory with the name Hello.java.  This is especially helpful with jEdit because the editor will highlight the syntax when you save a file with the .java extension.  It is wise to save your files often.

 /**
  * Hello.java
  *
  *
  * @author   Shawn Brenneman
  * @version  12-AUG-2014
  */
  public class Hello
  {
      public static void main (String[] args)
      {
          System.out.println("\nHello World!\n");
      }
  }

Compile and Execute Your Code

People typically write code in a high level language like Java that is easy for people to read and write. Computers understand machine code. A compiler translates source code, written in a high level language, into machine code. The compiler also checks the syntax of the code and identifies syntax errors.

You will use the command line to compile and execute your code. Return to your command prompt window (cmd on Windows, Terminal on OS X). Enter the command below to list the contents of your current directory. (Use ls on a Mac.)

   > dir

When you list the contents of your directory you should see a file named Hello.java.

javac is the command that runs the java compiler.  When you compile a file, you must use the include the .java file extention. To compile your program, type:

   > javac Hello.java

If there are errors in the program the compiler will provide error messages to help you find and fix the errors. If there are no errors you are ready to execute your program. When you run your program, you execute a class, and you do not use a file extension. To run your program, type:

   > java Hello

If you entered the program correctly and the complier did not display any errors, the program will print Hello World! on the screen.

Save Your Work

When you work locally your files are only available on the computer you are using. That is why it is important that you save your work on your H: drive, which is accessable from any machine on the RU network. Therefore, the files in your lab01 directory on your H: drive are accessible from any computer with Internet access. However, if you are working off campus you will need to be on the VPN in order to be on the RU network. How to install the VPN.

The contents of your home directory are backed up each night (i.e., copied to a second location).  This protects your files.  If you delete, corrupt, or otherwise lose a file in your home directory, it is possible to recover a previous version of the file.  For this reason you should keep a copy of your program files in your home directory to reduce the chances of losing your work. 

If you are not working in your home directory on your H: drive, then you should save all of the files in your workspace to your home directory before you logout today.

Submit Your Assignment

You will get a quiz grade for this lab, based on your completion of two things: