Laboratory 0: Linux Operating System and Java

January 24, 25, 2006

Objectives

The objectives of this laboratory are
  1. to familiarize yourself with the Linux operating system,
  2. to provide practice editing, compiling, running and revising a Java application.

1. Introduction

What is the Linux operating system? Linux is an open-source operating system published under the GNU Public License. Open source means that anyone is free to use and modify any of the actual source code. The Linux operating system is touted for its speed, minimal hardware requirements, security and remote administration. Linux is a fully featured operating system which does not cost a dime. You download many different distributions directly from the Internet, and the support community is huge. Because of Linux's speed and stability, it has become the operating system of choice for most types of servers and permeates the entire web market.

There are thousands of file installed with the operating system, and users and administrators may create thousands more. To bring organization to all this information, directories (or folders) are created. The following figure represent a typical Linux file system.

2. Logging in

To use Linux, you must first identify yourself to the system. This is done so that the system knows who you are, what permissions you have, and what your preferences are. To identify who you are, you have a username. You also have a password, so no one else can pretend to be you. When you start Linux, you will see a prompt---i.e., a request from the computer for some information. In this case, you must first select a session by clicking on the word Session in the upper left corner of the login dialog box. Then select KDE as the desktop session to use once you have logged on to the system. At the prompt for login, you should enter your username and press Enter. When the system requests your password, enter your password and press Enter. If you type your username or password incorrectly, you will have to start this over. If you do it correctly, you will see a brief message and then the KDE desktop.

KDE is a network desktop environment for Unix/Linux workstations. It fills the need for an easy to use desktop for Unix/Linux workstations, similar to the desktop environments found under the Mac OS or Microsoft Windows.

The graphical desktop should not pose any problem for the Windows or Macintosh users. The main components are the icons on the desktop and the panel at the bottom of the screen. The panel consists of several areas. The house icon provides quick access to your home directory with all subdirectories. The other icons execute applications such as, a terminal window, a web browser, an e-mail client, etc. See the following figure for more details.

3. The Firefox web browser

The Mozilla Firefox browser is an award winning preview of the next generation browser. Firefox empowers you to browse faster, more safely, and more efficiently than with any other browser. It is a web browser developed as an open source program with standards, and it is portable across different computing platforms. To start Firefox, click the Start Application icon (which looks like a gear) on the KDE panel and then select Internet -> Mozilla Firefox.

To see the CPSC 115L homepage, type the following URL in the address box.
http://www.cs.trincoll.edu/~cpsc115/

4. Creating a cpsc115 directory

Click the Personal Files icon (which looks like a house) on the KDE panel. This will open the file manager window in your home directory. Once you have the file manager window displayed, right-click anywhere inside the window and then select Create New -> Folder. Name the new folder cpsc115 (all lowercase). All of your programs for this course will be stored in this folder. Remember how to find this folder because you are going to need it every week.

5. File and directory access permissions

File and directory access permissions are defined separately for the following three categories of users: For files, there are three categories of permissions: For directories, there are also three categories of permissions: Here, the execute permission on the directory means not only to allow reading the files in the directory but also to allow viewing their attributes, such as the size and the time modified. To see permission information (and more) of a file/directory, right-click on the file/folder, select Properties, and then click the Permissions tab.

Now, change the access permission of the cpsc115 directory as follows:
  1. Right-click on the cpsc115 folder.
  2. Select Properties -> Permissions.
  3. For Owner, select Can View & Modify Content.
  4. For Group, select Forbidden.
  5. For Others, select Forbidden.
  6. Click OK.
Throughout the term, you should maintain the access permission of the cpsc115 as above so that no one besides you can access your files.

6. The KWrite text editor

KWrite is a simple text editor, with syntax highlighting, dynamic word wrap and more. In addition to text editing, KWrite can also be used to view and edit various types of source code with syntax highlighting.

Launch KWrite by clicking the Start Application icon (which looks like a gear) on the KDE panel and then selecting Editors -> KWrite. Now, perform the following.
  1. Copy the following text and Paste it into the blank KWrite window. You can also type the text but make sure to enter exactly as shown.
    public class HelloWorld {
      public static void main(String args[]) {
        System.out.println("Hello World!");
      }
    }
    
  2. Select File -> Save As to save the file in your cpsc115 directory. Name the file as HelloWorld.java. Notice that the name of the Java file and the name of the Java class are identical.

7. Compiling and running the HelloWorld.java application

To compile and run HelloWorld.java, open a terminal window by clicking the Terminal Program icon (which looks like a monitor). When the terminal window opens and the ~$ prompt appears, use the following commands to compile and run the HelloWorld.java application.
  1. cd - This command changes the working directory to the one specified. Set the working directory to cpsc115 by entering the following.

    cd cpsc115

  2. javac - This command compiles the application into Java bytecode and then save the compiled file with the .class extension. Compile your Java source file by entering the following.

    javac HelloWorld.java

  3. java - This command interprets the Java bytecode of a compiled file and produces an output. Run your program by entering the following.

    java HelloWorld
As a result, something like the following should appear on your desktop.

8. Editing and re-running the HelloWorld.java application

Next, perform the following.
  1. Open the source code HelloWorld.java by clicking anywhere inside the KWrite window. The program contains the following code:
    public class HelloWorld {
      public static void main(String args[]) {
        System.out.println("Hello World!");
      }
    }
    
    Change "Hello World!" to some other message by editing the System.out.println() statement on line 3. Select File -> Save to save the updated file. Re-run the program.

  2. Notice that the name of the class in HelloWorld.java is HelloWorld. The rule in Java is that the Java source file must have exactly the same name as the class the file contains.

    Change the name of the class in HelloWorld.java to TrivialApplication, save the updated file, and then try to re-run the program.

    A system error will occur when Java tries to load the TrivialApplication class into memory but cannot find it. You can fix this error by changing the class's name back to HelloWorld. Save the updated file and re-run the program.

  3. A syntax error is an error generated by the compiler when it cannot correctly parse your source program. There are lots of ways to generate a syntax error: misspelling Java keywords, forgetting to declare a variable before using it, misspelling a previously declared identifier, even forgetting a semicolon at the end of a statement, etc.

    Delete the semicolon after the System.out.println() statement and then re-run the program.

    This time you should get an error message:
    HelloWorld.java:4: ';' expected
      }
      ^
    1 error
    
    Add a semicolon and run your program again.

9. What are unethical bahaviors involving computers?

As computers become indispensable in everyday life, it is very important to be aware of ethical issues surrounding computers.

In the file HelloWorld.java, below the Java program, give a computer-based example of each of the following unethical behaviors:
  1. breaking into a room or office,
  2. going through someone's desk,
  3. vandalism,
  4. harrassment,
  5. plagiarizing.

10. Optional exercises

Linux is an open-source operating system that is built on the well-known UNIX operating system, the most popular command-line based operating system for workstations and multi-user servers. So, most Linux commands (including all the commands you learned today) are actually UNIX commands.

If time permits, to familiarize yourself with more basic UNIX commands, read "Introduction to the UNIX operating system" in the UNIX Tutorial for Beginners available at
http://www.cs.trincoll.edu/~pyoon/unixtut/
and complete Tutorials 1 and 2.

What to hand in

Make sure to write your name and laboratory section (Tuesday or Wednesday) in the HelloWorld.java file. Save your work. Upon completion of your laboratory exercises, have your work checked by the laboratory instructor or TA. Turn in a printout of your HelloWorld.java file.



*CPSC 115L home page


Computer Science Department
Trinity College
300 Summit Street
Hartford, Connecticut 06106-3100