Lab 00: Introduction to BlueJ
No Due Date
version 1.2 -- Recent changes are marked in
italic red.
<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
>
<Prev
| Next
>
The CS computers are PH520 and 513.
<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
- Find the folder C:\Temp\138. Create a subfolder called myJavaProjects.
Ask the Lab Assistant for help if necessary.
- If you've never used Windows and Explorer before, you may should play with
it for a while and in your off time, you should look up a short tutorial on
using Windows and Explorer.
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:
- Select Start -> Programs -> Programming tools -> BlueJ.
(Click and hold the mouse on the Start button and keep it down as you
navigate through the menus that pop up. When you find and select BlueJ,
let go.) If there are multiple BlueJ's in the list, use the highest-numbered
BlueJ, local copy (if one is available).
- You may or may not find that an MS DOS window opens up; if so, minimize
it. (It may open already minimized.) (Recall that to "minimize"
is to click on the small "_" (underbar) square in the upper right-hand
corner of a window.)
- After a few long moments, a BlueJ "project window" should open
up. You can recognize it because it has a blue jay in the corner and
Project as its first menu item.
<Prev
| Top
| Bottom
>
<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.
- 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.
- 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.
- 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.
- 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.
- 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
>
<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.
- Your project doesn't currently contain any classes, so let's create
a new class.
- Create a class by clicking the New Class button on the left side of
the project window.
- 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.
- 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.
- Notice that the BlueJ project window now contains an icon for
HelloWorld.
- 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.
- 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
).
- 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.
- 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.
- The BlueJ project window only shows the unextended name of the
class, HelloWorld; it doesn't show HelloWorld.java
.
<Prev
| Top
| Bottom
>
<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.
- 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.
- 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.
- 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.
- 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.
- 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!");
}
}
- 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
>
<Prev
| Next
>
- 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.
- 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.
- 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.)
- 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 */ ?
- 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.
- 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.
- 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.
- You can also compile a class by right-clicking it in the project
window and selecting Compile.
- 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.
- Using Windows, inspect your Lab00 folder. Note that
it now contains a HelloWorld.class file. It's the compiled
code for your HelloWorld class.
- 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.
- 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.
- 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
>
<Prev
| Next
>
- Bring BlueJ's project window to the front by clicking on it or
by closing the editing window.
- 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.
- 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.
- 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.)
- 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).
- 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.
- 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.)
- 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.
- Rerun your program. Note that the output appears in the Terminal window.
- 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.
- 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.
- Bring the Terminal window back to the front. (If necessary,
rerun your program.)
- 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....
- 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.
- 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.
- Print the terminal output to the lab printer.
<Prev
| Top
| Bottom
>
<Prev
| Next
>
- To close a project in BlueJ, go to the project window and select
Project -> Close.
- 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
>
<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.
- Quit BlueJ (select Project -> Exit). Once you're back in Windows,
relaunch BlueJ.
- 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.
- Your project window should now be the one for Lab00
.
- 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.
- 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.
- If you move the bluej.pkg file back into the directory, things should work correctly again when you try to open the project.
- 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
>
<Prev | Next >
Often you will have to add existing code to your project in order to modify it.
- Still with the Lab00 window open (reopen the project if you have to.)
- Use a web browser to download FirstProgram.java from the course web pages. Save the file in your Lab00 folder.
- Use Edit->Add File From Class to select the file you must downloaded.
- You should see an icon named "FirstProgram" appear in the project window.
- 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.
<Prev
| Next
>
- Open the HelloWorld source file again (by double-clicking
on the HelloWorld class icon ).
- Delete the semicolon after ("Hello")
- Compile. This error is easy to find because we just introduced
it, explicitly. Note that a line has been highlighted, indicating where
the compiler became aware of an error. In the message box at the bottom
of the window is a brief description of the error. "';' expected." Note
which line is flagged with the error: It's the line at which the Java
compiler first realized there existed an error.
- Click on the "?" button at the right of the message. Some of
the more common errors have more help available this way.
- In the previous line, add the semicolon back and change
println to Println.
- Compile again. Click the "?" icon for this error: See if there
is any more information.
- Fix the line so it says println again.
- Compile again.
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
>
<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
>
<Prev
| Top
>
- Tue 8/27/02: Posted initial version.
- Wed 9/1/05: Updated for BlueJ 1.9.
- Wed 9/5/07: Updated for Java1.5.