COMP 170 Lab 0  -- Constructing a Simple Program

Course Home Page

Goals for this lab:

  1. Login
  2. Run BlueJ
  3. Create and execute a project in BlueJ
  4. Run the program from a terminal window, without BlueJ.

Steps

  1. Login to the computer.  In the University Labs, use your universal ID.
  2. Launch BlueJ
    • WINDOWS Lab CC 103:  Go to the Start Menu, select Programs (not Loyola Software), and then BlueJ. 
      (An alternative that may be needed in other labs is to find the Windows key to the left of the space bar, with the 4 wavy windows; hold it down until you press R.  This brings up the Run Dialog Box.  Enter
      c:\bluej\bluej
      Note \bluej is repeated twice.) 
    • With Windows at home:  You can use either of the methods above, but it is may be easier in the long run to set up a shortcut on your desktop.  The very first time BlueJ is run, a window for selecting the Java Virtual Machine (VM) may come up. If so:
      • You should be click on the highest version available. 
      • Click the "Launch BlueJ" button. BlueJ should now begin to load.
  3. Create a new Project
    1. Create a new project in BlueJ by selecting it from the Project menu on the menubar at the top.  You will likely see an extraneous directory come up.  The strongly preferred place to put your files (like labs) is a designated folder on a personal flash drive.  A temporary alternative is D: drive. 
    2. Traverse your way to the existing folder wher you want to place your designated folder (say labs).  Choose the icon to create a new directory at the top of the new Project Window, and name it labs. 
    3. Click on the labs directory to open it.  For the File Name enter lab0 and return.  This actually creates a folder lab0, which holds your project. You should see BlueJ: lab0 in the Title bar for the new project.
    4. Click the New Class button to create a new Class.  In the Create New Class Window, the name of the class should be: Hello (match the capitalization), and  its Class Type should just be:  Class.  Click Ok when done.
    5. In the BlueJ lab0 window, double click on the striped Yellow-Orangish Box called "Hello".  This opens an editor with the the standard editing operations, plus a number of features particularly useful for Java programs.
    6. Delete the sample code that BlueJ inserts and instead make its contents match the following (copy and paste or type - as you like):
       /**
      * COMP170 Lab0
      * Prints the famous first CS message, "Hello World!".
      *
      * @author ...you
      * @version 01/10/2009
      */
      public class Hello
      {
      public static void main(String[] args)
      {
      System.out.println("Hello World!");
      }
      }
    7. The system needs to check your syntax and translate the code into a more efficeint form that the Java Virtual Machine can execute.  This process is called compiling.  Click on the Compile button at the top left of the window.  After a while you should see in the bottom message pane "Class compiled - no syntax errors"  If not, you will need to fix a typo and try again.  Compiling automatically saves your file.
    8. Go back to the other window that is titled "BlueJ: lab0".  See the Compile button on the left in this window, too.  You could have used that button to compile instead.  Note that since the compiling was completed successfully, the box labled Hello isl no longer shaded. This means you may run code.  Right click on the Hello box and select "void main(String[] args)", click "ok" for the window that pops up.  Sit back and enjoy the output of your first Java program.
    9. Go back into the editor and change what the program prints out (the part inside quotes).  Test it by compiling and running again, as in the last two numbered parts.
    10. The BlueJ environment is a handy place to start developing programs, but it is not the main environment where Java programs are run!  Unlike all of the early code in the book, you have created a complete program.  Complete Java programs can be run without BlueJ.  On your own machine it would be worth the steps to make it always easy to execute programs outside of BlueJ.  In the University labs where all settings are forgotten at each login, there is an alternative:  
      1. Right click on http://cs.luc.edu/anh/170/examples/jset.cmd and choose to save the link in your designated lab folder (labs in our example).  If this is on your own flash drive, it is stored for future use, too. 
        Firefox issue in Windows:  Firefox (but not IE) changes the name of the file when downloading, adding ".txt".  Before you can run the file, you need to rename it  back to the original, deleting the ".txt".  There is a complication that Windows has an annoying default: it hides file extensions.   If each extension dos not have a unique icon, you do not know what file you are looking at.  A .txt file has a document icon.  If you rename it to jset.cmd, you should see a gear icon for the file.  Hopefully, if you rest your mouse over a Windows file icon, it should show you the extension in a popup Type window.
      2. Open a Windows directory window and change to your designated folder. 
      3. Double click on the icon for jset.  You should see a text terminal window appear.  The jset command hs made it know how to find the Java system files.
      4. Each project has its own folder.  To change director (or folder) enter the cdcommand in the terminal window with the name of the project folder.  for instance, if you used lab0 as in the earlier instructions, and put jset.cmd in the parent folder that contains subfolder lab0, enter
        cd lab0
      5. Enter the command  to see a text listing of the directory:
        dir
      6. This shows the full name of a number of files.  Many are BlueJ specific.  The two files of general interest are
        Hello.java
        (the text of your program, called the source file)
        Hello.class (the output of the compiler - the class file, not intelligible to humans, but that is readable by the Java Virtual Machine)
      7. To run the program, type the following, with the capitalization to match your class name:
        java Hello
        This will make the Java run-time system execute, using Hello.class.  Do not include the .class extension in the command.
      8. Now make another small change in Hello.jhava to what gets written out.  Save it but do NOT compile it in BlueJ.  In the command window, again run
        java Hello
        You should see the same thing as before, since Hello.class has not been updated.  Now run in the command window this slightly differebt line (note the final "c" and the ending ".java":
        javac Hello.java
        This is the actual command that compiles, taking Hello.java and producing Hello.class.  Even when using BlueJ, this may be useful.  Occasionally the extra levels of complexity built into BlueJ have a bug in compiling.  Then it pays to know how to go to the command line.
      9. In the command window, again run
        java Hello
        This time you should see the results of your latest changes to Hello.java.
  4. Show your TA the version with your altered text printed out, and then go home with a new-found sense of accomplishment.

See you next week...


Course Home Page