Running Java programs

When you're ready to test your program or applet, you can simply run it, or you can run it and debug it at the same time. When you run your program, JBuilder uses the Class Path to locate all classes your program uses. To understand how JBuilder locates files to run the program, see "How JBuilder constructs paths" and "Where are my files?"

There are several ways to run your files. You can run an individual file, such as an applet HTML file or an application file, by right-clicking the file in the project pane and choosing the Run command. You can also run a project by selecting the Run Project button on the main toolbar. The Run Project button can also be configured with preset runtime parameters and saved as drop-down menu selections in JBuilder Professional and Enterprise.

Running .java files

To run a .java file or applet HTML file within a project,

  1. Save the file.

  2. Select the .java file in the project pane. The application .java file must contain a main method.

  3. Right-click the file and select Run  .

Note: You can also select Run|Run Project which runs the main class set on the Run page of the Project Properties dialog box.

The application compiles and runs if there are no errors. The build progress is displayed in the status bar and the message pane displays any compiler errors. Before compiling, error messages are displayed in the Errors folder of the structure pane. If the program compiles successfully, the classpath is displayed in the message pane and the program runs.

Running applets

To run an applet within a project,

  1. Save the file.

  2. Select the applet HTML or applet .java file in the project pane.
  3. Right-click the file and select Run  . You can also select Run|Run Project which runs the main class or HTML file set on the Run page of the Project Properties dialog box.

The applet compiles and runs if there are no errors. The build progress is displayed in the status bar and the message pane displays any compiler errors. Before compiling, error messages are displayed in the Errors folder of the structure pane. If the program compiles successfully, the classpath is displayed in the message pane and the program runs.

Important: For more information on running JDK 1.1.x and 1.2 applets in JBuilder, see the "Runtime" topic in the "Release Notes" (Help|Release Notes).

JBuilder's AppletTestbed

When you right-click the applet HTML file and select Run, the applet runs in Sun's appletviewer. If you select Run|Run Project, the applet runs by default in JBuilder's applet viewer, AppletTestbed. You can change the default to Sun's appletviewer by choosing the HTML File option and selecting the applet HTML file to run on the Run page of the Project Properties (Project|Project Properties|Run).

Running web files

This is a feature of JBuilder Professional and Enterprise.
JBuilder also supports running web files, such as JSPs, servlets, SHTML, and HTML, through a web server for a live view of your web application. To run JSP, SHTML, and HTML files, right-click the file in the project pane and select Web Run.

For servlets, you must first set the Enable Web Run/Debug From Context Menu option. If you create your servlets with JBuilder's Servlet wizard, this option is set automatically. To enable this option, right-click the servlet file in the project pane and select Properties. Check the Enable Web Run/Debug From Context Menu option on the Web Run page. Then right-click the servlet file and select Web Run.

See also:
"Compiling and debugging the JSP"
"Compiling the servlet."

Running projects

You can run your project by selecting Run|Run Project or the Run Project button Run button on the main toolbar. This runs the main class selected on the Run page of the Project Properties dialog box. If a main class has not yet been selected, the dialog box appears for you to make the selection. If you created your file with the Application wizard, the main class is automatically selected.

With JBuilder Professional and Enterprise, you can also set runtime and debug configurations for the Run Project and Debug Project buttons. These configurations are added to the drop-down menus that are accessible from the Down arrows to the right of each buttons.

See also: "Setting runtime configurations."

To run your project without debugging,

  1. Save your files.

  2. Set the main class on the Run page of the Project Properties dialog box if it is not already set.
    1. Right-click the project file in the project pane and select Properties.
    2. Select the Run page.
    3. Select the appropriate tab for setting the main class: Application, Applet, JSP/Servlet, or EJB. The main class must be in the current project.

      • Application: Select the Application tab and click the ellipsis button. Browse to the class file containing the main() method.
      • Applet: Select the Applet tab and do one of the following:
        • Select Main Class and click the ellipsis button to browse to the class containing the init() method. This option runs the applet in JBuilder's applet viewer, AppletTestbed.
        • Select HTML File and click the ellipsis button to browse to the applet HTML file containing the APPLET tag. The HTML option runs the applet in Sun's appletviewer.
      • JSP/Servlet: Select the JSP/Servlet tab and click the Help button in the dialog box. See "Compiling and debugging the JSP" and "Compiling the servlet."
      • EJB: Select the EJB tab and click the Help button in the dialog box.

      Run page of Project Properties

  3. Click OK in all dialog boxes to close them.
  4. Choose Run|Run Project, press F9, or click the Run button   on the toolbar.

    Tip: Alternatively, you can first choose Project|Make Project to compile the project, then click the Run button on the toolbar.

JBuilder compiles and runs your program. Any errors during compile are displayed in the message pane at the bottom of the AppBrowser. If there are errors, compiling stops so you can fix the errors and try again. Select an error in the message pane or in the Errors folder in the structure pane to highlight the code in the source pane. For help on an error message, select the error in the message pane and press F1 Help.

See also: Compiling Java programs

Error messages in the AppBrowser

Compiler errors

Setting runtime configurations

This is a feature of JBuilder Professional and Enterprise.
Runtime configurations (Run|Configurations) are preset runtime parameters. Using preset parameters saves you time when running and debugging, because you need to set the parameters only once. With preset configurations, each time you run or debug your application you simply select the desired configuration.

To set the runtime configurations, use the Runtime Configurations dialog box.

  1. Select Run|Configurations.

    Runtime Configuration

  2. Click New to open the Run page of the Runtime Properties dialog box.

    Run page of Project Properties

  3. Name the configuration which will be added to the drop-down list next to the Run Project and Debug Project buttons.

  4. Select the appropriate tab, such as Application, Applet, JSP/Servlet, or EJB, and set the runtime configurations.

    You can set the following run and debug options:

  5. Click OK in all dialog boxes to close them.

Runtime configurations are displayed in the Runtime Configurations dialog box. You cannot delete the <Default> configuration, although you can edit it. To choose a configuration once you've created it, select the Down arrow next to the Run and Debug icons on the main toolbar to access the drop-down menu of configurations.

Runtime Configurations menu

If you get errors, runtime exceptions, or other program misbehavior, you may want to debug your program as you run it to find the problems. You do that by running the program in JBuilder's integrated debugger.

See also: Debugging Java programs

Using the command line to run and debug

Running from the command line

Running your program outside JBuilder requires that you put all the libraries required by your program on your CLASSPATH or add them to the -classpath argument to the java command. For example,

java -classpath  /user/jdk/lib/rt.jar /user/jbuilder/lib/dbswing3.0.jar
/user/local/jbproject/mypackage/project1 mypackage.project1

Running a deployed program from the command line

After deploying the program using the Archive Builder or the jar tool, you can run the .jar file from the command line.

  1. Open the command-line window.

    Tip: For Windows, use backslashes (\) in all commands discussed here.

  2. Enter the following command on one line at the prompt from any location:

    java -classpath /user/[username]/jbproject/hello/classes/HelloWorld.jar
    hello.HelloWorldClass

    Note: The jdk/bin directory must be on your path.

    The command must have the following form:

    java -classpath package-name.main-class-name

    You can also use the -jar option:

    java -jar HelloWorld.jar

    Note: You must first change to the directory that contains the .jar file before running this command with the -jar option.

See also:
Compiling Java programs
Deploying Java programs
The JAR tutorial at http://java.sun.com/docs/books/tutorial/jar/index.html
Using the command-line tools

Debugging from the command line

To debug outside JBuilder, use the jdb tool in the JDK/bin directory. See the JDK documentation at http://java.sun.com/j2se/1.3/docs/tooldocs/tools.html for more information on this tool.

See also:
Using the command-line tools
Debugging Java programs
Deploying Java programs