Development Example 1: Creating the Classic Hello World Java Class in Oracle JDeveloper 10g

Development Example 1: Creating the Classic Hello World Java Class in Oracle JDeveloper 10 g

In this section, we re going to put into practice the information you learned about the physical structure of the Oracle JDeveloper 10 g interface to create the classic Hello World Java class in Oracle JDeveloper 10 g. Throughout all of the examples in this chapter, we will be stressing the productivity benefits of the tool and, therefore, will be utilizing several of the code generating features of Oracle JDeveloper 10 g.

Creating the Application Workspace

Recalling the unit of work structure in Oracle JDeveloper 10 g from the preceding chapter, the first thing we will have to do is create an application workspace. As you may recall, there are several ways to do this. For this example, right-click the Applications node of the drill-down tree in the Applications Navigator and select New Application Workspace. The new application workspace dialog is displayed. Here, we enter our application workspace information. For this example, I have used the following settings, as shown in Figure 13-8:

click to expand
Figure 13-8: Application settings for our first Oracle JDeveloper 10g project
  • The application name is changed from the default to JdevTutorial.

  • The directory name should be automatically updated for you to correspond to your application name; if you wish, you can change it to anything you would like as long as Oracle JDeveloper 10 g has visibility to the directory in question and there is enough space to hold the files needed.

  • The application package prefix field allows you to specify a default package for all files that will be created in this application workspace. As you will see later when creating Java classes, it will auto-populate the package name with the value we have entered here. For this example, I used the package prefix of com.tusc.tutorial. For our purposes, the default web application template will be sufficient, so it was left unchanged. Click OK to generate the application workspace.

Creating the Project

As mentioned earlier, the next level down in the application hierarchy in Oracle JDeveloper 10 g is the project. This is used to organize pieces of code into units of work. Using the default web template, Oracle JDeveloper 10 g created two projects for us, one named Model and the other called ViewController. For this example, we will create a third project called Example1. Our second example will be more in-depth and will take advantage of the default packages Oracle JDeveloper 10 g created for us.

To create a new project, right-click the JdevTutorial application workspace and choose New Project. This brings up the New Project Wizard. Since we intend only to demonstrate the basic functions of Oracle Jdeveloper 10 g in this example, let s keep things simple and leave the Empty Project option selected and click OK. In the next dialog, enter the project name; in this case, let s call it Example1. The directory path will be automatically updated with the name of your project. Be cautious when changing the directory of your projects, as you can easily end up with an application structure that will cause problems when you try to deploy it, especially if you misplace folders, causing the application to become fragmented .

The last thing we need to do is to inspect the project properties. To do this, right-click the Example1 project and click Project Properties. Notice in the Input Paths tab under the common drill-down that our default package is listed as mypackage1. At first glance, this might appear to be a bug ”when we configured our application workspace, it was supposed to configure our projects to use the package prefix we specified, so why is mypackage1 displayed? If you open up one of the projects that Oracle JDeveloper 10 g created for us when we created our application workspace (Model or ViewController) and check the same properties we just checked for our Example1 project, you will see that the com.tusc.tutorial prefix is used with the project name appended on the end. The package prefix specified when we created the application workspace is automatically applied only to the projects it creates as part of the application template. Since it is configured in the projects, all classes created in those projects will also inherit these prefixes.

To bring our new project in line with the existing ones from the Application Workspace Wizard, let s re-open the Project Properties dialog box for Example1 and update the default package to read com.tusc.tutorial.example1.

Note  

We do not capitalize the e in example1 even though it is in our project name, since it is considered bad form to use capitals in package names .

Creating the Class

If you are unfamiliar with Java development, some of this section may be hard to follow. Unfortunately, covering the basics of Java development is outside the scope of this book, so in this case, you will simply have to follow along as best you can.

To create the class, right-click the Example1 project and select New. This brings up the now-familiar gallery for selecting what component we wish to create. In this case, drill into the General options, highlighting Simple Files and finally Java Class in the right-hand side, and click OK. In the Create Java Class dialog, change the name to HelloWorld . The package should now be properly prepopulated, since we reconfigured the default package for the project in the last section. Leave it as extending Object, and the only thing we ll change in the Optional Attributes section is to request Oracle JDeveloper 10 g ˜s code generator to generate a main method for us. Finally, click OK and you will taken back to the workbench with the new HelloWorld class active in the Editor pane. This is as good a time as any to save your work by selecting SAVE ALL from the menu bar.

Let s add the code to generate our Hello World message. For this example, we will stick with the simplest solution and have the class display the text to standard out, utilizing a System.out.println call. However, even something as simple as this provides the opportunity to detail another of Oracle JDeveloper 10 g ˜s productivity-increasing features.

Code templates are, essentially , shorthand for creating code components in Oracle JDeveloper 10 g. To invoke a code template, type the key for the template, press CTRL-ENTER, and Oracle JDeveloper 10 g will expand the key into the code that is defined to be mapped to it. To view, edit, or create new code templates, you can go to Tools Preferences and then drill into the code editor selection, choosing code templates from within the drill-down. Here, you will see a listing of the existing shortcuts and the code they will generate. These shortcuts create code ranging from things as simple as the System.out.println that s about to be demonstrated all the way up to things like fully generating a JDBC connection object. To create new code templates, click Add in this dialog and follow the instructions for specifying the shortcut and template code to be applied when it is invoked. Editing is even easier: just select the template you want to edit and edit it directly in the dialog.

Back to our hello world class: make sure the Editor window with your HelloWorld class is active and position your cursor at the end of the first line in the main method, which should read something like this:

 HelloWorld helloWorld = new HelloWorld(); 

Hit ENTER to place the cursor on a new line, type sop , and then press CTRL-ENTER . Notice how Oracle JDeveloper 10 g replaced the sop text with System.out.println() and automatically placed the cursor inside the parentheses that delineate the location of the parameter list for the println method. All we have to do now is place a string here saying Hello World (be sure to include the quotes to indicate it is a String) and we re done!

Compiling / Running the Class

Compiling is a simple process. There are three different ways to directly compile our HelloWorld class:

  • The shortest is to use the shortcut CTRL-SHIFT-F9 .

  • If you are more visually driven or happen to have your hand on the mouse, you can do a quick right-click in the Editor pane and select Make from the pop-up menu.

  • Finally, you can take the longest route and left-click the Run menu item, selecting Make HelloWorld.java from the drop-down menu.

Assuming you typed in everything correctly, you should see something like

 Successful compilation: 0 errors, 0 warnings 

displayed in your message log. If you missed something or typed something incorrectly, you will get compilation exceptions. You can double-click the exception message in your message log to jump to the line of code causing the error and repair it. Once you are able to successfully compile, let s move on to running.

If you tried the right-click option for compilation and you were paying attention, you probably noticed the Run option directly below the Make option. You will locate the menu item option under the same Run menu item drop-down as holds the make command.

Any of these options will work. Once you ve run the class, you should notice a new Message Log window open with the project name as the name of the tab. Inside this window, you will see the Java command utilized to execute your class (good if you re ever trying to debug a stand-alone app), our text Hello World!, and the exit condition (see Figure 13-9).

click to expand
Figure 13-9: Displaying the output of the HelloWorld.java program

The Hello World Class in Oracle JDeveloper 10 g

You can also run the class from the project level. The keyboard shortcut for this is F 11. In a larger project, you will need to specify what should actually be run when you indicate that you wish to run a project. This is done by specifying it within the project properties for the project in question. The option to specify this can be found in the project properties dialog by drilling into Profiles Development Runner. In the Runner section, you can specify the default run target for the project, what VM you would like Oracle JDeveloper 10 g to use, Java options, program arguments, and a default run directory. If you were building something that was command-line driven, the program arguments item would be of particular interest. This is where you would actually be able to specify what arguments were passed into your main method for testing and execution.

If you ever find you need to pass additional arguments to the Java executable for any reason (altering max heap size , configuring a system parameter using “D, etc.), you would use the Java Options section. This is where you would enter exactly what you would normally place directly between the Java executable and the run target, if you were typing it explicitly on the command line.

Debugging the Class

Debugging in Oracle JDeveloper 10 g has enough features in and of itself that it could encompass an entire chapter. Few developers can write code perfectly on the first try, and as you add developers to a development project, the odds of needing to debug code grow exponentially as integration issues arise. Even syntactically correct code may not do what is expected, and even if your code compiles and does what it is expected, that does not mean that the code being developed by another team member is going to integrate with it successfully.

Having a rich debugging tool is a fundamental part of an efficient IDE. Having a developer who is skilled in its use is as essential as having a developer who knows the syntax of the language he or she is writing the application in. For a more in-depth look at the broad set of features included in the Oracle JDeveloper 10 g debugging tool, you can refer to the Oracle JDeveloper 10 g online help, or the Oracle JDeveloper 10 g Handbook by Oracle Press referenced earlier in this chapter.

In this section, you will get a brief introduction to the debugger, including how to load it and the different command options to navigate your way through a debugging session. The class we ve coded is extremely simple, so it doesn t provide very many debugging options. However, since the intent of this section is to illustrate how to get around in the debugger, it should suit our needs.

The first thing we re going to have to do is set a breakpoint. For this example, we will place a breakpoint on the line in the main method that is constructing a new instance of our class. It should look something like this:

 HelloWorld helloWorld = new HelloWorld(); 

To place the breakpoint, click the line number to the left of the code. This should place a red dot on the line number, indicating that there is a breakpoint on this line and that while running the debugger, Oracle JDeveloper 10 g will break on that line.

Next, start the debugger by one of these means:

  • Click the small red bug in the toolbar.

  • Right-click in the Editor pane and select Debug.

  • Hit SHIFT - F 9.

  • Select Debug from the menu bar and then Debug from the drop-down menu.

You ll notice when you do this that Oracle JDeveloper 10 g first compiles the class, then loads the debugger tool, and then loads your class into the debugger process. At this point, the debugger will stop at the breakpoint we designated in the editor; it tells you this by placing a check mark on the line it is currently at as well as by highlighting the line in light blue. You should also notice that a new window was brought up to the left of our Message Log window (the Message Log window will now be half its original size). This is the Data pane.

The primary use of the Data pane is to allow you to monitor the variables and values in memory for the code you are debugging. It has a couple of different tabs within it:

  • The Smart Data tab shows you the variables, constants, arguments, and their values that are close to the execution point in your code (where the red arrow is).

  • The Data tab will show all variables, constants, arguments, and their values for your code in general. If a particular element is out of scope, it will indicate this as well.

  • The Watches tab allows you to specify particular elements that you want to watch. This is commonly used when you have an element that is key to the code you are working with and potentially nested into a complex relationship. Simply place a watch on it and you can monitor its progress through the Watches tab instead of having to drill through the relationships in one of the other tabs as you debug.

A couple of other tabs are available in the Data pane but are outside of the scope of this chapter. Now let s look at the navigation options once the debugger is loaded.

We ll traverse the buttons from left to right, as shown in Figure 13-10. The button furthest to the left is the Debugger button. This is the little red bug that we were referring to earlier when we were going to start the debugger.

click to expand
Figure 13-10: Oracle JDeveloper 10g debugger navigation tool buttons

The next button that looks like a green arrow is the Resume button ( F 9), which instructs the debugger to resume execution of your code until it either terminates (for whatever reason) or reaches the next debug point.

Next is the Step Over button ( F 8). This button instructs the debugger to step over the line of code that is the current execution point. This will still invoke the logic on that line and any nested calls therein and will return control to you on the next line in the current source. Keep in mind when debugging complex logic that if you have a breakpoint at some other point in your code that ends up being invoked by the call at the current execution point, the debugger will stop at that breakpoint even though you requested it to step over in the current source.

Next is the Step Into button ( F 7). This button will instruct the debugger to actually step into the call stack for the code at the current execution point. For example, if you are at the execution point in class A and the code at that execution point is calling a method in class B, hitting the Step Into button will bring up the code for class B in the editor and move the execution point to the first line of the method being called in class B. Should you ask the debugger to step into code that Oracle JDeveloper 10 g does not have the source for but has a reference to, the class file for it will ask you if you wish to have Oracle JDeveloper 10 g try to generate the code for you.

Caution  

The results you will get from this vary, but in my experience you are better off to avoid trying to do this. In general, when you are debugging, you can be fairly certain that the code you are working with that is a black box is coded correctly and it is only a matter of getting your code to interoperate with it correctly.

The next button is the Step Out button ( SHIFT - F 7). This button once again relies on the call stack and instructs the debugger to try to step out of the current scope and back up the call stack one level. So in our example from the preceding button, if we are in class A and step into a method in class B, and then we realize that we re not really interested in what s happening in the method in class B, we can step out to return us to the next execution point in class A. This can also be a helpful remedy when we inadvertently step into something we really don t want to.

The next button is the Step To End Of Method button. This button will, as its name implies, step you to the end of the current method, placing the execution point at the line before the method would exit.

Tip  

A very common use of this button is to assist you in locating where an element is being mutated into an incorrect value. Place a watch on the element in question, step into methods along the call stack, and step to the end of the method as you go until you notice the value change to the incorrect value. You now know what method is causing the problem. You can then reload the debugger with a breakpoint at the beginning of the offending method and step through it line-by-line to determine exactly where the problem is occurring.

Next, we have the Pause button. This button actually pauses execution of the program wherever it is. To resume execution, use the Resume button. To halt execution, use the Stop button (which we will discuss next).

As was stated in the description of the last button, the Stop button ( CTRL-F 2) halts execution of the program and terminates the debugger.

The final button is the Garbage Collection button. As we all know, there is no reliable way to directly invoke Java garbage collection. This button makes an attempt to manually trigger garbage collection, assuming the virtual machine supports such an action.

Back to our example: We currently have our HelloWorld class loaded into the debugger with the current execution point at the line that instantiates the class. If you look at the Smart Data tab in the Data pane, you notice that our helloWorld variable is currently out of scope. This means the debugger is aware of it but it has yet to have memory allocated for it. Essentially, it is just saying the debugger does not have a handle to this variable yet; in other cases, it could indicate the debugger once had a handle to it but does no longer. Step into this line using the Step Into toolbar button or the shortcut F 7. Notice that it took us to the head of the constructor for this class, which is what was invoked by the new keyword that was at our previous execution point. We re not really interested in the constructor (particularly since it has no logic in it), so let s step out of the method using the Step Out toolbar button (or SHIFT-F 7). This takes us back to our last execution point. Let s go ahead and step over this line now using the Step Over button (or F 8). This takes us to the next line in our code, which is our call to System.out.println. Notice that the arrow marking our execution point and the highlight have now shifted to this line. If we once again look in the Smart Data tab of the Data pane, we notice that our helloWorld variable is no longer out of scope. If we had any variables declared within the HelloWorld class, we could now drill into that variable reference to look at them. For now, go ahead and step over this line as well ( F 8). You should now notice Hello World! show up in the output window for our Example1.jpr tab in the Message pane. That s all we were really interested in seeing, so go ahead and hit the Resume toolbar button (or F 9). Our Example1.jpr tab in the Message pane should now show that the debugger has disconnected and that we have exited the process with an exit code of 0. This also terminates the debugger and closes our Data pane.

Something you may have noticed while running the debugger: an additional tab added to the bottom of the Message pane called Breakpoints. If you opened this, you would have seen the default breakpoints as well as the one we set in our HelloWorld class. This is also accessible while not running the debugger. To get it to appear, you can right-click the head of the Message pane (which actually appears on the left-hand side of the pane) and click breakpoints on the pop-up window.



Oracle Application Server 10g Web Development
Oracle Application Server 10g Web Development (Oracle Press)
ISBN: 0072255110
EAN: 2147483647
Year: 2004
Pages: 192

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net