How to Compile a Java Stored Procedure That References a Class in Another Schema? (Doc ID 1421076.1)

Last updated on JULY 05, 2017

Applies to:

Oracle Server - Enterprise Edition - Version: 10.2.0.1 and later   [Release: 10.2 and later ]
Information in this document applies to any platform.

Goal

This Note will illustrate what is required to compile a Java Stored Procedure in one schema that references a Class in a different schema.

The following provides the steps to execute a simple sample.
This sample has two Classes (Class1 & Class2).
Class1 is a standalone class and Class2 makes reference to Class1.
Each class will exist in its own schema.

This first part creates the two schemas.

CREATE USER JAVA_USER1 IDENTIFIED BY TIGER;
GRANT DBA TO JAVA_USER1;

CREATE USER JAVA_USER2 IDENTIFIED BY TIGER;
GRANT DBA TO JAVA_USER2;


Connect as JAVA_USER1 to create Class1

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Class1" AS
public class Class1 {
private static int v_counter = 0;
   Class1 () {
      v_counter ++;
   }
   public int getV_Counter() {
      return v_counter;
   }
};
/


Execute the following SQL to confirm the class is valid.
COL OBJECT_NAME FORMAT A15
SELECT OBJECT_NAME, OBJECT_TYPE, STATUS FROM USER_OBJECTS;


OBJECT_NAME     OBJECT_TYPE         STATUS
--------------- ------------------- -------
Class1          JAVA SOURCE         VALID
Class1          JAVA CLASS          VALID


Connect as JAVA_USER2 to create Class2

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Class2" AS
public class Class2 {
  public static void main (String [] args){
    Class1 c_Class1 = new Class1();
      System.out.println(c_Class1.getV_Counter());
   }
};
/

Warning: Java created with compilation errors.

SQL> SHOW ERROR
Errors for JAVA SOURCE "Class2":

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0 Class2:3: cannot find symbol
0/0 symbol : class Class1
0/0 location: class Class2
0/0 Class1 c_Class1 = new Class1();
0/0 ^
0/0 2 errors
0/0 symbol : class Class1
0/0 location: class Class2
0/0 Class1 c_Class1 = new Class1();
0/0 ^
0/0 Class2:3: cannot find symbol

The above error is expected because Class2 is in a different schema than Class1 so Class2 is unable to see classes from any other schema.




Solution

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms