我正在尝试将Java方法的退出代码返回到PL / SQL。似乎总有一个例外。我怎样才能知道什么是例外 是的。我在12c上运行。
SQL> CREATE OR REPLACE JAVA SOURCE NAMED UTLCMD AS
2 import java.lang.Runtime;
3 public class UTLCMD
4 {
5 public static int execute(String command)
6 {
7 int exitCode = 0;
8
9 try
10 {
11 Runtime rt = java.lang.Runtime.getRuntime();
12 Process proc = rt.exec(command);
13 exitCode = proc.waitFor();
14 }
15 catch(Exception e)
16 {
17 System.out.println(e.getMessage());
18 exitCode = 12;
19 }
20
21 return exitCode;
22 }
23 }
24 /
Java created.
SQL> CREATE OR REPLACE FUNCTION JavaRunShellCommand(
2 command IN VARCHAR2)
3 RETURN NUMBER
4 AS LANGUAGE JAVA
5 NAME 'UTLCMD.execute(java.lang.String) return oracle.sql.NUMBER';
6 /
Function created.
以下是尝试调用该方法的结果。
SQL> VARIABLE n NUMBER;
SQL> CALL JavaRunShellCommand('cmd.exe /C CALL C:\Users\pwatson\src\java\my.bat') INTO :n;
Call completed.
SQL> PRINT n
N
----------
12
SQL> CALL JavaRunShellCommand('cmd.exe /C EXIT /B 3') INTO :n;
Call completed.
SQL> PRINT n
N
----------
12
SQL> SELECT JavaRunShellCommand('cmd.exe /C CALL C:\Users\pwatson\src\java\my.bat') AS R FROM DUAL;
R
----------
12
SQL> SELECT JavaRunShellCommand('cmd.exe /C EXIT /B 3') AS R FROM DUAL;
R
----------
12
答案 0 :(得分:2)
你得到一个异常,因为你得到12回来了,但是你正在捕获所有异常,只用System.out.println()
显示它们,并且Java控制台输出进入跟踪文件 - 你(或你的DBA)将需要找到正确的文件并提取实际的错误。
抛出异常会更简单:
CREATE OR REPLACE JAVA SOURCE NAMED UTLCMD AS
import java.lang.Runtime;
public class UTLCMD
{
public static int execute(String command)
throws java.lang.InterruptedException, java.io.IOException
{
int exitCode = 0;
Runtime rt = java.lang.Runtime.getRuntime();
Process proc = rt.exec(command);
exitCode = proc.waitFor();
return exitCode;
}
}
/
如果我用命令运行(在我的情况下是Unix-y),那么我看到了真正的问题:
exec :n := JavaRunShellCommand('/bin/ls /tmp');
ORA-29532: Java call terminated by uncaught Java exception: java.security.AccessControlException: the Permission (java.io.FilePermission /bin/ls execute) has not been granted to STACKOVERFLOW. The PL/SQL to grant this is dbms_java.grant_permission( 'STACKOVERFLOW', 'SYS:java.io.FilePermission', '/bin/ls', 'execute' )
ORA-06512: at "STACKOVERFLOW.JAVARUNSHELLCOMMAND", line 1
ORA-06512: at line 1
29532. 00000 - "Java call terminated by uncaught Java exception: %s"
*Cause: A Java exception or error was signaled and could not be
resolved by the Java code.
*Action: Modify Java code, if this behavior is not intended.
与我在跟踪文件中看到的方式相同,但是更容易访问。
如果我通过发出dbms_java.grant_permission()
调用(作为DBA)来解决我的问题,因为错误有助于告诉我,并重新连接,它现在可以正常工作:
exec :n := JavaRunShellCommand('/bin/ls /tmp');
anonymous block completed
print n
N
-
0
当然,您可能会遇到不同的例外,但在开始解决实际问题之前,您需要了解它是什么。
答案 1 :(得分:0)
问题是oracle.exe作为machine \ oracle用户运行,并且在我的目录中没有权限。当我将所有内容移动到C:\ app \ oracle \ product \ 12.1.0 \ dbhome_1 \ BIN时,它开始按预期工作。该解决方案来自Oracle社区站点。 https://community.oracle.com/thread/3669270
非常感谢所有有建议的人。