CS520 – Setting Up the Programming Environment for Windows Suresh Kalathur 1. Java8 SDK Java8 SDK (Windows Users) For Windows users, download the Java8 SDK as shown below. The Java Development Kit (JDK) can be downloaded from the following web site: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html Accept the License agreement. Download and execute the appropriate exe file. (If you use the 64-bit version that ends in –x64.exe, make a note of it to use the 64-bit Eclipse IDE in Step 2). Proceed and complete the installation. At some point, a window is prompted to select the destination folder for JRE. Click Next and proceed through the installation. 2. Eclipse IDE Eclipse IDE is the only supported IDE for the course. Download the Eclipse IDE from the following web site: http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/mars2 Download and extract the contents of the corresponding file for your Operating System. The extracted content appears in the Explorer as follows: 2.1. Running Eclipse IDE Start Eclipse by double-clicking the above application. You will be prompted for the workspace folder location. In my case, I created the following folder under my home directory and choose it as the workspace location. (The workspace location can be switched at any time from Eclipse using the File->Switch Workspace menu option.) Click OK to proceed. Close the Welcome screen if it is open. You can keep the following windows as shown below. 2.1. The Navigator View The Navigator View provides a natural hierarchical structure of the Java projects and the files they contain. Close the Package Explorer tab and Open the Navigator View through the menu item Window -> Show View -> Navigator. 3. Creating a new Java Project In Eclipse, select File->New->Java Project. Specify the project name, say, Sample1, as shown below. A folder named Sample1 will be created in the workspace location. Click Finish. The sample project’s structure is as shown below in the Navigator view. The src folder within the project will be used for creating and placing the Java source code files. Eclipse uses the bin folder for the compiled files. 3.1. Creating a Java source file With the src folder of the project selected, do a right-click and select the menu option New->Class (or from the File menu) as shown below. Eclipse now prompts for the information regarding the new Java class that is being created. Type cs520 for the Package field, Test1 for the Name field, and select the checkbox for the method stub public static void main…. The selections are as shown in the figure below. Click Finish. The file Test1.java is now created under the src/cs520 folder. The package cs520 corresponds to the folder under which the respective java files should be placed. The java class Test1 should match with the name of the file Test1.java. A dummy main method stub is also created. When the code is run, the statements in the main method are executed. Modify the content of the file Test1.java as shown below. The println statement displays the provided information to the console when the program is executed. 3.3. Compiling and Running (or Executing) the program Select the source file, Test1.java, in the Navigator pane on the left hand side. Right-click on the selected file and select the Run As->Java Application option in the pop up menu. The java source file is first compiled which ensures that there are no syntax errors in the code. If the compilation phase succeeds, the program executes and the output is shown in the Console window as depicted in the following figure. Another option to run the program is to select the source file, Test1.java, in the Navigator pane on the left hand side and click the menu Run. In the resulting drop-down menu, select the Run option. 3.4. Modifying the Code Modify the code in the Test1.java file as shown below. The program declares two integer variables, number1 and number2, with initial values 10 and 20 respectively. The two numbers are added and assigned to the variable sum (line 9). The println statements are used to print out the values. The first println statement displays the sum of the two numbers. The second println statement shows the individual numbers along with their sum. The output from executing the program is shown below. 3.5. Debugging Java Programs Debugging allows the programmer to step through the code and watch the variables and their values. This allows the programmer to fix any errors in the logic when the output is not what is expected. The following example shows a simple scenario. The program execution can be paused at any point by inserting breakpoints into the source code. Select the line number 7 in the Test1.java code. Insert a breakpoint using the menu Run->Toggle Breakpoint. The breakpoint is shown at the left of the line number as in the following figure. To debug the program, select the menu Run->Debug option. Eclipse prompts the user to switch to the Debug perspective. Select the checkbox to remember the decision and click YES. The program is now being executed in the debug mode and the execution stops at line number 7 as shown in the following figure. Select the menu Run->Step Over option to step over each line of the code. The variables window shows the variables and their values as the code is stepped over each line of the code. After seeing the desired results, click Run->Terminate to exit from debugging. To return to the Java perspective, select the menu Window->Perspective->Open Perspective->Java. 4. Java Documentation The Java programming language has a lot of built in classes. The documentation for all these classes can be viewed online through the following link: http://docs.oracle.com/javase/8/docs/api/