将退出代码从 Java 返回到 PL/SQL

Getting exit code from Java back to PL/SQL

我正在尝试将 Java 方法的退出代码返回到 PL/SQL。似乎总是有例外。我怎样才能找出异常 n是。我在 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

你得到了一个异常,因为你得到了 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 Whosebug. The PL/SQL to grant this is dbms_java.grant_permission( 'Whosebug', 'SYS:java.io.FilePermission', '/bin/ls', 'execute' )
ORA-06512: at "Whosebug.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

当然,您可能会遇到不同的异常,但在开始解决真正的问题之前,您需要找出它是什么。

问题是 oracle.exe 是 运行 作为 machine\oracle 用户并且在我的目录中没有权限。当我将所有内容移动到 C:\app\oracle\product.1.0\dbhome_1\BIN 时,它开始按预期工作。该解决方案来自 Oracle 社区站点。 https://community.oracle.com/thread/3669270

非常感谢所有提出建议的人。