How to Run Java Programs Using JBuilder3(1)

Document Sample
How to Run Java Programs Using JBuilder3(1) Powered By Docstoc
					CS111 - How to Run Java Programs Using JBuilder 7 Computer Science Dept. – Saint Michael’s College
Created September, 1999 by LSH Updates: Jan. 2000 GLM, Aug. 2001 MEB, Sep. 2003 JAT, Jan. 2004 GLM

1. 2. 3. 4. 5. 6. 7. Set up a CS111 Directory structure. Copy the ann Class library from the \\Phantom\CompSci\CS111 area. Create a “skeleton” start-up source file for all your Java applications. Create a project in JBuilder 7. Create a source file from your “skeleton” file. Set the project properties. Compile and run the programs.

Directory Structure
You may store your Java programs either on your Zip disk, a floppy disk or your allocated disk space on the server (also known as the Z: virtual drive). The choice will depend on where you usually do your class work. If you don‟t have a computer in your room I would recommend a Zip disk since all computers on campus have a Zip drive; the latest Zip disks can store up to 750MB of information and the access speed is quite fast. If you do have your own computer with a Zip drive the same recommendation holds. If your computer doesn‟t have a Zip drive, then I would recommend maintaining your files on your own hard drive and bringing them to lab either on a floppy or in your area of the shared drive. You will have to be more diligent about keeping your files current, as you will have multiple copies of each file. (Files stored on a floppy or your Z: drive are slower to access and update than from a Zip disk. The Zip drive on most MIKENET PC labs is now set to E:, therefore this handout will refer to E:.) Whichever drive you select this document assumes the following directory structure:

I assume the F: drive, your PC may be different.

CS111 ann Lab1 Lab2 skeleton

Obtaining the ann software
Your textbook comes with a set of predefined classes that you will be using during the semester – mainly to do input and output to our programs. These files are stored on the server. First you need to connect to the server using My Computer. In My Computer use the Tools + Map Network Drive to connect to the CompSci directory on Phantom. Once you have done that, go to the CS111 directory in CompSci. Copy (drag) the ann directory to the


correct drive (A:, E: or Z:) and place it right over the CS111 folder there, so that it is located as shown in the structure above. You now have copied all of the ann source files into the ann directory above. You will be creating your source files for each lab in the appropriate directory. You may either use My Computer to create the rest of the directory structure above or you can create the Lab directories when writing your programs (as shown in the following text). Note that JBuilder 7 will be creating one or more extra subdirectories in each of the Lab directories – it will also be named ann and will contain the compiled ann classes.

Starting JBuilder 7
JBuilder 7 is the latest version of the Java Development environment we use here at Saint Michael‟s. To start it use Start + Programs + Course Applications + JBuilder 7 Enterprise. You may want to create a short cut for your desktop to run JBuilder 7 (which works only on the PCs in JeanMarie, St. Ed‟s and the Durick Library). Use My Computer select the C: drive, then the Program Files folder, then the JBuilder7 folder, then bin. Right click on JBuilderW.exe and select Create Shortcut then drag the short cut icon to the desktop. Click on that icon whenever you want to run JBuilder 7.

Creating your skeleton:
Your instructor will guide you through these steps.

Creating a Project
JBuilder7 and other Java IDEs (Integrated Development Environments) use project based concepts. A project consists of various types of files that are necessary for compiling and running programs. For the sample programs from the earlier chapters in your textbook, the project will include only one or two source files. To create a project, select the menu choice File/Open Project. A window that is very similar to the one below will appear on the screen:


Browse the Directory: pull-down box to locate the drive you are storing your files on. Then select the CS111 directory. Your window should look like this:

If you have not yet done so, now create a new folder to put your lab work in – say for example it is Lab1. Do this by selecting the create new folder icon as shown below:

A „Create Directory‟ dialog box will appear. Type the directory name you wish to use (e.g., Lab1) and press enter or click OK. Finally, move to that folder by clicking on the Lab1 folder icon.


Now that you are in the folder in which you wish to place the project file, you need to specify the name of the project file. It is important that you enter the extension .jpr for the file you are creating – which is how JBuilder7 recognizes that it is a project file. In the File name field enter the name of your project – for the example I will use Program1.jpr. JBuilder will create the Program1.jpr file for you after you press enter and click OK.

Creating a Source File
Now you‟re ready to add a Java source file to the project. You will want to click on the icon in the upper left corner that has a green „+‟ in it. (Place the cursor on this and Add files/packages will appear.) This will allow you to browse for an existing file. You want to find your Java skeleton source file to add it to the project. After you have done that, and the filename appears under the project file name, you can display that file‟s contents by double clicking on it. Then, you must save that file as a .java file in the current folder you are using via File/Save As. If you don‟t do this first, you‟ll be modifying your skeleton, which is very undesirable. The first letter of the filename (and the first letter of the class name) must be an Upper case letter. Make sure you have updated the saving directory to be your Lab1 folder, and then place check marks in the 1st, 3rd and 4th boxes for the operations that should be performed after the file is saved. (See below for what this should look like before you click OK.) Change the class name from Skeleton to Program1 (for this example), and then set the project properties as described below. Once the Project properties have been correctly set, you may begin to modify the contents of the file by adding or modifying the comments, placing Java statements into the main method, etc.


Setting Project Properties (VERY IMPORTANT)
JBuilder7 is very picky on where you place files and how you name them. To make your project work successfully, it is very important to set the project‟s properties correctly. You use the Project Property dialog to tell JBuilder7 where to find files and where to place the generated class files. To open this dialog, select the JBuilder7 menu choice Project, then select Project Properties. A similar dialog to the one on the next page will appear on your screen except jtrono will be replaced by your own MIKENET username.


You must add the current folder (Lab1) to the list of Sources you wish to use for this project. When you press the Add… button, it should already be set to the correct folder (Lab1), so just hit OK, as shown below.

Select each of the other two default source folders on C: by clicking on them, and then Remove each of them, leaving you with simply the CS111 and Lab1 folders as your sources. Finally, you must add E:\CS111 to the „Source tab‟ because that is where JBuilder7 will look for the ann folder that contains all of the predefined classes we need to use with the CS111 text. Hit „Add‟ again, and click on CS111, and then hit OK. You must now change the output path to the folder that you are working in (E:\CS111\Lab1). This tells the compiler where to store the output of your program (Program1.class for this example). You press the „...‟ button to the right of the output path window to select the Lab1 folder. In this case, you will have to hunt around a little for E:\CS111\Lab1 because JBuilder defaults to storing the .class files in a different folder than the one where your project is located. You should then change the backup path and the working directory. (Both should be stored in your Lab1 folder.)


When you are done with these tasks, the image display of your project‟s properties will look like what is shown below. However, there are two more things you need to do. Click on the General tab, and click in the box next to the sentence Enable source package discovery and compilation and make the check mark disappear. Once that is done, continue to click on OK until you have closed the Project Properties window.

But as I said there were two things left to do in Project Properties and you only did one of them! You must also tell JBuilder where to find the class that contains your main method, which is in for this example. Re-select Project Properties and click on the Run tab, then click on the button named „New‟, click on the „...‟ button, click on the Browse tab if the Search tab is the active one, and then type in Program1 (for this example) in the field which has the label „Class name:‟. (You may also wish to change the project‟s Configuration name to something like YOUR_NAME Lab#.) Click on OK until you exit Project Properties. JBuilder 7 requires that a valid class exists by the name specified in the Run Tab. This is why we had to exit Project Properties and then re-select it. That way JBuilder knew what folders to look in to find the class we specified. If the correct class name cannot be found it indicates that there may be an error in the class we‟re creating or that the class cannot be found in the folders specified for the Source Path.


Quick Summary for Working on Subsequent Assignments
1. Create a project. Name this project differently from the directory that will contain the project. For simple programs the project name can be the same as the class name. Make sure the project name ends in .jpr. 2. Add your skeleton file to the project file. Change the name of the class to what you want, Program2 for instance, and then save it as in the appropriate folder. Finally, update the name of the class from skeleton to the name of your file – and remember to capitalize both the file and class names. 3. Set the project properties. a. You should change the working, backup and output directories, then select the General tab to disable package discovery and exit Project Properties b. Re-select Project Properties and select the Run tab so you can set the classname field (this is the name of the Java file where the main method can be found.) 4. Modify the Java file as you wish, and then click the green arrow to compile and run the program once you are done with your changes. Helpful hints: In the past, having more than one program or assignment being worked on in the same folder (directory) has caused some unforeseen difficulties. This seems to have been fixed in this latest release, but you could be cautious and maintain a separate folder for each program you create. Along the same lines, it is more reliable to have all the folders (directories) you create be subfolders of CS111 and you shouldn‟t create any other subfolders in Lab1, Lab2, etc. Some will be created automatically, but you should stick with the rule one program per folder. It is a good idea to use different names for each of your Java classes that you create as well.


Shared By:
Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail you!