# How Do I: Reuse Existing Java Code?

When you plan to write regular Java classes that will be used in multiple projects, you can package Java class files in a Java project. A Java project is a special kind of project that produces a JAR file which can be used across projects.

Note. If you are planning to reuse Java controls, you can create a control project. For more information, see How Do I: Create and Use a Java Control Within a Control Project?

To Create a Java Class Within a Java project

1. In the Application tab, right-click the top-level folder representing your application, then click New-->Project.
2. In the New Project dialog, in the left-hand pane, click Business Logic.
3. In the right-hand pane, click Java Project.
4. In the Project name box, type a name for your new control project.
5. Click Create. The Java project folder is created in the Application pane.
6. In the Application tab, right-click the Java project folder, then click New-->Folder. Enter a folder name in the Create New Folder dialog.

   It may be good idea to create separate subfolders within your Java project and group classes with related functionality in the same folder. Keep in mind that a folder beneath the Java project's top level becomes part of the control's Java package name. With this in mind, you should create a folder structure that reflects what the various Java classes are designed to do. Note that you cannot create the control in the root of a project — it must be in a subfolder.

7. In the Application tab, right-click the subfolder, then click New-->Java Class. The New File dialog appears.
8. In the File name field, type a name for the Java class you are creating.
9. Click Create. The file opens and you can add your Java class.

To Build and Package the Java Project

- Right-click the java project's folder, then click Build "java project folder".

WebLogic Workshop compiles the Java classes, packages it into a JAR file with the same name as the control project's folder, and adds the control as a library available in the application. You'll find the resulting JAR file in the Libraries folder of your application.

To Add a JAR File from Another Application

If you want to use a Java class that is in a JAR file in a different application, you will first need to add the library to your current application:

1. Go to the Application pane, right-click the Libraries folder in the root of the application folder, and select Add Library.
2. In the Add Library dialog, navigate to and double-click the root folder of the other application. Then double-click APP-INF-->lib, and select the JAR file. Click Open.

To Use the Java Class

To use the class, you must handle it like any regular Java class in a package. That is, you can add an import statement at the beginning of the source file or you can refer to the class by its long name. For more information, see Introduction To Java.

Related Topics

Introduction to Java