Lab 1 Command Line Java and Eclipse The Lab Course Like CPSC150 lab, you will read the lab and answer questions and get your instructor's signature when the lab requests it. Your lab grade will be based on the signatures you collect for each lab. This semester, you will have much more room for creativity. This will also require you to research and think more. So, your textbook (and the web, your instructor, your fellow students, and your lecture notes) will be helpful resources even when the lab does not refer to the textbook directly. Background In CPSC150, you used BlueJ as an Integrated Development Environment (IDE). This semester, you will use Eclipse which is a professional IDE. This means that Eclipse has more powerful features although their sheer number can be intimidating sometimes. Like BlueJ, Eclipse is free and runs on Macintosh, UNIX, Linux and Windows. It can be downloaded from www.eclipse.org. Similarly to CPSC150, you will use your PCSE UNIX account into login to the lab computers. Programs will run locally, but your data will be saved on the UNIX servers (which you can access using ftp from any Internet‐enabled computer). To protect your files, you should always logout after using any computers in the lab. 1. Command line Java Before beginning Eclipse, the first part of this lab will review some Java, some command line instructions, and (possibly new) material about the main method. To run a program written in Java from start to finish, you must have a main method, which is the method from which all Java applications start executing. Exercise 1 1. Create a Java file using any text editor you like (you may use BlueJ if you want). Name your application Lab1.java (BlueJ and Eclipse will add the .java automatically). 2. In this file, write and compile a program that prints all of its command line arguments, followed by a line displaying how many arguments there were. For example, if you run this in a terminal window, you would type: And it would display: In BlueJ, you would run it by choosing the main method, and its parameter be: {"here", "1", "are", "2", "some", "3", "arguments", "4"} (the curly braces already exist in the parameter prompt). Here are some hints: Remember the main method header is: public static void main(String[ ] args) You do not need to “read” from the command line. All of the command line arguments are automatically stored in the array args when main begins. Thus, in the above example, args = {"here", "1", "are", "2", "some", "3", "arguments", "4"}; and args.length is 8 at the beginning of the method. The array args is like any other String array. You can process the args String array using a for loop or any other array iteration you know. You will not need any constructors, and will need only one method, main. Test Your Code Run your application from a command prompt (in Windows) or a terminal (on a Macintosh). Use cd (change directory) to navigate to the directory where your Lab1.java program is located. Compile it with: javac Lab1.java Run it with: java Lab1 here 1 are 2 some 3 arguments 4 When your application is running successfully, get your instructor's signature. Exercise 1 ‐ Get Instructor’s Signature 2. “Hello World” in Eclipse In the exercise below you will be given a step‐by‐step introduction to your first Java program in Eclipse. Exercise 2 1. Start Eclipse by choosing it from the list of available programs in your computer. 2. Once it starts running, Eclipse will ask you the folder in which your workspace is located. The workspace is a folder in which Eclipse will save Java programs you create. If this is the first time you are using Eclipse (and we assume it is), use the browse button and create a new workspace folder (e.g., cpsc250) under My Documents. This folder is stored in your UNIX account, and can be used the next time you open Eclipse in the lab computers. 3. After selecting a workspace folder, Eclipse will display the Welcome screen shown in Figure 1(If you do not see this Welcome screen, you can get to it by choosing the menu options Help and Welcome). 4. Move the mouse over each of the icons to see what they do, and choose the one that says Tutorials (shown in Figure 1 as the icon with a green check mark). Clicking this icon will display a new screen with several hyperlink options. Select link saying Create a Hello World Application tutorial. Eclipse should display a screen resembling the one in Figure 2. This Eclipse screen has a lot of items in it, which we will get more acquainted with as the semester advances. The most important items are: A perspective, which is the name given to an entire Eclipse screen with tabbed windows. The screen shown in the figure is called the Java perspective (note that there is a “Java” button close to the upper right corner of the Eclipse window). A view is each and every one of the tabbed areas in the perspective; for example, the tabs named “Package Explorer”, “Task List” and “Outline” are views of the Java perspective. 5. The Welcome view at the far right shows the series of steps that you should follow to create a “Hello World” application (“Hello World” is geek‐speak for “one of the simplest programs you can write in a language”). The tutorial will also introduce you to the concepts of: a project, which is a workspace folder with all files that belong to one Java program a package, which is a namespace within the src folder where Java source files are stored. 6. One of the features of Eclipse is that it will compile your source code as you write it. If there is a compile error, you will notice a wavy red line underlining in some of your code. Do not worry if this Figure 2. Eclipse’s “Hello World” Tutorial. Figure 1. Eclipse’s Welcome Screen. happens when you are typing. However, once you have written a statement you believe to be correct, look to see if there were any underlined words. This would be an indication that your statement is not correct and has a compile error. Figure 3 shows code with a compile error. Another way in which Eclipse indicates compile errors is by displaying a crossed red dot circle on the left margin where the error is found. Hovering the mouse over a compile error shows the type of error and possible solutions (also known as Quick Fixes). Note: Sometimes, Eclipse will show a compile error even though the statement you just typed is correct. This happens because Eclipse has not yet automatically re‐compiled your code. However, we can force Eclipse to compile it by saving your file (use Ctrl‐S to save the foremost file, or Ctrl‐ Shift‐S to save all opened files). At any point, you can tell whether a file needs saving if there is an asterisk “*” before the name on the tab of an open editor view. For example, Figure 3 shows that the file “HelloWorld.java” has not been saved because it has a small “*” before the file name. If the error is not gone after saving your file then you will need to look closely at the offending code—at that point you need to figure out why it is wrong! 7. Get your instructor’s signature once your program is free of compile errors and it displays “Hello World!” in the Console perspective when executed. Note: Sometimes when asking Eclipse to run your program, the dialog shown next will indicate that there is a compile error in your program and will ask you whether you would like to execute it anyway. Be aware that this means that Eclipse will execute the last successfully compiled version of your code (which could be days old in the case of large programs). Figure 3. Unsaved “Hello World.java” with syntax errors. It is strongly recommended that you do not proceed. Rather go back to your code and find any files in the project that have errors and fix them before running. Exercise 2 ‐ Get Instructor’s Signature 3. More about Eclipse and Java You are encouraged to use the Welcome screen to learn more about Eclipse. It is rich with features and useful tools. Here are a few “getting started'' suggestions: You may have noticed that there were several views open in Eclipse when you were writing the “Hello World” application: Package Explorer tells you the project and packages in the workspace where your source code is saved; Console shows the output (the result of “print lines”) of your program; and Problems tells whether there are compile errors in the workspace. To close a view, click on the “X” on its tab. To open an editor view with your source code, find the file in the Package Explorer view and double‐click on it. For all other views, use the menu options Window and Show View. Once you compile your .java source code, Eclipse creates compiled files named .class (e.g., Lab1.java is compiled into Lab1.class). Just as the Package Explorer shows a src folder for your source code, there is a (usually hidden) bin folder where class files are stored. Eclipse can indent any selected source code by pressing Ctrl‐I (or choosing the menu options Source and Correct Indentation). You can select all source code in a file by pressing Ctrl‐A. Eclipse can hide parts of your source code if you want, and will often hide automatically all of the import statements at the top of source files. To see how this feature works, find and press on the circled “‐“ on the left margin exactly to the right of your main method in the “Hello World” source code. This will hide all the body (i.e., code within curly braces ) of the main method. Note that the “‐ “ is now a “+”. Pressing on the “+” makes the code re‐appear. Hovering the mouse over the name of a Java class or method, displays a pop‐up with its Javadoc description (Javadoc is the name given to HTML documentation describing the methods of a Java class). You can find the Javadoc of all Java classes, also known as the JDK API (Java Development Kit Abstract Programming Interface) by looking Java 6 API in Google or by directing your web browser to http://java.sun.com/javase/6/docs/api/. Be aware that this URL may likely change as Java releases new versions. Also note that when writing your source code statements, Eclipse may suggest ways in which these statements may get completed. This feature is (fittingly) called auto‐completion. For example, when writing System and a dot, Eclipse will display a list with possible options, including in and out. Selecting out, displays another set of options that include various print and println. If for whatever reason auto‐complete does not appear you can always invoke it by pressing Ctrl‐Space. 4. Importing Lab1 into Eclipse In this section, we will be importing the Lab1 Java file you wrote into your Eclipse workspace. If you initially created your Lab1 in Eclipse then you can skip to “test your code” part below. Exercise 3 1. Create a new project. There are different ways to do this: by using the menu options File, New and Java Project; by using the New Java Project icon on the toolbar (hover the mouse over the icons to see their names), or right‐click on an empty part of the Package Explorer to bring up a popup menu from where to choose the options New and Java Project. 2. Selecting these options will open a dialog box titled New Java Project. Write Lab1 as the name of the project then press the Finish button. This will create a Lab1 project in the Package Explorer. 3. Within the Lab1 project in the Package Explorer view, find the src folder and right‐click on it. This will display a popup menu. Select the option Import, which will open a dialog box. Select the options General and File System, and press the Next button. The next section will require you to locate the folder where your Lab1.java file is located: use the Browse button to locate this directory and then check the Lab1.java file shown in the list of files in that directory (if the file did not show, then you are browsing to the correct folder). Do not import the Lab1.class file. Click on the Finish button. Figure 4. Importing the “Lab01” class into the “Lab01” Project’s the source folder. 4. In the Package Explorer view, you should see that the Lab1 project has a src folder within which there is a default package with the Lab1.java file. Your workspace should look similarly to the one shown in Figure 4. Test Your Code Run your application. You can run most programs by right‐clicking on the file in the Package Explorer view and selecting the popup menu options Run As and Java Application. In this case, this option will not allow you to enter your command line arguments. Instead, select the popup menu options Run and Run Configurations. This selection should show a dialog box with several tabs. One of these tabs is labeled Arguments. Click on it and enter the arguments in appropriate text area, then press the Run button (make sure that Lab1 is the project to execute).As shown in the figure above, you should see your results in the Console view. Get your instructor’s signature once your application is running successfully. Remember to LOG OUT before you leave the lab. Exercise 3 ‐ Get Instructor’s Signature Lab 1 Instructor’s Signature Page Student’s Name: ___________________________________________ Student ID _________________ Exercise 1: ___________________________ Comments: Exercise 2: ___________________________ Comments: Exercise 3: ___________________________ Comments: