Running Java Code
To run Java code, you must be in one of the Java perspectives. There are three basic ways to run code in your Java projects: You can run (launch) a Java program with the Run action, you can run a Java program with the Debug action, and you can evaluate (execute) an Java expression in a scrapbook page. With the Run action, your program executes and you do not have an opportunity to suspend its execution or examine variable or field values. In debug mode, you suspend, resume, and examine a program's execution. We'll look more at debugging in Chapter 4.
You can run code even if it still has compiler errors. If the scope of an error is limited to a method, you can run code in the class, except for the method with the error. If the scope of an error is at the class level, for example, a problem with a static declaration, you can run code in other classes but not the one with the error.
Using the Run Action
Java programs, that is, classes with main methods, are identified with the Run label decoration. To run a Java program, select a class or a Java element containing a class, and select Run from the menu or the Run pull-down menu, and then select Run As > Java Application. JDT executes the main method and sends output to the Console view. If you have previously run Java programs, you will have entries under Run > Run History and Run from the toolbar. Select from these to re-run programs. You can also press Ctrl+F11 or simply select Run from the toolbar to re-run the last program you ran.
When you run a Java program, you run it as a Java application, JUnit test, or run-time workbench. We're going to focus on Java applications here. Running as a run-time workbench is for testing extensions to Eclipse. We'll get to that in Chapter 8. Running as a JUnit test is beyond the scope of this book. For more information on this, refer to "Using JUnit" in the "Tasks" section of the Java Development User Guide.
To run a Java program, JDT needs two things: a main method and a launch configuration. Depending on what view was active and what you had selected when you requested to run a program, if a unique class with a main method can be determined, that main method will be run. For example, if the Java editor is active on an element with a main method, that main method will be run. If you have a project selected in the Package Explorer view and there are multiple classes in that package with main methods, you will be prompted to select one.
If the program encounters a run-time error, the exception information goes to the Console view and is displayed in error text color to distinguish it from other output types. Color-coding for output text in the Console view is defined in your Console preferences under Debug.
Managing Launch Configurations
|
Launch configurations define information for running Java programs. With launch configurations you can specify input parameters, JVM arguments, and source for your code, and set the run-time classpath and JRE. If you do not specify a launch configuration when you run a Java program with the Run action, a default is created for you. To define a launch configuration, select the Run pull-down menu and then Run.... In the Launch Configurations dialog (see Figure 3.29), select Java Application for Launch Configurations and then select New. If you had previously run Java programs with the Run action, you will see the default launch configurations that were created for you (see Figure 3.29).
Figure 3.29 Creating a Launch Configuration
On the Main page, Project is optional. If you specify it, its build path is used to set the classpath, source lookup, and JRE. Use Search... to search a project's build path for Java programs. If a valid project is specified, the build path of that project is searched for classes with main methods matching the search pattern (you can use wildcards) in Main class. If a project is not specified, the build paths of all the projects in your workspace are searched for classes with main methods matching the search pattern.
On the Arguments page, you can set the input parameters for the Java program and any JVM parameters. The Java program parameters are strings separated by spaces. For more information on JVM arguments, see "Running Eclipse" in the "Tasks" section of the Workbench User Guide.
On the JRE page, you set the JRE used to execute the Java program. Use this to override the JRE defined in the build path properties of the project containing the program. Choose from the list of choices, which is populated with the JREs you have defined in your Installed JREs preferences, or add a new JRE. This is useful, for example, to specify a 1.4 JRE in order to do hot code replace when you debug your code.
On the Classpath page, the classpath is set based on the build path information from the project specified on the Main page. To override or add to this, deselect Use default classpath. If you do not have a project specified, you need to do this and specify classpath information. If you choose not to use the default classpath, add references in the classpath to those projects and JAR files containing declarations the Java program references, including the project or JAR file containing the main method being executed.
|
On the Common page you specify information about how to save the launch configuration. By default, launch configurations are saved as metadata in your workspace. If you select Shared, you specify a Java project into which to save the launch configuration as a .launch file. In this way, it's easy to keep and manage launch configurations with the code they run and to share them with others accessing the project; this is especially important for teams. The Run mode and Debug mode selections allow you to specify the perspective to display when the launch configuration is used to run or de- bug, respectively, the Java program. You can also indicate which favorites list displays the launch configuration, the Run or the Debug toolbar pull-down menus.
Evaluating Expressions in Scrapbook Pages
|
Java scrapbook pages (*.jpage files) allow you to edit and evaluate Java code expressions and display or inspect the results. They are a quick and easy way to test code and experiment with Java code expressions. You can evaluate an expression that's a partial statement, a full statement, or a series of statements. To create a scrapbook page, select Create a Scrapbook Page or use the New wizard. Scrapbook pages can be located in Java projects, folders, source folders, and packages.
|
Enter an expression in the Scrapbook page. This could be something simple like System.out.println("Hello World"), or an invocation of your Java code, for example, its main method. Content assist (Ctrl+Space) is available in scrapbook pages. Select the expression and then select Display from the toolbar or the context menu. JDT evaluates the expression, sends output to the Console view, and displays the toString value returned by the expression you selected in the scrapbook page. Select Inspect to display the results in the Expressions view. (We'll discuss the Expressions view in more detail in Chapter 4.) Run Snippet simply runs the code and sends the output to the Console view.
There are a couple of scrapbook page properties worth noting. To view or change these, select a scrapbook page and then Properties from the context menu. Working directory by default is the Java project in your workspace containing the scrapbook page. This is for relative file references. You can also change the JRE used for the scrapbook page. The default JRE is the one specified in your Java preferences under Installed JREs.
|
Scrapbook pages get their classpath from the containing project's build path. If in a scrapbook page you want to reference a Java element that is not on the build path of the containing Java project, you need to add to the Java project's build path. Scrapbook pages also allow you to specify import statements. You do this by selecting Set Imports from the context menu of a scrapbook page or Set Import Declarations for Running Code from the toolbar. You need to set import statements for references to Java declarations in your projects. This is a common oversight. If the type or package you are attempting to import is not listed in the Add dialog, it means you need to add it to the build path of the project containing the scrapbook page. If you are referencing an element that has multiple declarations, you will need to add an import statement to uniquely identify the element.
|
Each scrapbook page has its own associated JVM. The JVM is started the first time you evaluate an expression in a scrapbook page after it is created or opened. The JVM for a scrapbook page runs until the page is closed or explicitly stopped with Stop Evaluation from the context menu or the toolbar. When a scrapbook page is in the process of evaluating, the icon changes to a red J on a gray background. If the expression you're evaluating results in a loop or a hang, select Stop Evaluation. In some cases this may not stop execution. If it doesn't stop, simply close the scrapbook page and then re-open it.