JavaData & JavaCreating a Java Application in Oracle JDeveloper, Part 2

Creating a Java Application in Oracle JDeveloper, Part 2

Oracle JDeveloper is a Java IDE with support for other frameworks, including the proprietary Application Development Framework (ADF). In two articles, we are discussing creating a Java application in JDeveloper. In the first article, “Creating a Java Application in Oracle JDeveloper, Part 1,” we created a Java project and added a Java interface. In this continuation article, we shall create a Java class that implements the interface. This article has the following sections:

Creating a Java Class

To create a Java class, right-click the Client project and select New>Java Class, as shown in Figure 1.

Client>New>Java Class
Figure 1: Client>New>Java Class

Alternatively, select File>New>Java Class, as shown in Figure 2.

File>New>Java Class
Figure 2: File>New>Java Class

In New Gallery, select General>Java in Categories, as shown in Figure 3. Select Class in Items. Click OK.

New Gallery>Java>Class
Figure 3: New Gallery>Java>Class

The Create Java Class dialog gets started, as shown in Figure 4. Specify a class Name (HelloWorldImpl) and the default Package (helloworld) should get added automatically. Keep other settings as the default and click OK.

Create Java Class
Figure 4: Create Java Class

A new Java class, HelloWorldImpl, gets added to the helloworld package. A Java source file for the class also gets created, as shown in Figure 5.

Java Class helloworld.HelloWorldImpl
Figure 5: Java Class helloworld.HelloWorldImpl

Setting Code Editor Preferences

We shall be adding code to the Java class to create a Java application that implements the interface helloworld.HelloWorld. But, first we need to discuss the various options available to set code Editor Preferences. To launch the Preferences dialog, select Tools>Preferences or, alternatively, right-click in the Editor and select Preferences. The Preferences dialog gets launched (see Figure 6). Select Code Editor. The various Code Editor options, including Auto-Indent New Lines and Auto-Surround Selected Text, get displayed. We have kept the default settings.

Preferences>Code Editor
Figure 6: Preferences>Code Editor

The Block Coloring used to distinguish between various types of code blocks is shown in Figure 7.

Block Coloring
Figure 7: Block Coloring

JDeveloper Code Editor supports code assist to provide a drop-down of alternative code snippets that could complete a partial code snippet added. The Code Insight>Completion settings are shown in Figure 8.

Code Insight>Completion
Figure 8: Code Insight>Completion

The Code Style used is displayed in Figure 9.

Code Style
Figure 9: Code Style

The Code Templates used by different code components are displayed in Figure 10.

Code Templates
Figure 10: Code Templates

The Code Editor preferences are for all types of code including ADF. The Code Editor settings specific to Java are shown in Figure 11.

Code Editor Settings for Java
Figure 11: Code Editor Settings for Java

The Code Insight for Java includes Automatically Add Import When Completing a Fully-Qualified Class Name, Show Imported Classes, Show Local Variables and Method Parameters in Italics, and Include Calling Method Definitions, as shown in Figure 12.

Java>Code Insight
Figure 12: Java>Code Insight

The preferences for Java folded regions are shown in Figure 13.

Java>Folding Preferences
Figure 13: Java>Folding Preferences

The preferences for Java Imports include Sort Automatically When Organizing Imports and Enable Import Assistance, as shown in Figure 14.

Java>Imports
Figure 14: Java>Imports

The Imports>Auto preferences include Automatically Add Unambiguous Imports, as shown in Figure 15.

Imports>Auto Preferences
Figure 15: Imports>Auto Preferences

The Java Structure preferences are shown in Figure 16.

Java>Structure
Figure 16: Java>Structure

Creating a Java Application

In this section, we shall create a Java application in the Java class HelloWorldImpl. We need to make the following additions/modifications to create a Java application:

  1. Add a public static void main (String[] args) method. Without the main method, a Java class is not an application.
  2. Implement the HelloWorld interface.
  3. Implement the getMessage(String) method. A class that implements an interface must implement all its abstract methods or, alternatively, declare the class also abstract.
  4. Create an Instance of the class HelloWorldImpl in the main method and invoke the getMessage(String) method.

Add an implementation of the getMessage(String) method:

public String getMessage(String name) {
  return "Hello " + name;
}

If the code added has any syntax error, the error message is displayed in the Code Editor. As an example, if a return statement is not included in the implementation for the getMessage(String) method, an error message “Return statement missing” is displayed, as shown in Figure 17.

Error Message
Figure 17: Error Message

Adding a return statement fixes the error, as shown in Figure 18.

Error message fixed by adding a return Statement
Figure 18: Error message fixed by adding a return Statement

Add the main method to make the Java class a Java application. The main method is invoked when a Java application is run.

public static void main(String[] args) {
   }

The Java application HelloWorldImpl is shown in Figure 19.

Java Application HelloWorldImpl
Figure 19: Java Application HelloWorldImpl

The Java application is not complete yet. Running the Java application as it is would not generate any output because the getMessage(String) method has not been invoked in the main method. The getMessage(String) method cannot be invoked directly because the method is an instance method and cannot be invoked from a static context, the main method. To invoke the getMessage(String) method, create an instance of the HelloWorldImpl class:

HelloWorldImpl helloImpl = new HelloWorldImpl();

Subsequently, invoke the getMessage(String) method using the class instance. The code assist may be used to invoke the getMessage(String) method, as shown in Figure 20.

Using Code Assist to invoke getMessage(String) Method
Figure 20: Using Code Assist to invoke getMessage(String) Method

The main method with an instance of the class invoking the getMessage(String) method is as follows:

public static void main(String[] args) {
   HelloWorldImpl helloImpl = new HelloWorldImpl();
   System.out.println(helloImpl.getMessage(args[0] + " "
      + args[1]));
}

To format the code, right-click in the Code Editor and select Reformat (see Figure 21).

Code Editor>Reformat
Figure 21: Code Editor>Reformat

The Java class and interface must be saved after making any modifications. Select File>Save All, as shown in Figure 22, to save the Java source code files.

File>Save All
Figure 22: File>Save All

The HelloWorldImpl class is as follows:

package helloworld;
public class HelloWorldImpl implements HelloWorld {
   public HelloWorldImpl() {
      super();
   }

   public static void main(String[] args) {
      HelloWorldImpl helloImpl = new HelloWorldImpl();
      System.out.println(helloImpl.getMessage(args[0] + " " +
         args[1]));
   }

   public String getMessage(String name) {
      return "Hello " + name;
   }
}

Setting Program Arguments

In the main method, we make use of the String[] type args parameter to obtain the program arguments. The program arguments are supplied at runtime. We need to configure the Program Arguments in Project Properties. Right-click the Client project and select Project Properties, as shown in Figure 23.

Client>Project Properties
Figure 23: Client>Project Properties

In Project Properties, select Run/Debug and click the Edit button, as shown in Figure 24. The button is not labeled “Edit,” but is identified by the pencil icon.

Project Properties>Run/Debug>Edit
Figure 24: Project Properties>Run/Debug>Edit

The Edit Run Configuration “Default” dialog gets displayed, as shown in Figure 25. Specify a name (John Smith) in the Program Arguments field, and click OK.

Specifying Program Arguments
Figure 25: Specifying Program Arguments

Click OK in Run/Debug in Project Properties (see Figure 26).

Run/Debug>OK
Figure 26: Run/Debug>OK

Running the Java Application

Having configured the program arguments, next we shall run the Java application. Right-click HelloWorldImpl.java in Applications>Projects view and select Run, as shown in Figure 27.

HelloWorldImpl.java>Run
Figure 27: HelloWorldImpl.java>Run

The application runs, and the Hello John Smith message gets output, as shown in Figure 28.

Output from running HelloWorldImpl Application
Figure 28: Output from running HelloWorldImpl Application

If an error or exception is generated when the application is run, it is displayed as shown, in Figure 29.

Exception Message
Figure 29: Exception Message

Conclusion

JDeveloper provides several features designed to create and run Java applications. In two articles, we have discussed creating and running a Java application in Oracle JDeveloper.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories