Solved

calling an external java program from an oracle plsql procedure

Posted on 2004-10-07
2
714 Views
Last Modified: 2013-12-12
Hi,

I am calling a java program from an Oracle (9.1) stored procedure in a web browser

the oracle procedure is in schema OTIF_Executor that has
connect, resource, javasyspriv privileges.

the java program takes in a cmd and exeutes it on the operating system (windows server 2000).

I've had it working before my db crashed, I then rebuilt the database and now cannot get the procedure working.

--------------------------------------------------------------
begin..

lv_Cmd:= 'cscript //nologo D:\KPI\SCRIPTS\'||lv_file_name||'';
lv_Res:= pty_osutil_pkg.fn_runoscmd(lv_Cmd, my_tab);

 IF lv_Res = 1 THEN
           NULL;
 ELSE
          RAISE java_broken;
 END IF;


--PL\SQL wrapper for the java class ExecCmd which is stored in the database
FUNCTION fn_exec_cmd(p_shell VARCHAR2,p_cmd VARCHAR2,p_output IN OUT cmd_output) RETURN VARCHAR2
as
LANGUAGE JAVA NAME 'com.bat.utils.OSExecutor.executeCommand(java.lang.String,java.lang.String,oracle.sql.ARRAY[]) return java.lang.String';
      
            
FUNCTION fn_RunOsCmd(p_cmd IN VARCHAR2,   tab_cmdoutput IN OUT osutil_cmd_output) RETURN VARCHAR2 IS

tab_output cmd_output;
tab_errors cmd_output;                                    
lv_shell VARCHAR2(10);
lv_cmd VARCHAR2(255);
lv_success VARCHAR2(32767);
lv_output_count INTEGER :=0;

 BEGIN

   IF UPPER(substr(p_cmd,1,3)) ='RSH' THEN
           lv_shell := 'rsh';
        lv_cmd := substr(p_cmd,4,length(p_cmd)-3);
   ELSE
            lv_shell := 'cmd /c';
         lv_cmd := p_cmd;
   END IF;
 
   -- Execute command
 
   lv_success := fn_exec_cmd(lv_shell,lv_cmd, tab_output);
 
   IF tab_output.COUNT >0 THEN
      FOR x IN tab_output.first..tab_output.last LOOP                
              tab_cmdoutput(lv_output_count+x) := tab_output(x);
        END LOOP;
        lv_output_count := tab_cmdoutput.COUNT;
   END IF;
   
   RETURN lv_success;       

I did not have this issue before. I created a collection as:
CREATE OR REPLACE
type CMD_OUTPUT as table of varchar2(255)
/

again, originally this worked. I then started getting
ORA-06531: Reference to uninitialized collection

the only way I could get round this was to initialse the collection

e.g.

  IF UPPER(substr(p_cmd,1,3)) ='RSH' THEN
           lv_shell := 'rsh';
        lv_cmd := substr(p_cmd,4,length(p_cmd)-3);
   ELSE
            lv_shell := 'cmd /c';
         lv_cmd := p_cmd;
   END IF;
 
   tab_output:= CMD_OUTPUT('A','B','C','X','Y','Z');
   -- Execute command
 
   lv_success := fn_exec_cmd(lv_shell,lv_cmd, tab_output);

the procedure then has no error message but will not execute the command.

I know there are no issues with the java program so it has to be due to me putting
a) tab_output:= CMD_OUTPUT('A','B','C','X','Y','Z');
or
b) not setting up the correct privileges in the database

any ideas?
0
Comment
Question by:joehodge
2 Comments
 
LVL 23

Accepted Solution

by:
seazodiac earned 250 total points
ID: 12249167
glad that you nailed it.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now