Skip to Main Content

Java and JavaScript in the Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

Solomon Yakobson

It couldn't unless dropped in both schemas directly or indirectly (e.g. there is after DDL trigger). Also, second schema could have not the view but rather a synonym to first schema view, although in such case you'd get "synonym translation no longer valid" rather than "table or view doesn't exist". You could check DBA_AUDIT_TRAIL to find out who and when dropped what, assuming DDL audit it enabled.
SY.

Kamran Agayev A.

Dear OP
If audit wasn't enabled in the database, it's hard to answer the question "who dropped the view". If the data is still in UNDO, you can use flashbcak query to get the dropped views back. Check the following blog post for more information on this:
How to flashback procedure or package - OracleDocs.com (0 Bytes)BR,
Kamran Aghayev A.

Solomon Yakobson

There is no need to dig into flashback unless OP has no saved view definition script (which would be strange and would indicate much deeper organizational issues).
SY.

Kamran Agayev A.

OP mentioned that views in other schemas were dropped which means that most probably they didn't saved the script of the views which dropped by accident

user545194

enable_ddl_logging is not enabled. We are on SE, therefore do not have flashback query. I can check the alert log to see what happened. Aren't dropped objects moved to the recyclebin?

Solomon Yakobson

If so, then, as I already mentioned, it would indicate much deeper organizational issues. Any changes must be scripted and saved to CMS before deploying.
SY.

Kamran Agayev A.

Views are just scripts which are not moved to the recycle bin. Do you have a test environment for this particular database to imitate the same action there as well and see if views are dropped (which shouldn't )

user545194

Yes, we have a test environment and the views are there, but they are not in sync with the production env. But I can perform the same action and see what the outcome is. For production however, we'll have to restore from a recent backup.
Good news: Suddenly the views "re-appeared" and are visible in the SQL Developer tree view.
Really strange. Thanks to all for your feedback.

Kamran Agayev A.

Sure you have to restore it from backup anyways. What I'm wondering the abnormal behavior that happened in your system. I would suggest you to enable the audit on the test environment, drop the mentioned views and check if other views are also dropped
BR,
Kamran Aghayev A.

user545194
Answer

Yes, I will definitely run this in the test environment. Thanks again!

Marked as Answer by user545194 · Jan 25 2021
1 - 10

Post Details

Added on Mar 28 2022
4 comments
729 views