University of North Carolina at Asheville

CSCI 273.002: Mathematical Algorithms


Lab 01

Developing Programs in Java: Terminal Commands vs. IDEs


[Introduction] [Lab Project] [What To Submit]

Introduction

In this first lab you will use both terminal commands and the NetBeans IDE (Integrated Development Environment) to build and test simple console applications. A working knowledge of both programming environments will be very helpful to you throughout this course, for your labs and also for your homework assignments.


Lab Project

  1. Login to your home directory and open a terminal window (essentially the same thing as a Command Prompt window in Microsoft Windows systems). One convenient way to do this in your Linux environment is via the popup menu that appears if you right-click on any blank region of the Desktop. Then enter the following sequence of terminal commands:
    
        cd csci
        mkdir 273.002
        cd 273.002
        mkdir labs
        mkdir homework
        cd labs
        
    If necessary, get help from your instructor for these steps. Later you may also want to consult our summary help page on Linux Terminal Commands. But don't worry, you should not have to go through these particular steps again for the remainder of this semester...

    When the smoke has cleared, note that you have just created a new directory (folder) named 273.002 in your prexisting csci directory. And in 273.002 itself, you now have two new folders: one to hold your lab projects, and one for your homework. The path from your login (home) directory to your lab directory is just csci/273.002/labs. From now on, be sure to create all your lab project folders in csci/273.002/labs.

  2. If you entered all the commands as shown above, at this point your default directory should be csci/273.002/labs. For future reference, note that you can immediately enter this directory from your home directory using the single command
    
        cd csci/273.002/labs
        
    You may also find it useful to know that the command
    
        cd
        
    returns you to directly back into your home directory (this works with Unix/Linux systems, but unfortunately not with Windows command prompts).

    So now that you are in your labs directory, launch a simple text editor and use it to write your own version of a HelloWorld program in Java as described in your text. One available editor is called gedit, which you can launch with the command

    
        gedit HelloWorld.java &
        
    The little & at the end of the command is there so that you can jump back and forth between the gedit window and the terminal window as necessary (without it, your terminal window is "locked up" until gedit terminates).

    Note: several text editors are actually suitable to use for this step, including gedit, nano, and even vi. If you happen to have preferences here, just use your favorite editor. But please feel free to consult your lab instructor if you have any questions.

  3. Once you have written your source file, save it using the same name as the Java class you defined (presumably HelloWorld), along with a .java extension (HelloWorld.java). Then try to compile it using the command
    
        javac HelloWorld.java
        
    If you get error messages, just jump back to your editor window, fix the errors, save the file, jump back to the terminal window, and repeat the javac command until all is well. At this stage you should enter the ls (directory listing) terminal command and look for two files, namely HelloWorld.java and HelloWorld.class. The .class file is what the compiler creates. It stores the sequence of bytecode instructions representing your program, which can now be executed by the Java Virtual Machine (JVM).
  4. Next, try to run your program, using the command
    
        java HelloWorld
        

    If all goes well, you should finally see the message that you specified in your program. If not, it's another good time to consult with your lab instructor...

  5. Now launch the NetBeans IDE and select the File/New Project... option in the toplevel menu bar. A dialog box will appear as shown below:

    In the Categories and Projects panes shown above, select Java and Java Application respectively and then click Next. This raises a second dialog with the title New Java Application, in which you are supposed to enter the name and location of your project. Enter Lab01 as the Project Name. and set the Project Location to csci/273.002/labs. Also, for this lab only, uncheck the Create Main Class checkbox:

    Otherwise, accept the default settings and click Finish.

    Now go to the NetBeans explorer window. Make sure that the Projects view is selected, open your Lab01 project, then open Source Packages, and look for <Default Package>. If you want to create a new Java class in the default package, just right-click on <Default Package> and select the type of class you want (for example, Java main class), and start editing. But first you should check out the next step...

  6. Now switch from the Projects view to the Files view. Again, first open your Lab01 project folder, then look for a folder named src. This is where the Java source files in your "default package" actually reside. It is also the folder into which you now want to download a couple of textbook demos.

    Use your browser to access the Booksite, and download the java source files HelloWorld.java and UseArgument.java from Section 1.1 in your text. Be sure that both files end up in Lab01/src/.

  7. Attempt to compile and run each of these files separately using the NetBeans environment. When you are satisfied that all is well, show your results to you lab instructor.
  8. Now build your complete Lab01 project. This not only compiles all the files in your project, but it also creates a JAR file named Lab01.jar and stores in in a subfolder named dist. After building, you should use the Files view to locate this JAR file.

    Note that Lab01.jar contains only the bytecode (.class) files for the project classes, not the project source files. But you can still run any of the classes in your project from this JAR file, using only terminal commands such as the following:

    
        cd ~/csci/273.002/labs/Lab01
        java -cp dist/Lab01.jar HelloWorld
        java -cp dist/Lab01.jar UseArgument Fred
        
    It turns out to be convenient to use this approach when you want to try any of the textbook demos that require command-line arguments. In fact it is much more awkward to supply or change these arguments through the NetBeans IDE. But by combining the use of NetBeans with terminal commands, you have the benefit of working with a powerful editor with many good debugging features, plus a flexible way to supply and modify command-line arguments to Java programs.


What To Submit

When your project is complete and working correctly, be sure to demonstrate it to your lab instructor. Then, before you exit NetBeans, clean your Lab01 project. This step removes all the .class files, but leaves all your project sources (.java files) intact. Finally, before you logout, open a terminal window and use the command


    cd ~/csci/273.002/labs
    
to reset your default directory to csci/273.002/labs. Then create a JAR file of your Lab01 project folder, using the command
jar cf Lab01Project.jar Lab01
Note that this JAR file differs from the Lab01.jar file that NetBeans creates when you build a project. Lab01Project.jar contains your entire Project "development" folder, including all your sources and NetBeans project management files. In fact you could upload this file to another system, extract its contents via the command
jar xf Lab01Project.jar
and use NetBeans to continue working on it.

Please leave Lab01Project.jar in your csci/273.002/labs directory for the remainder of the semester.


Last revised 5 February 2009, 10:05 am