public static void main(String [] args) { if (args.length >= 1) { int r = Util.RunThis(args[0]); System.out.println("Command executed, return code = " + r); } else { System.out.println("Usage: java Util <command1> <command2> ... <commandN>"); } }
public static int RunThis(String args) { Runtime rt = Runtime.getRuntime(); int rc = -1; try { Process p = rt.exec(args); int bufSize = 4096; BufferedInputStream bis = new BufferedInputStream(p .getInputStream(), bufSize); int len; byte buffer[] = new byte[bufSize];
create or replace and compile java source named Util as import java.io.*; import java.lang.*; public class Util extends Object { public static int RunThis(String args) { Runtime rt = Runtime.getRuntime(); int rc = -1; try { Process p = rt.exec(args); int bufSize = 4096; BufferedInputStream bis = new BufferedInputStream(p.getInputStream(), bufSize); int len; byte buffer[] = new byte[bufSize];
create or replace function RUN_CMD(p_cmd in varchar2) return number as language java name 'Util.RunThis(java.lang.String) return integer'; /
create or replace procedure RC(p_cmd in varchar2) as x number; begin x := run_cmd(p_cmd); end;
/
-- 然后进行调用
SQL> vari x number; SQL> set serveroutput on SQL> exec dbms_java.set_output(100000);
PL/SQL procedure successfully completed
SQL> exec :x := RUN_CMD('ipconfig'); java.security.AccessControlException: the Permission (java.io.FilePermission < <ALL FILES>> execute) has not been granted to SYSTEM. The PL/SQL to grant this is dbms_java.grant_permission( 'SYSTEM', 'SYS:java.io.FilePermission', ' < <ALL FILES>>', 'execute' ) at java.security.AccessControlContext.checkPermission(AccessControlContext.java) at java.security.AccessController.checkPermission(AccessController.java) at java.lang.SecurityManager.checkPermission(SecurityManager.java) at oracle.aurora.rdbms.SecurityManagerImpl.checkPermission(SecurityManagerImpl.java) at java.lang.SecurityManager.checkExec(SecurityManager.java) at java.lang.Runtime.exec(Runtime.java) at java.lang.Runtime.exec(Runtime.java) at java.lang.Runtime.exec(Runtime.java) at java.lang.Runtime.exec(Runtime.java) at Util.RunThis(UTIL.java:11)
PL/SQL procedure successfully completed x --------- -1