Lab 00: Introduction to BlueJ

No Due Date

COSC111: Introduction to Programming, Fall'07

Matt Evett,   CS Dept , Eastern Michigan University


version 1.2 -- Recent changes are marked in
italic red.

 

Introduction to BlueJ

Introduction

<Bottom | Next >

The purpose of this lab is to give you practice in using the CS computers and BlueJ (pronounced "Blue Jay"), the Java development program we'll be using. to create, compile, and execute programs.

<Top | Bottom >

1. Log on to a Math/CS Computer

<Prev | Next >

The CS computers are PH520 and 513. 

2. Prepare for Java and Launch BlueJ

<Prev | Next >

Once you've logged on to the machine, work with Mac OS for a while and then launch BlueJ.

2.1 Prepare for Java

2.2 Some tutorials on Windows XP and Windows Explorer

Inclusion does not imply approval or disapproval of any commercial product, etc., sigh.

2.3 Launch BlueJ

BlueJ (pronounced "Blue Jay") is the Java development program we'll be using. The three letter acronym is IDE: Integrated Development Environment. Unlike other Java development environments (such as JBuilder or Eclipse), BlueJ is small, and specifically designed for teaching Java. It was developed by a team of people at Monash University, in Melbourne, Australia. If you're planning to work on your Java programs at home, you'll want to download a copy of it (and of Java, too). See the web page on Using Java at Home .

There are two ways to start BlueJ. The easiest is by double-clicking on the "BlueJ" icon at the bottom of the screen. If you don't see such an icon, you can access BlueJ from the "Start" menu, in the bottom left-hand corner of the desktop:

<Prev | Top | Bottom >

3. Create a New Project

<Prev | Next >

A BlueJ project is a Windows folder that contains (in addition to various Java files) a couple of special files that BlueJ uses to keep track of its work for that project. A BlueJ project name is just the Windows directory name.

  1. To create a new BlueJ project, go to the Project menu and select New... . A selection dialog opens that lets you specify a name and location for the new project.
  2. You want to navigate through the navigation dialog box to get to the V: drive entry. This corresponds to the files.emich.edu space, and so is accessible from any browser. You may want to create a new folder there (maybe named COSC111) and select that. Thje "filename" should now look like V:\COSC111\ . Complete the name by appending "Lab00" to get V:\COSC111\Lab00 Press Create or hit return.
  3. BlueJ will open up a Project Window with some buttons and a plain document icon. (It's for a file Readme.txt; we'll ignore it.) Other icons will appear as we write our program.
  4. Look at v:\COSC111 using Windows Explorer. You'll find that you now have a folder Lab00. This folder is your BlueJ project Lab00. Note that the Lab00 folder contains files Readme.txt and bluej.pkg . The Readme.txt file is for documentation; for now we ignore it. The second file, bluej.pkg, holds information that BlueJ collects on how your project is put together. A BlueJ project is simply a Windows folder that contains a bluej.pkg file.
  5. If folder Lab00 contains files called Readme and bluej, not Readme.txt and bluej.pkg , it's because the .txt and .pkg extensions are being hidden. To unhide it, select View -> Options from your folder menu, select the View panel, and uncheck Hide file extensions for known file types. Press OK. Now your Windows folder Lab00 should show Readme.txt (and bluej.pkg).
<Prev | Top | Bottom > 

4. Create a Java Class

<Prev | Next >

Java programs are composed of classes, so you write programs by creating and editing classes. Creating a class causes BlueJ to create a Java "source code" file inside the folder that makes up your BlueJ project.

  1. Your project doesn't currently contain any classes, so let's create a new class.
  2. Create a class by clicking the New Class button on the left side of the project window.
    1. A dialog box will appear, asking for the name for the class: Type HelloWorld. By convention, Java class names begin with an initial capital letter. Be sure not to insert spaces into the name, so type HelloWorld, not Hello World, or helloworld or HELLOWORLD, etc.
    2. The dialog box also gives you a choice of what to create. Choose the Class radio button, because we want a general class. Click Ok or hit return to create the class.
  3. Notice that the BlueJ project window now contains an icon for HelloWorld.
  4. Look at yourV:\COSC111\Lab00 folder with Windows Explorer. You should now find a file named HelloWorld or HelloWorld.java inside your Lab00 folder. This is the source code file for the class HelloWorld.
    1. If this were a homework assignment and we asked you for an electronic copy of your program, you'd copy HelloWorld.java somewhere, using Windows or a file transferring program (see Electronic Submission Procedures ).
  5. If folder Lab00 seems to contain a file called HelloWorld, not HelloWorld.java, it's because the .java extension is being hidden. To unhide it, select View -> Options from your folder menu, select the View panel, and uncheck Hide file extensions for known file types. Press OK. Now your Windows folder Lab00 should show HelloWorld.java.
  6. If the Lab00 window is set to view Details, you'll find the HelloWorld.java file has the kind "Java source file" or some such.
  7. The BlueJ project window only shows the unextended name of the class, HelloWorld; it doesn't show HelloWorld.java .
<Prev | Top | Bottom >

5. Edit Your Class

<Prev | Next >

BlueJ allows you to edit the source code for a class. We'll edit yours so that it will print a short message.

  1. Open an editing window for the HelloWorld class by double-clicking its icon or by right-clicking the icon and selecting "Open Editor " from the popup menu.
  2. The source code file doesn't start off empty, it uses a default template. As a rule, you'll want to modify your class from this template.
  3. The editor is pretty simple. You can move using the arrow keys, and copy and paste text. The lines do not wrap around if they get long: You'll need to press Enter to break up long lines.
  4. Modify the HelloWorld class so it looks like the code below. Add your name, your section number, and today's date as appropriate. Please give your name in the order "Family name, Given name" or "Given name Family name". E.g., Sasaki, Jim or Jim Sasaki.
  5. Editing the HelloWorld class in BlueJ causes the Windows file HelloWorld.java file to be modified. Selecting Class -> Save in the editor window saves the HelloWorld.java file. (So does pressing Control-S.) You can "cut and paste" the following code if you want.
    /*
    * This is my very first Java program!
    * Comp 170, Section ??? [fill in your section number].]
    * @author: ??? [fill in with your name]
    * @date: August 27, 2002
    */
    public class HelloWorld
    {
    // Our first program prints a short traditional message.

    public static void main (String[] args)
    {
    System.out.println("Hello, world!");
    }
    }
     
  6. If you like playing with editors, then someday when you feel bold & adventurous and have plenty of time, you can select Options -> Key Bindings from an edit window and change the way BlueJ binds keys to various editor functions. Don't do it today, though.
<Prev | Top | Bottom > 

6. Compile Your Program

<Prev | Next >

  1. In the BlueJ project window, the HelloWorld class icon should have blue diagonal lines crossing it. This indicates a file that needs to be compiled before it can be executed.
  2. Click the Compile button near the top of the editor window. As the compiler does its work, it displays messages in the status line at the bottom left of the project window.
    1. You may see "Class saved" because BlueJ automatically saves any changes to your class before compiling it. (BlueJ also saves your code when you close a file or quit the program.)
    2. If compiler finds an error, an error message is displayed. You'll have to fix your program and recompile it. Check for typographical mistakes: Did you misspell something? Did you type something in the wrong case? Did you forget one of the lines with the /* or */ ?
    3. You may get an odd errors message about an "illegal character". If so, try clicking on the little "question mark" icon in the bottom right corner of the Editor window. It will probably tell you how to fix the problem.
    4. The status message "Class compiled - no syntax errors" (in the Editor window, not in the Project window) indicates that compilation succeeded and you can now run your program.
  3. Once the compilation succeeds, look at the HelloWorld class icon. The blue diagonal lines should be gone, indicating that the class does not need to be compiled.
  4. You can also compile a class by right-clicking it in the project window and selecting Compile.
  5. You can compile all the classes in your project that need compiling (if any) by clicking the Compile button in the project window. This will be useful in larger projects, which may consist of several class files.
  6. Using Windows, inspect your Lab00 folder. Note that it now contains a HelloWorld.class file. It's the compiled code for your HelloWorld class.
    1. If you are hiding extensions for known file types (see Create a class ), then you'll only see a HelloWorld file, not a HelloWorld.class file.
    2. In fact, you'll see two files named HelloWorld : One is the .java file and one is the .class file. If you set the Lab00 window to View -> Details, you'll see that the files have different kinds.
  7. When you're submitting electronic copies of your homework (not lab) programs, you don't normally send copies of your .class files.
<Prev | Top | Bottom > 

7. Run Your Program

<Prev | Next >

  1. Bring BlueJ's project window to the front by clicking on it or by closing the editing window.
  2. Right click on the HelloWorld class icon. From the popup menu, select void main(args). This tells BlueJ that you want to execute the class's main method.
    1. A dialog box will appear that asks you to specify the arguments to this call of the main method. The default value is fine; press OK.
  3. The program now runs. BlueJ will open a Terminal window and display your output in it. Is it the right output? If so, congratulations! (If not, ask the Lab Assistant for help.)
  4. Again, bring BlueJ's project window to the front. Don't close the Terminal window. In fact, move the project window so that it hides the Terminal window (or at least, the top of it).
  5. Rerun your program by repeating the instructions above: Right click on the HelloWorld icon in the project window. Select void main(args), and press OK when the argument dialog box opens.
    1. When you run a program, if the Terminal window is already open, BlueJ doesn't bring it to the front, even if it's hidden. You must click on the Terminal window to bring it to the front. Alternatively, (usually because there's no part of the Terminal window visible) you can click on the small icon at the bottom of your screen that corresponds to the Terminal window. This is probably the rightmost icon there. (When you hold the cursor over it, a pop-up label, "BlueJ: Terminal Window", will appear.)
    2. Note that the Terminal window contains two sets of output, not just one: BlueJ doesn't clear the window between executions. To clear the window, select Options -> Clear from the Terminal window.
  6. Rerun your program. Note that the output appears in the Terminal window.
  7. You can close the Terminal window, but there isn't any way to get it back without running the program again. Do that: close the Terminal window, then rerun the program one more time. Note that the Terminal window pops up and is brought to the front.
    1. You may wish to keep the Terminal window closed in general. That way, it will always pop up to the front when you run something that prints output. On the other hand, you may wish to keep the Terminal window open in general, so you can see output as it is generated.
  8. Bring the Terminal window back to the front. (If necessary, rerun your program.)
    1. Note that there's no option to print the Terminal window. Often, when you submit a program you will also have to submit a copy of the output it generates, so....
    2. To print a Terminal window's content, you have to save it as a file first. Select Options -> Save, and save your output as a text file (with a .txt extension). Pay attention to the name of the directory in which you are saving the file. It should be in your project's directory.
    3. Now you can open this text file using a program like WordPad to print it out. (You can find WordPad under Start -> Accessories -> WordPad.) Add your name to the top of the terminal output.
    4. Print the terminal output to the lab printer.
<Prev | Top | Bottom > 

8. Close Your Project

<Prev | Next >

  1. To close a project in BlueJ, go to the project window and select Project -> Close.
  2. If you quit BlueJ (don't do it now), then all open projects will be saved and closed first, so changes to classes will be saved eventually even if you don't save the class or close the class's project. Nonetheless, it's a good idea to save your classes occasionally when you're editing.
<Prev | Top | Bottom > 

9. Open an Old Project

<Prev | Next >

So far, you've launched BlueJ, created a new project, edited it, and run it. We'd like you to start all over with BlueJ but this time we'll work on opening an already existing project.

  1. Quit BlueJ (select Project -> Exit). Once you're back in Windows, relaunch BlueJ.
  2. Once the BlueJ project window reappears, choose Project->Open. If necessary, change directories to get back to your myJavaProjects folder. Note that in the file directory dialog, the Lab00 project appears with an icon that looks like a wrapped package. Select the Lab00 project.
  3. Your project window should now be the one for Lab00 .
  4. Quit BlueJ again. Look at your c:\Temp\138\myJavaProjects\Lab00 folder under Windows. One of the files there should be bluej.pkg file. Move it to another folder, using Windows.
  5. Relaunch BlueJ again and use Project -> Open... to try to open Lab00. Note in the selection dialog, Lab00 appears with a plain directory icon. Double click on it to open it. Note that the Lab00 folder now contains a folder labelled "HelloWorld", but not an icon you can select. It isn't, but that's because BlueJ only shows you directories and BlueJ projects, not individual Java files or text files, or so on. Cancel the Open.
  6. If you move the bluej.pkg file back into the directory, things should work correctly again when you try to open the project.
  7. If you're familiar with Windows, you know you can double click (or Open) a file in Windows and expect the program that goes with it to start running. The lab computers are not set up to run BlueJ when you open a .java file; you need to launch BlueJ and open a project directory. Back in Windows, find your c:\Temp\138\myJavaProjects\Lab00\HelloWorld.java file. Double click on it. Does BlueJ start running? (Hint: NO.) Quit this program.
<Prev | Top | Bottom > 

10. Add an Existing Class

<Prev | Next >

Often you will have to add existing code to your project in order to modify it.

  1. Still with the Lab00 window open (reopen the project if you have to.)
  2. Use a web browser to download FirstProgram.java from the course web pages. Save the file in your Lab00 folder.
  3. Use Edit->Add File From Class to select the file you must downloaded.
  4. You should see an icon named "FirstProgram" appear in the project window.
  5. Compile and run the program. Again, add your name to the terminal output and print it. Hand in this output along with the earlier output to me at the end of class.

11. Fix Errors

<Prev | Next >

This Edit/Compile/Debug sequence is the basic sequence you'll go through. Of course, you won't typically know what your errors are! This sequence repeats until your program no longer has any errors that the Java compiler can catch. (This doesn't mean that no errors exist, unfortunately.)

<Prev | Top | Bottom > 

12. BlueJ Tutorial & Textbook Files

<Prev | Next >

For broader and more detailed information about using BlueJ, go to the BlueJ project window, the Help menu (way on the right), and select BlueJ Tutorial. Much of this may be hard to follow before you know more about Java classes and methods.

The textbook contains a number of sample programs. They should be available via the web page http://emunix.emich.edu/~evett/COSC238/AbsoluteJava2e_SrcCode/. As the semester goes on, you may wish to inspect or compile these programs under BlueJ yourself.

<Prev | Top | Bottom > 

Document History

<Prev | Top >