Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

Error While Execute Java Code From Database

Sameh M. SewilamMar 28 2022 — edited Mar 28 2022

Hi All

I have a 19c Oracle Database with the following specifications

SELECT * from v$version;
Oracle Database 19c Standard Edition 2 Release 19.0.0.0.0 - Production

SELECT dbms_java.get_jdk_version JDK_Version FROM dual;
1.8.0_201

OS : Windows Server 2016
Installed Java jdk1.7.0_80 and jdk-9.0.1

I have Created A java Code with eclipse to print reports and merge the printed reports to one PDF.
I tested it and it works fine. Then i loaded it to Database with all dependence using loadjava
The Class created successfully without any compilation error and its status is VALID

create or replace and compile java source named MergePDFJava As

import oracle.aurora.rdbms.ClassHandle;
import oracle.aurora.rdbms.Schema;
import oracle.aurora.rdbms.DbmsJava;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;

public class MergePDFJava {

public static String combineReports (String file_name ,String report_name,String visit_id,String pid) throws ClassNotFoundException, SQLException, JRException, IOException {
String dir = "E:/printedresults/";

  List\<JasperPrint> jasperPrintList = new ArrayList\<>();  

  Connection conn = getOracleConnection();  
   String\[\] report\_name\_array = report\_name.split(",", 100);  
   String\[\] visitid\_array = visit\_id.split(",", 100);  
   String\[\] pid\_array = pid.split(",", 100);  
       for (int i = 0; i \< report\_name\_array.length; i++) {  
               String Report = dir+report\_name\_array\[i\];  
               Map\<String, Object> parameters = new HashMap\<>();  
               parameters.put("PVisitID", visitid\_array\[i\]);  
               parameters.put("PID", pid\_array\[i\]);  
               JasperReport jr = JasperCompileManager.compileReport(Report);  
               JasperPrint jp1 = JasperFillManager.fillReport(jr, parameters,conn);  
               jasperPrintList.add(jp1);  

}  


  JRPdfExporter exporter = new JRPdfExporter();  
  exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));  
  exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(dir +file\_name +".pdf"));  
  SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();  
  exporter.setConfiguration(configuration);  
  exporter.exportReport();  
  return "Printing...";  

}
public static Connection getOracleConnection()
throws ClassNotFoundException, SQLException {
String hostName = "server";
String sid = "orclpdb";
String userName = "username";
String password = "password";

   return getOracleConnection(hostName, sid, userName, password);  

}

public static Connection getOracleConnection(String hostName, String sid,
String userName, String password) throws ClassNotFoundException,
SQLException {

   Class.forName("oracle.jdbc.driver.OracleDriver");  


   String connectionURL = "jdbc:oracle:thin:@" + hostName + ":1521/" + sid;  

   Connection conn = DriverManager.getConnection(connectionURL, userName,  
           password);  
   return conn;  

}
}

Then I created a Function as
create or replace FUNCTION portal_print_result (
file_name VARCHAR2,
report_name VARCHAR2,
visit_id VARCHAR2,
pid VARCHAR2
) RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'MergePDFJava.combineReports( java.lang.String, java.lang.String, java.lang.String, java.lang.String ) return java.lang.String ';
and it's created successfully without errors

and while execution i got the below error
DECLARE
x CLOB;
BEGIN
x := portal_print_result(
'45896',
'Dental_Lab_Order.jasper,Dental_Lab_Order.jasper',
'8615,8572',
'48,21'
);
END;

ORA-29532: Java call terminated by uncaught Java exception: java.lang.NoClassDefFoundError
ORA-06512: at "PORTAL_PRINT_RESULT", line 1
ORA-06512: at line 4
29532. 00000 - "Java call terminated by uncaught Java exception: %s"
*Cause: A Java exception or error was signaled and could not be
resolved by the Java code.
*Action: Modify Java code, if this behavior is not intended.

Consider that user has all needed privilege
BEGIN
dbms_java.grant_permission('USER','SYS:java.io.FilePermission','<<ALL FILES>>','read,write,execute,delete');
dbms_java.grant_permission('USER','SYS:java.lang.RuntimePermission','readFileDescriptor','');
dbms_java.grant_permission('USER','SYS:java.lang.RuntimePermission','writeFileDescriptor','');
dbms_java.grant_permission('USER','java.util.PropertyPermission','*','read');
dbms_java.grant_permission( 'USER', 'SYS:java.lang.RuntimePermission', 'getClassLoader', ' ' );
dbms_java.grant_permission( 'USER', 'SYS:oracle.aurora.security.JServerPermission', 'Verifier', ' ' );
dbms_java.grant_permission( 'USER', 'SYS:java.lang.RuntimePermission', 'accessClassInPackage.sun.util.calendar', ' ' ) ;
dbms_java.grant_permission( 'USER', 'java.net.SocketPermission', '*', 'connect,resolve' );
dbms_java.grant_permission( 'USER', 'SYS:java.lang.RuntimePermission', 'createClassLoader', ' ');
COMMIT;
END;
/

Can anyone support in the mentioned errors
Thanks in advance

Comments
Post Details
Added on Mar 28 2022
4 comments
398 views