How do a call a Java jar file from Oracle PL/SQL?

Ground Hog picture Ground Hog · Jan 31, 2013 · Viewed 7.2k times · Source

My customer wants to be able to call a jar file from Oracle PL/SQL.

Java 1.6, Oracle 11g R2

How do I do this?

Answer

Ground Hog picture Ground Hog · Feb 3, 2013

I did a bit of research into loadjava (the program that loads classes into Oracle). Sounded like a pain in the butt.

So I opted to run my jar outside Oracle but called from Oracle DBMS_SCHEDULER by PL/SQL.

Here's how:

BEGIN
DBMS_SCHEDULER.CREATE_PROGRAM (
program_name            => 'testjar',
program_type            => 'EXECUTABLE',
program_action          => 'C:\MYSTUFF\testjar.bat',
enabled                 => TRUE,
comments                => 'test testjar'
);
END;
/

begin  
dbms_scheduler.create_job  
 (job_name => 'testjar_job',  
  program_name=> 'testjar',    
  enabled=>true,  
  auto_drop=>false,  
  comments=>'Only run immediately by dbms_scheduler.run_job');  
end; 
/


begin  
    dbms_scheduler.run_job('testjar_job',TRUE);  
end;  
/