- Using Visual J++ -

Chapter 5

Running Visual J++ Programs


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.

Overview of the Build Menu

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."

 
If you exited Visual J++ at the end of the previous chapter, start Visual J++ and load the First project that you generated in chapter 4. Choose File, Open to open the project named First. You may need to select Project Workspcaes (*.mdp) from the Files of Type drop-down list box to see the First.mdp project file in the Open dialog box.
 
 
Be careful that you do not open the First.java file. Generally, choosing File, Open command displays only Java source files that have the .Java file name extension unless you change the option displayed in the Files of Type drop-down list box.

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

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.

 
Visual J++ does not run the compiled program after the Build, Compile command completes its task. The Compile command compiles but does not run the file.

The Build, Build <project name> Command

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.

 
An exception can occur that forces Visual J++ to recompile more than the single source file that you changed, however. If other source files depend on information within the file you changed, Visual J++ will also recompile those dependent source files to ensure that they have the latest information from the file they depend on for information.

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.

 
Visual J++ compiles at speeds that reach 1,000,000 lines of code per minute! Therefore, you typically do not have to wait very long for a project's build to complete.

The Build, Rebuild All Command

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

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.


FIG. 5.1

Select the targets for the batch build.

Click the Cancel command button to cancel the building of the targets.

The Build, Stop Build Command

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.

The Build, Update All Dependencies Command

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 Build, Debug Submenu

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.

The Build, Execute <project name> Command

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.

 
You do not have to specifically start your Web browser before executing your applet. As you'll see in the last half of this chapter, Visual J++ automatically starts your browser and runs the applet program from within the project's HTML Web page when you run your applet from within Visual J++.

Where's the Link?

If you've worked with compilers in the past, you probably know that a compiled program does not contain enough information to execute. The compiled program must go through the link editor to resolve external references before a computer can actually run the program.
 
Many of today's PC-based compilers automatically send the compiled code through the link editor right after the code's compilation. Visual C++ does this and Visual C++ uses the same Developer Studio that Visual J++ uses.
 
Visual J++ does not link your compiled program, however! Remember that Visual J++ produces compiled bytecode, not a compiled executable file. The bytecode does not need linking because your Java-enabled Web browser interprets the bytecode and executes according to those interpreted instructions. Therefore, you'll find nothing about linking in the Visual J++ environment.

The Build, Settings Command

When you choose the Build, Settings command, Visual J++ displays the Project settings dialog box (see figure 5.2).


FIG. 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.

 
If you make the wrong change to a project option, click the Reset button to restore the options to their original settings.
 
 
As you make changes to the Java page, the small text window labeled Project Options updates with messages such as /w1 /g that may make little sense. If you were to build the current project from the MS-DOS command line, as you're able to do with Visual J++, the Project O ptions window shows you the command-line options you must set to build the project with the options you current have set.

The Build, Configurations Command

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.


FIG. 5.3

Add or remove configuration targets to your project with the Configurations dialog box.

The Build, Subprojects Command

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.)


FIG. 5.4

You can insert subprojects into your current project.

The Build, Set Default Configuration Command

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.


FIG. 5.5

Select the target you want to build.

 
Be sure to set the Debug target while working through this book's projects. You'll then be able to use the Debugger, once you master chapter 8, to locate any errors.

Review the Project Toolbar

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.


FIG. 5.6

The Project toolbar lets you see your project run as soon as possible.

 
Chapter 8 explains how to use the last three debug-related Project toolbar buttons.

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.


FIG. 5.7

The Project toolbar lets you see your project run as soon as possible.

Building Your First Application

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:

  1. Compile your Java code into bytecode.
  2. Start your default Java-enabled Web browser software.
  3. Load the HTML page in the Web browser.
  4. Execute the applet.

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:


FIG. 5.8

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.

 
If you want to use a non-default Web browser, start the browser, cancel the dialing of the Internet if your Web browser attempts to dial, and choose the File, Open File command to select your project's HTML file (called First.html in the program you created in chapter 4). Not all Web browsers use the exact File, Open File menu command (Netscape Navigator does), so you may have to read your browser's manual to learn how to open an HTML file on your local disk drive.

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.


FIG. 5.9

Viewing the First application's Web page and Java applet.


FIG. 5.10

Click the hotspot to view the Java program's source code from within the Web browser.

 
If you use Netscape Navigator and have a problem seeing the twirling globe inside the applet's window, you may have to perform a workaround. Netscape Navigator prefers that an applet's .Class file (called First.class in the generated Visual J++ code) be located in a directory named Classes and that directory must reside directly beneath the HTML file's directory. Therefore, if Netscape Navigator cannot find the applet's class file (the applet's window will be blank with no spinning globe), create the Classes directory and copy the First.class file into the directory. Reload the HTML file and you should see the spinning globe after a brief image-loading time.

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.


FIG. 5.11

You can use Internet Explorer to display the First Web page and run the First applet.

Using the JView Applet Viewer

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:

  1. Open the Start menu and choose Run.
  2. Type JVIEW followed by your compiled applet file name. JView executes your applet.

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.

If Your Visual J++ Program Contains Errors

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).


FIG. 5.12

Errors appeared during the program's compilation.

 
Due to the nature of syntax errors, the error locations listed in the Output window do not always appear exactly where the errors occurred but the locations indicate the first character that Visual J++ realized it had just determined the error.

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.


FIG. 5.13

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.

 
With only two errors, the F4 and Shift+F4 error-jumping keys do not mean much. A typical Visual J++ application, however, may have several errors and you may not always be able to fix each one of them easily. The F4 and Shift+F4 keys let you move through the file to fix the ones that are easy first. you then can spend time on the rest of the errors. F4 and Shift+F4 give you a much easier vehicle for moving between errors than double-clicking over numerous scrolling error messages in the lower Output window.

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.

 
Visual J++ is completely project-oriented. Therefore, if your project contains two or more source files, and each of those files contain syntax errors, the Output window will show every source code's error! When you jump through the errors, Visual J++ jumps from source file to source file, locating each error found.

Summary

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 Page TOC Next Page

| 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.