Home » Developer & Programmer » JDeveloper, Java & XML » Calling Java function from pl/sql
icon9.gif  Calling Java function from pl/sql [message #658330] Fri, 09 December 2016 10:30 Go to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Below is the Java query which am using

<Code>
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class EncryptPdf
{
String owner = "PASS2";
String user = "PASS1";
public static final String DEST = "/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001_e.pdf";
public static final String SRC = "/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001.pdf";

public static void main(String[] args)
throws IOException, DocumentException
{
File file = new File("/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001.pdf");
file.getParentFile().mkdirs();
new EncryptPdf().manipulatePdf("/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001.pdf", "/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001_e.pdf");
}

public void manipulatePdf(String src, String dest)
throws IOException, DocumentException
{
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.setEncryption(this.user.getBytes(), this.owner.getBytes(),
2052, 10);
stamper.close();
reader.close();
}
}
</Code>



Oracle procedure which am using:

<Code>

create or replace
PROCEDURE PR_encryptpdf (src in varchar2,dest in varchar2)
AS LANGUAGE JAVA
NAME 'EncryptPdf.manipulatePdf(java.lang.String ,java.lang.String)';

</code>

am not able to call the function successfully.it is showing class "manipulatePdf" is not found in class EncryptPdf

Please assist
Re: Calling Java function from pl/sql [message #658332 is a reply to message #658330] Fri, 09 December 2016 10:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

https://www.google.fr/?q=oracle+call+java+class+from+pl%2Fsql

Re: Calling Java function from pl/sql [message #658333 is a reply to message #658330] Fri, 09 December 2016 11:02 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Bro am able to do the Hello.world classes but the above is bit complicated and i have used some external libraries.
Do i have to import these libraries??
itext.jar ??
Re: Calling Java function from pl/sql [message #658334 is a reply to message #658333] Fri, 09 December 2016 11:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

OK, all libraries/classes must be loaded into the database (using loadjava):
Database Java Developer's Guide, chapter 2 Java Applications on Oracle Database

Re: Calling Java function from pl/sql [message #658335 is a reply to message #658334] Fri, 09 December 2016 11:15 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Bro Sorry am very new to the oracle Java integration.

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

how to use the above libraries in my java code??


am using the below code for creating the java code:
create or replace and resolve java source named encryptpdf as



Re: Calling Java function from pl/sql [message #658336 is a reply to message #658335] Fri, 09 December 2016 11:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read the links I gave you.
In the first one, for instance, you'll find a complete example at http://everythingoracle.com/plsqlcljv.htm.
The second one is the basic documentation you have to know to use Java in Oracle application.

Re: Calling Java function from pl/sql [message #658337 is a reply to message #658336] Fri, 09 December 2016 11:27 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Sorry Dude..I have already gone through the links.
even in the link they have shown only a very simple java code (Hello.World)
But i need some examples of using external libraries please.
Re: Calling Java function from pl/sql [message #658338 is a reply to message #658337] Fri, 09 December 2016 11:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No, you didn't, you lie, and if you don't and nevertheless can't do it then tell your boss you are not skilled for this task and he has to find someone else.

Re: Calling Java function from pl/sql [message #658339 is a reply to message #658338] Fri, 09 December 2016 11:42 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Bro i dont find any examples using external java libraries..
I will definitely do it if some one could help.
Re: Calling Java function from pl/sql [message #658342 is a reply to message #658339] Fri, 09 December 2016 12:02 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
https://docs.oracle.com/javase/tutorial/deployment/selfContainedApps/addlibrary.html
Re: Calling Java function from pl/sql [message #658343 is a reply to message #658342] Fri, 09 December 2016 12:13 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Correct me as i have done few things:

Creating the java code as below and its successfully getting created:

create or replace and resolve java source named encryptpdf as

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class EncryptPdf
{

public static void manipulatePdf(String src, String dest , String USER_PASSWORD , String OWNER_PASSWORD ) throws IOException {
try
{
PdfReader pdfReader = new PdfReader(src);
PdfStamper pdfStamper = new PdfStamper(pdfReader,new FileOutputStream(dest));
pdfStamper.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(),2052,10);
pdfStamper.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}

}
}


Creating the oracle procedure to call the above created java function:


create or replace
PROCEDURE PR_encryptpdf_1 (src in varchar2,dest in varchar2,user_pwd in varchar2,owner_pwd in varchar2)
AS LANGUAGE JAVA
NAME 'EncryptPdf.manipulatePdf(java.lang.String ,java.lang.String ,java.lang.String ,java.lang.String)';


when i run the procedure am getting the below error:

ORA-29532: Java call terminated by uncaught Java exception: java.lang.NoClassDefFoundError: !!!ERROR!!! generated by genmissing
ORA-06512: at "CRDB.PR_ENCRYPTPDF_1", line 1
ORA-06512: at line 12
Re: Calling Java function from pl/sql [message #658344 is a reply to message #658343] Fri, 09 December 2016 12:18 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals, as solution depends on it.

Use SQL*Plus and copy and paste your session, the WHOLE session.

Re: Calling Java function from pl/sql [message #658345 is a reply to message #658343] Fri, 09 December 2016 12:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SQL> create or replace and resolve java source named encryptpdf as
  2
  3  import com.itextpdf.text.pdf.PdfReader;
  4  import com.itextpdf.text.pdf.PdfStamper;
  5  import java.io.FileNotFoundException;
  6  import java.io.FileOutputStream;
  7  import java.io.IOException;
  8
  9
 10  public class EncryptPdf
 11  {
 12
 13  public static void manipulatePdf(String src, String dest , String USER_PASSWORD , String OWNER_PASSWORD ) throws IOException {
 14  try
 15  {
 16  PdfReader pdfReader = new PdfReader(src);
 17  PdfStamper pdfStamper = new PdfStamper(pdfReader,new FileOutputStream(dest));
 18  pdfStamper.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(),2052,10);
 19  pdfStamper.close();
 20  }
 21  catch (FileNotFoundException e)
 22  {
 23  e.printStackTrace();
 24  }
 25
 26  }
 27  }
 28  /

Warning: Java created with compilation errors.

SQL> sho err
Errors for JAVA SOURCE "ENCRYPTPDF":
LINE/COL ERROR
-------- -------------------------------------------------------------------------------------
0/0      ENCRYPTPDF:1: cannot find symbol
0/0      6 errors
0/0      location: package com.itextpdf.text.pdf
0/0      import com.itextpdf.text.pdf.PdfReader;
0/0      ^
0/0      ENCRYPTPDF:2: cannot find symbol
0/0      symbol  : class PdfStamper
0/0      location: package com.itextpdf.text.pdf
0/0      import com.itextpdf.text.pdf.PdfStamper;
0/0      ^
0/0      ENCRYPTPDF:14: cannot find symbol
0/0      symbol  : class PdfReader
0/0      location: class EncryptPdf
0/0      PdfReader pdfReader = new PdfReader(src);
0/0      ^
0/0      ENCRYPTPDF:14: cannot find symbol
0/0      symbol  : class PdfReader
0/0      location: class EncryptPdf
0/0      PdfReader pdfReader = new PdfReader(src);
0/0      ^
0/0      ENCRYPTPDF:15: cannot find symbol
0/0      symbol  : class PdfStamper
0/0      location: class EncryptPdf
0/0      PdfStamper pdfStamper = new PdfStamper(pdfReader,new FileOutputStream(dest));
0/0      ^
0/0      ENCRYPTPDF:15: cannot find symbol
0/0      symbol  : class PdfStamper
0/0      location: class EncryptPdf
0/0      PdfStamper pdfStamper = new PdfStamper(pdfReader,new FileOutputStream(dest));
0/0      ^
0/0      symbol  : class PdfReader
Re: Calling Java function from pl/sql [message #658347 is a reply to message #658345] Fri, 09 December 2016 12:34 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member

SQL> create or replace and resolve java source named encryptpdf as
  2
  3  import com.itextpdf.text.pdf.PdfReader;
  4  import com.itextpdf.text.pdf.PdfStamper;
  5  import java.io.FileNotFoundException;
  6  import java.io.FileOutputStream;
  7  import java.io.IOException;
  8
  9
 10  public class EncryptPdf
 11  {
 12
 13    public static void manipulatePdf(String src, String dest , String USER_PA
SSWORD , String OWNER_PASSWORD ) throws IOException {
 14       try
 15       {
 16       PdfReader pdfReader = new PdfReader(src);
 17       PdfStamper pdfStamper = new PdfStamper(pdfReader,new FileOutputStream(
dest));
 18       pdfStamper.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBy
tes(),2052,10);
 19       pdfStamper.close();
 20        }
 21       catch (FileNotFoundException e)
 22        {
 23                e.printStackTrace();
 24        }
 25
 26    }
 27  }
 28  /

Java created.

Elapsed: 00:00:00.15

For me am not getting any errors.

will you be able to import those external libraries in your Database.
Re: Calling Java function from pl/sql [message #658348 is a reply to message #658347] Fri, 09 December 2016 12:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Michel Cadot wrote on Fri, 09 December 2016 19:18

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals, as solution depends on it.

Use SQL*Plus and copy and paste your session, the WHOLE session.
Quote:
will you be able to import those external libraries in your Database.
Yes, I am, and you too as you already did it otherwise you'd get the same errors than me.

Re: Calling Java function from pl/sql [message #658349 is a reply to message #658347] Fri, 09 December 2016 12:42 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member

create or replace
PROCEDURE PR_encryptpdf_1 (src in varchar2,dest in varchar2,user_pwd in varchar2,owner_pwd in varchar2)
AS LANGUAGE JAVA
NAME 'EncryptPdf.manipulatePdf(java.lang.String ,java.lang.String ,java.lang.String ,java.lang.String)';


once that java function has been created , we need to create the above stored proc and run the stored proc by using the below:


DECLARE
  SRC VARCHAR2(2000);
  DEST VARCHAR2(2000);
  USER_PWD VARCHAR2(200);
  OWNER_PWD VARCHAR2(200);
BEGIN
  SRC := '/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001.pdf';
  DEST := '/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001_u.pdf';
  USER_PWD := 'test';
  OWNER_PWD := 'kill';

  PR_ENCRYPTPDF_1(
    SRC => SRC,
    DEST => DEST,
    USER_PWD => USER_PWD,
    OWNER_PWD => OWNER_PWD
  );
END;


SRC := '/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001.pdf'; -- refers to a file in a database path
DEST := '/SANVOL/DW_REPORTS/REPORTS/CRDB_Statements/4010390898001_u.pdf'; -- creates the encryped file and this is the requirement and am facing the error while calling the java function.


Thanks Michael for helping this much
Re: Calling Java function from pl/sql [message #658350 is a reply to message #658349] Fri, 09 December 2016 12:43 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Can you please let me know how your importing the itext library ??

after importing will you be able to try it please.

Sorry for bothering you Sad
Re: Calling Java function from pl/sql [message #658351 is a reply to message #658350] Fri, 09 December 2016 12:45 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

As I already said, using loadjava, but you know it as you already read the documentation I pointed you to.

Re: Calling Java function from pl/sql [message #658352 is a reply to message #658351] Fri, 09 December 2016 12:47 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
if you have loaded the library means can you try to create the same java function in your database now ??
Re: Calling Java function from pl/sql [message #658353 is a reply to message #658352] Fri, 09 December 2016 12:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This is explain in the documentation I pointed you too AND in the example I gave you the link.

Re: Calling Java function from pl/sql [message #658354 is a reply to message #658353] Fri, 09 December 2016 12:52 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Bro have you imported the library in your database ??

if yes can you please create the same java function in your database please
Re: Calling Java function from pl/sql [message #658355 is a reply to message #658354] Fri, 09 December 2016 12:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I have not these libraries so I can't load them.

And I still don't know your Oracle version.

Re: Calling Java function from pl/sql [message #658357 is a reply to message #658355] Fri, 09 December 2016 13:02 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Michael ,

Please find the downloadable .jar file in the below link:

https://mvnrepository.com/artifact/com.itextpdf/itextpdf/5.5.5


My oracle database version is 10g
Re: Calling Java function from pl/sql [message #658367 is a reply to message #658357] Sat, 10 December 2016 05:36 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Dear Micahel,

Awaiting your reply on the above??

Have tried importing the java libraries ??

Is the code working fine for you ??

awaiting for your expert advise ....
Re: Calling Java function from pl/sql [message #658368 is a reply to message #658367] Sun, 11 December 2016 00:24 Go to previous messageGo to next message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
Cool Dear All,

The task has been completed.

Now am able to encrypt a PDF in the database path using the java code and i have written a UTL_SMTP procedure to email the same to the user.

Thanks Michael for your support.


Cool Cool Cool
Re: Calling Java function from pl/sql [message #658369 is a reply to message #658368] Sun, 11 December 2016 00:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And the problem was?
How did you solve it?

Re: Calling Java function from pl/sql [message #658370 is a reply to message #658369] Sun, 11 December 2016 00:43 Go to previous message
dineshkumar18
Messages: 28
Registered: September 2016
Junior Member
The Java library which i was using was not supported for JDK 1.5 version and because of that i was facing some class missing issues.
Now i have imported a library which is compatible with JDK 1.5 as that the one which is been installed in my ORACLE Database.
If any help needed in encrypting a PDF using in oracle please reach me.
I will help Cool
Previous Topic: ORA-01780 string literal required error while fetching data from xml table.
Next Topic: Generate XML for table definiton
Goto Forum:
  


Current Time: Thu Mar 28 12:55:01 CDT 2024