In this chapter, you'll compile and run the First application you generated in the previous chapter. The Build menu and the Project toolbar are both useful for turning your programs into executable programs. The Visual J++ compilation works a little differently from other compiled languages, such as Visual C++, as you'll see in this chapter. Once you compile your program into bytecode you will be able to see the program's results by viewing the program inside a Java-enabled Web browser or from within the JView applet viewer that comes with Visual J++.
Sometimes, you'll compile a program and find errors in that program. This chapter introduces you to the three kinds of errors that you may find and explains how to locate one of the most pesky kinds of errors: syntax errors.
Microsoft did a good job when they implemented the Developer Studio. The Developer Studio is extremely powerful but you don't have to master all the ins and outs of the Developer Studio before you can be productive. One example of Developer Studio's ease is found in its compilation tools, most of which you'll find in the Build menu as well as on the Project toolbar.
Remember that you must compile a program before your computer can run the program. The Java programs that you build generally consist of several files located in a project. You do not need to compile all of a project's files because many of the files are graphic images, and HTML scripts. Nevertheless, every project should contain at least one source code file that you need to compile into bytecode before you can send the project's applet along with the HTML file over the Web.
Although chapter 3, "Working Within Visual J++," discussed the various menu bar commands, chapter 3 did not go into great detail about the Build menu because you did not have any source code at the time to compile. The Build menu will make much more sense now that you have generated a complete project in chapter 4, "Using the Visual J++ Wizards."
The following sections briefly explain each of the Build menu's commands. Read through the command explanations to ensure that you know the difference between building a project and compiling a source code file.
The Build, Compile command compiles the source file inside the currently-active editor window. As you compile the file, Visual J++ displays a compilation progress report in the Output window that appears beneath the Project Workspace window. If you do not first display a compilable source code file in the editing window, Visual J++ grays out the Build, C ompile command making the command unusable. For example, if an HTML file is all that appears in the Editing window, you cannot compile the file so the command is unavailable.
The Build, Build <project name> command (your current open project appears in place of <project name> on the menu) compiles all uncompiled files within the currently open project. Choosing Build, Build builds your project and targets that project either to the Debug or the Release version.
To help speed up the build, if any files in your project are already compiled, Build will not recompile them. Therefore, if you were to make only one change to one source file within the project, then issue the Build, Build command, Visual J++ recompiles only the one source code file.
During your program's initial building and testing, set the target to the Debug version. Once you complete and thoroughly test your project, make the target the Release version. The Debug version is slower and larger than the Release version because the Debug version contains symbolic debug information that will help you test the program and find errors that may exist. (You can select the build target from the Build, Set Default Configuration command, described the later "The Build, Set Default Configuration Command" section.)
As with the Compile command, Visual J++ displays the progress of your build during the process. Depending on the size of your project and the number of source files, the build might take a few minutes.
When you want Visual J++ to rebuild every file in your current project, including those files that have not changed and that have no dependencies, choose Build, Rebuild All. When you change the target mode from Debug to Release, or when you want to ensure that all files are built properly, choose Build, Rebuild All.
The Build, Batch Build command lets you build multiple targets (such as a Debug and a Release version target). When you select Batch Build, Visual J++ opens the Batch Build dialog box (see figure 5.1). If you check both options and then click the Build or Rebuild All buttons, Visual J++ builds both targets.
Select the targets for the batch build.
Click the Cancel command button to cancel the building of the targets.
Choose Build, Stop Build when you want to cancel a build or compile currently in process. The shortcut key for Stop Build is Ctrl+Break. You may want to stop the building of a project if you remember a feature you forgot to add before building the project.
When you choose the Build, Update All Dependencies command, Visual J++ performs a check of all your source files and resource files to make sure that the internal list of dependencies is accurate. As you build a project, Visual J++ keeps track of dependencies to speed the subsequent compilation of your project.
If you were to add a new file to a project that is a resource file, for example, Visual J++ cannot automatically know if another file depends on that new resource file until the next Build command. After adding such a file, you can choose the Build, Update All Dependencies command to refresh Visual J++'s internal dependency list so that subsequent builds will build the proper files without having to rebuild everything else to get the missing dependent information.
The Debug submenu that appears when you choose the Build, Debug command starts your application's debugger. The Debug submenu offers the following commands:
Chapter 8, "Using Visual J++'s Debugging Tools," explores the Debug submenu in great detail. You will quickly master the Visual J++'s debugging tools because they help locate program problems for you.
when you want to run your compiled application, choose Build, Execute <project name> (your project's name will appear in place of <project name> in the menu). Visual J++ runs your program outside the debugger environment. You'll see all your program output as if you'd run the program from Windows or, if your project contains an applet, from your Java-enabled browser.
Where's the Link?
When you choose the Build, Settings command, Visual J++ displays the Project settings dialog box (see figure 5.2).
Control your project with the Project Settings dialog box.
The Project Settings dialog box is divided into sections by the tabs displayed along the top of the dialog box. The tabs let you access general, debug, and Java information settings. (Depending on your release date of Visual J++, you may see additional tabs in the Project Settings dialog box.)
The Project Setting's dialog box General page has options for both the Debug and Release targets. If, for example, you want to change the settings for your project's Debug target, click the plus sign next to First - Java Virtual Machine Debug to expand the folder and show all the project's files. You can change the output directory Visual J++ uses for your paths and compiled program output. In addition, if you highlight a particular file within the open folder, Visual J++ lets you exclude the file from subsequent builds if you mark the Exclude File from Build option.
When you expand one of the Project Settings targets, the Debug and Java tabs go away. To see these tabs once again, collapse the open target folder.
The Debug tab contains options that let you specify the debugging session class path as well as the category of options that you want to use for the debugging. Chapter 8 explores these debugging options more fully.
The Java tab lets you set the proper warning level for your error messages. Level 1 warnings are most severe. If Visual J++ finds anything wrong with your code, Visual J++ does not create an executable module. Warning level 4 is the least severe and lets many common warnings go undetected so you can see your program's execution even if warnings appear in the program. The fourth warning level lets you compile your program and see results sooner than you otherwise would be able to do, but you run into danger of an error appearing that can quickly turn into a bug if you don't reset the warning level to a more severe level and recompile.
You can optimize jumps into bytecode by checking the option on the Java page. Although this option produces more time-consuming compiles, your bytecode should execute faster. The Generate Debug info option is checked when you build a Debug target version of your program.
When you choose Build, Configurations, Visual J++ displays the Configurations dialog box (see figure 5.3). The Configurations dialog box lets you add a new configuration to an existing project or remove a configuration from a project. When you add a new configuration, you can select it and change its settings in the Project Settings dialog box described in the previous section.
Add or remove configuration targets to your project with the Configurations dialog box.
The Build, Subprojects command lets you add a subproject to your existing project's workspace. The Subprojects dialog box, shown in figure 5.4, lets you create a new project and add it as a subproject by clicking the New button. (The New button performs the same task as the Insert, Projects command.)
You can insert subprojects into your current project.
The Build, Set Default Configuration command opens the Default Project Configuration dialog box (see figure 5.5). The next build will build whatever target you've selected in the Set Default Configuration dialog box. The Debug target is larger and runs more slowly than the Release target, but when developing a project, the Debug target lets you utilize the debugger's features and symbolic references so you can check program values during a program's execution.
Select the target you want to build.
Figure 5.6 shows the Project toolbar. As you can see from the callouts, many of the Project toolbar buttons mimic the Build menu commands. Use the toolbar for faster access to common compilation and building commands. For example, you can see how selecting from the Select Default Project Configuration drop-down list box is simpler than opening the Default Project Configuration dialog box to change from a Debug to Release target. The toolbar gives you one-button compilation and even offers program execution from within the Debug mode.
The Project toolbar lets you see your project run as soon as possible.
Although a typical Visual J++ project requires extensive dependency checking and compilation, you can click the Go toolbar button to begin executing the project as soon as you complete the last line of source code. Go automatically builds your project if the project is not already built. You can click Yes if you want Visual J++ to rebuild your unbuilt project files (see figure 5.7). If you click No, Visual J++ runs your project based on the results of the most recent build.
The Project toolbar lets you see your project run as soon as possible.
Now that you've generated the code and HTML file in the project named First, you're ready to see the project in action. Obviously, you need to compile your Java source code and view that compiled program either in a Java-enabled browser or using the applet viewer that comes with Visual J++.
If you build and execute your project without using the applet viewer, Visual J++ will perform these tasks:
Before compiling and executing your First application, locate your Java-enabled browser. If you do not have a Java-enabled browser, you can use the Visual J++ applet viewer described in the following section, "Using the JView Applet Viewer."
The following two browsers will work and there are others that might work as well:
Netscape Navigator lets you know if another Web browser is your default browser.
The quickest way to compile, execute, and load your Visual J++ applet is to choose Build, Execute First. Visual J++ will do all the work and, assuming that no errors exist in your generated code (and there should be none unless you edited the code), you'll get no error messages and Visual J++ will load your browser and display the First application's HTML page that you generated in the previous chapter.
Figure 5.9 shows the Netscape Navigator Web browser displaying the First.html Web page. The applet appears in the window. The spinning globe will appear after the applet loads all the .Gif images into memory. If you click the Source File hypertext hotspot labeled, your Web browser should display the source code (as per the HTML page's <A HREF> tag command described in chapter 4) as shown in figure 5.10.
Viewing the First application's Web page and Java applet.
Click the hotspot to view the Java program's source code from within the Web browser.
The applet's title, First, referenced in the HTML file with the <TITLE> and </TITLE> tags, appears in Netscape Navigator's window title bar at the top of your Netscape Navigator screen in square brackets.
If you use Internet Explorer, Visual J++ easily opens and loads the First applet. Figure 5.11 shows your First applet displayed by Internet Explorer. Unfortunately, Internet Explorer does not display the Web page's title in the Internet Explorer title bar.
You can use Internet Explorer to display the First Web page and run the First applet.
Visual J++ contains an applet viewer, named JView, with which you can view your applets without a Web browser. JView is an MS-DOS program, so you can run JView from the command line or from the Start menu's Run command.
To start JView, perform these steps:
The JView applet viewer can run Visual J++ applications as well as applets. A Visual J++ application contains a main() procedure that begins with the procedure definition shown here:
public static void main(String args[])
If you've created your application with the New Project Wizard, Visual J++ adds the main() procedure for you. You can use the wizard's main() procedure format in the applications you create from scratch. The occurrence of the main() procedure designates the program as a stand-alone application. Without main(), your program can only be an applet viewed with a Java-enabled Web browser.
Although chapter 8 goes into great depth explaining how to use Visual J++'s debugging capabilities, you should already know what to expect if you attempt to compile a program that contains errors. Errors can inadvertently slip in if you press the wrong key while viewing source code in the Editor window.
This section explains how to deal with syntax errors and chapter 8 explains how to deal with logic and runtime errors. A syntax error is an error in typing, such as a spelling error or an error in Java language grammar. A logic error is an error in your logic that occurs when you tell the program to do something that you did not really intend for the program to do (such as printing payroll checks with negative dollar amounts). A runtime error is an error that your applet viewer (or Web browser) finds during the applet's execution. For example, if your program attempts to display a graphics file that does not exist, your program cannot perform such an impossible feat and, instead of giving incorrect results, your program will generate a runtime error to let you know about the problem.
Visual J++'s Output window, that appears below the Project Workspace window when you compile a Visual J++ program, displays syntax errors that might arise when you compile a program. For example, figure 5.12 shows the result of a program's compilation. The Visual J++ compiler found two errors. The errors occurred on lines 34 and 39 (the 11th and 9th characters on those lines, respectively).
Errors appeared during the program's compilation.
Not only does Visual J++ inform you of the syntax error locations, the Developer Studio gives you a simple way to jump right to the error so you can analyze and fix the problem. All you need to do is double-click the error message that you want to fix. Visual J++ takes you directly to the offending line in the source code file. For example, after double-clicking the first error message from figure 5.12's Output window, Visual J++ jumps to the offending line as shown in figure 5.13. The blue arrow to the left of the error shows you right where the error occurred.
Jump directly to the error by double-clicking the error message.
Once you locate an error, you can fix it or move to the next error. To move to the next error, you could double-click the next error message, but you can also press the F4 key to jump to the next line with an error. Press Shift+F4 to jump back to the previous source code error line. The F4 and Shift+F4 keys let you jump back and forth, as needed, to fix errors throughout your source code.
Once you fix the errors, you can rebuild the project or recompile the source code file if only one source code file exists in the project. When no errors appear, you'll be able to run the applet and begin your hunt for any runtime or logic errors that might exist.
The goal of this chapter was to explain how to understand and utilize the Build menu to turn your generated Visual J++ program into runnable code. The Build menu contains commands that can both compile individual source code files as well as compile every compilable file in your project and prepare the project for execution. In addition to the compilation tools, the Build menu contains option settings that let you control the way that the builds take place.
After building your project, you can see the program's results using either a Java-enabled Web browser or Visual J++'s own JView applet viewer. Perhaps Internet Explorer is the most seamless Web browser you can use with Visual J++ because of Microsoft's endorsement of the Internet Explorer product, but other Java-enabled browsers work as well, such as the Netscape Navigator. If you don't want to use a Web browser, you can run your applet through the JView applet viewer to see the results without the Web page getting in the way.
Compilations don't always go well. When errors appear, you can find them easily by jumping directly to syntax errors within your code. You'll be able to fix each error by jumping directly to each error in the program. Once you fix the syntax errors, you can then run the program to test its authenticity.
Here are the points this chapter covered:
| Previous Chapter | Next Chapter |
| Table of Contents | Book Home Page |
| Que Home Page | Digital Bookshelf | Disclaimer |
To order books from QUE, call us at 800-716-0044 or 317-361-5400.
For comments or technical support for our books and software, select Talk to Us.
© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.