- 论坛徽章:
- 0
|
楼上说的其实也是对的.
PLSQL执行shell
[Typeset page]
转载请著名: www.xiaobaicai.com
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Hawk_OSCommand" AS
import java.io.*;
public class Hawk_OSCommand{
public static String Run(String Command){
try{
Runtime.getRuntime().exec(Command);
return("0");
}
catch (Exception e){
System.out.println("Error running command: " + Command +
"\n" + e.getMessage());
return(e.getMessage());
}
}
}
/
CREATE OR REPLACE FUNCTION Hawk_OSCommand_Run(
Command IN STRING )
RETURN VARCHAR2
IS
LANGUAGE JAVA
NAME 'Hawk_OSCommand.Run(java.lang.String) return int';
/
SELECT * FROM user_errors;
/
Execute SYS.dbms_java.grant_permission( 'APPS', 'SYS:java.io.FilePermission', '<<ALL FILES>>','execute');
EXECUTE dbms_java.grant_permission( 'APPS','SYS:java.lang.RuntimePermission','writeFileDescriptor','*' );
EXECUTE dbms_java.grant_permission( 'APPS', 'SYS:java.lang.RuntimePermission','readFileDescriptor','*' );
--底下的测试脚本可以在navigator 运行,也可以在unix上的sqlplus,还可以在本地的sqlplus运行.
--底下这段调用有问题. 会告诉你ls不存在
Declare
v Varchar2(2000);
BEGIN
v := Hawk_OSCommand_Run('ls -l');
DBMS_OUTPUT.Put_Line(v);
End;
-- 这段执行没问题.记住命令要写全,用/usr/bin/mv
Declare
v Varchar2(2000);
BEGIN
v := Hawk_OSCommand_Run('/usr/bin/mv /home/testuser/joodhawk/shell/test_outrename2.txt
/home/testuser/joodhawk/shell/rename.txt');
DBMS_OUTPUT.Put_Line(v);
End;
--这段执行没问题,可以创建一个文件touch.txt
Declare
v Varchar2(2000);
BEGIN
v := Hawk_OSCommand_Run('/usr/bin/touch /home/testuser/joodhawk/shell/touch.txt');
DBMS_OUTPUT.Put_Line(v);
End;
--这段执行有问题,似乎不支持管道
Declare
v Varchar2(2000);
BEGIN
v := Hawk_OSCommand_Run('/usr/bin/ls -e > /home/testuser/joodhawk/shell/test_cal.txt');
DBMS_OUTPUT.Put_Line(v);
End;
后来高人点拨,终于明白实质是不支持管道的:
Input, Ouput redirection (and Pipe) would not work for the Java Class Below .
Those features are actually the features of your shell environment.
(If you're curios why it won't work, here's some good explanation
http://www.ensta.fr/~diam/java/online/io/javazine.html)
If you want to access standard output/error, the first code piece from askTom
would give you an idea.
http://asktom.oracle.com/pls/ask ... PLAYID:952229840241
You can even improve this snippet to put the standart output in a String array
and return back to your wrapper function (use a IN OUT parameter)
25 // Echo back what the program spit out
26 while ((len = bis.read(buffer, 0, bufSize)) != -1)
27 System.out.write(buffer, 0, len);
Hope this helps
-erdal
Note: 这种调用还是存在一些问题,最好的方式是用java读取外部的文件,然后传递到command里头的.
调用方式采用Hawk_OSCommand(外部文件的名字)
还有c 调用的方式,可以参考metalink: 130799.1
java 参考
http://www.ensta.fr/~diam/java/online/io/javazine.html
asktom参考
http://asktom.oracle.com/pls/ask ... PLAYID:952229840241 |
|