Skip to Main Content

SQL & PL/SQL

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!

PL/SQL Procedure Calling Java Host Command Problem

357014Oct 23 2002
This is my first post to this forum so I hope I have chosen the correct one for my problem. I have copied a java procedure to call Unix OS commands from within a PL/SQL procedure. This java works well for some OS commands (Eg ls -la) however it fails when I call others (eg env). Can anyone please give me some help or pointers?

The java is owned by sys and it looks like this
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "ExecCmd" AS
//
//ExecCmd.java
//
import java.io.*;
import java.util.*;
//import java.util.ArrayList;
public class ExecCmd {
static public String[] runCommand(String cmd)
throws IOException {
// set up list to capture command output lines
ArrayList list = new ArrayList();
// start command running
System.out.println("OS Command is: "+cmd);
Process proc = Runtime.getRuntime().exec(cmd);
// get command's output stream and
// put a buffered reader input stream on it
InputStream istr = proc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
// read output lines from command
String str;
while ((str = br.readLine()) != null)
list.add(str);
// wait for command to terminate
try {
proc.waitFor();
}
catch (InterruptedException e) {
System.err.println("process was interrupted");
}
// check its exit value
if (proc.exitValue() != 0)
System.err.println("exit value was non-zero: "+proc.exitValue());
// close stream
br.close();
// return list of strings to caller
return (String[])list.toArray(new String[0]);
}
public static void main(String args[]) throws IOException {
try {
// run a command
String outlist[] = runCommand(args[0]);
for (int i = 0; i < outlist.length; i++)
System.out.println(outlist);
}
catch (IOException e) {
System.err.println(e);
}
}
}

The PL/SQL looks like so:
CREATE or REPLACE PROCEDURE RunExecCmd(Command IN STRING) AS
LANGUAGE JAVA NAME 'ExecCmd.main(java.lang.String[])';

I have granted the following permissions to a user who wishes to run the code:

drop public synonym RunExecCmd
/
create public synonym RunExecCmd for RunExecCmd
/
grant execute on RunExecCmd to FRED
/
grant javasyspriv to FRED;
/
Execute dbms_java.grant_permission('FRED','java.io.FilePermission','/bin/env','execute');
commit
/
Execute dbms_java.grant_permission('FRED','java.io.FilePermission','/opt/oracle/live/9.0.1/dbs/*','read, write, execute');
commit
/

The following test harness has been used:
Set Serverout On size 1000000;
call dbms_java.set_output(1000000);
execute RunExecCmd('/bin/ls -la');
execute RunExecCmd('/bin/env');

The output is as follows:
SQL> Set Serverout On size 1000000;
SQL> call dbms_java.set_output(1000000);

Call completed.

SQL> execute RunExecCmd('/bin/ls -la');
OS Command is: /bin/ls -la
total 16522
drwxrwxr-x 2 ora9sys dba 1024 Oct 18 09:46 .
drwxrwxr-x 53 ora9sys dba 1024 Aug 13 09:09 ..
-rw-r--r-- 1 ora9sys dba 40 Sep 3 11:35 afiedt.buf
-rw-r--r-- 1 ora9sys dba 51 Sep 3 09:52 bern1.sql

PL/SQL procedure successfully completed.

SQL> execute RunExecCmd('/bin/env');
OS Command is: /bin/env
exit value was non-zero: 127

PL/SQL procedure successfully completed.

Both commands do work when called from the OS command line.

Any help or assistance would be really appreciated.

Regards,

Bernard.


Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2002
Added on Oct 23 2002
4 comments
904 views