oracledba.help
QuickStarts

OJVM

<- QuickStarts

Overview

The main purpose of this page is to ensure your Oracle OJVM is working and ready to execute custom internal Java. The Oracle user\schema x15 is used in these examples. Change to match your environment.

TOC

Prerequisites

  • Your database has the OJVM installed.

OJVM Status Checks

OJVM Status

Good health check post OJVM PSU.

 COL DBMS_JAVA.LONGNAME('OK') FORMAT a9
 SELECT dbms_java.longname('OK') FROM dual;
   -- OK Example Output
   DBMS_JAVA
   ---------
   OK
   -- Not OK Example Output
   ORA-29548: Java system class reported: release of Java system classes in the database 
   (12.1.0.2.181016 1.6) does not match that of the oracle executable (12.1.0.2.180717 1.6)

For RAC run on each node.

Component Status

 COL comp_name FORMAT a35
 COL version   FORMAT a15
 SELECT comp_name, version, status from dba_registry 
 WHERE upper(comp_name) LIKE '%JAVA%%';
   COMP_NAME                           VERSION         STATUS
   ----------------------------------- --------------- -----------
   JServer JAVA Virtual Machine        12.1.0.2.0      VALID
   Oracle Database Java Packages       12.1.0.2.0      VALID

Registry Banners

 SELECT * FROM all_registry_banners;
   JServer JAVA Virtual Machine Release 12.1.0.2.0 - Production
   Oracle Database Packages and Types Release 12.1.0.2.0 - Production

OJVMSYS Total Objects

 COL owner  FORMAT a15
 COL status FORMAT a15
 SELECT owner, status, count(*) FROM all_objects 
 WHERE object_type LIKE '%JAVA%' GROUP BY owner, status;
   OWNER                STATUS            COUNT(*)
   -------------------- --------------- ----------
   OJVMSYS              VALID                    4

Java v$option

 COL parameter FORMAT a10
 COL value     FORMAT a10
 SELECT * from v$option WHERE parameter = 'Java';
   PARAMETER  VALUE          CON_ID
   ---------- ---------- ----------
   Java       TRUE                0

Java Roles

 SELECT role FROM dba_roles WHERE role LIKE '%JAVA%';
   ROLE
   -------------
   JAVAUSERPRIV
   JAVAIDPRIV
   JAVASYSPRIV
   JAVADEBUGPRIV
   JAVA_ADMIN
   JAVA_DEPLOY
   6 rows selected.

Usage

 EXEC DBMS_FEATURE_USAGE_INTERNAL.exec_db_usage_sampling(SYSDATE);
 COL currently_used FORMAT a15
 SELECT currently_used, name FROM dba_feature_usage_statistics 
 WHERE name LIKE '%Java%';
   CURRENTLY_USED  NAME
   --------------- ------------------------------------
   TRUE            Oracle Java Virtual Machine (user)
   TRUE            Oracle Java Virtual Machine (system)
  • DBMS_FEATURE_USAGE_INTERNAL...(SYSDATE) updates usage as of now as opposed to default (weekly).
  • FALSE is not necessarily bad. It just may not have been used during sampling.

Create and Test Java Program

Set CLASSPATH

 export CLASSPATH=/home/oracle

Set this to the path where your java source files will be.

Create Java Source

  1. cd $CLASSPATH
  2. vi Hello.java
 public class Hello
 {
   public static String world()
   {
     return "Hello world";
   }
 }

Compile Your Source

 $ORACLE_HOME/jdk/bin/javac Hello.java

Creates Hello.class

Load Java Program Into Database

 OS> loadjava -user x15/x15 Hello.class

Create Function to Call Java Class

 sqlplus x15/x15@oradb

 CREATE OR REPLACE FUNCTION helloworld RETURN VARCHAR2 AS
 LANGUAGE JAVA NAME 'Hello.world () return java.lang.String';
 /
 Function created.

Test Java

 sqlplus x15/x15@oradb

 SQLPLUS> VARIABLE myString VARCHAR2(20);
 SQLPLUS> CALL helloworld() INTO :myString;
    Call completed.
 SQLPLUS> PRINT myString;

 MYSTRING
 --------------------------------
 Hello world

DROP Java Schema Object

DROP JAVA {CLASS | RESOURCE | SOURCE} [schema.]object_name
 DROP JAVA CLASS x15.Hello;
 DROP JAVA SOURCE X15.Hello;

 DROP FUNCTION x15.helloworld;

Make sure to use the exact case of object. You may need to use double-quotes around schema and object names ("SCHEMA"."MyObjName").

APPENDIX

Unset CLASSPATH

Long term it is better to use a dedicated OS app account rather than the oracle user. So remark out the CLASSPATH if you added it. This way it will not interfere with future patching and other operations.

 vi .bashrc
 #export CLASSPATH=/home/oracle