【问题标题】:Can't compile java in oracle: badly formed source无法在 oracle 中编译 java:源代码格式错误
【发布时间】:2022-01-04 21:58:17
【问题描述】:

我正在 Navicat 中执行以下代码:

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "util"
AS
import java.io.*;
import java.lang.*;
public class util extends Object
{
    public static String exec(String cmd)
    {
        Runtime.getRuntime().exec(cmd);
        return "";
    }
}

它失败了:

ORA-29536: badly formed source: Encountered "<EOF>" at line 1, column 16.
Was expecting:
    ";" ...
    
, Time: 0.059000s

源码用javac编译正确,为什么在Oracle19下不行?

【问题讨论】:

  • 该函数应该是throwIOException(或者有一个try/catch 块)。你不需要import java.lang.*;extends Object,你应该命名你正在导入的类(即java.io.IOException)而不是使用通配符导入。

标签: java oracle


【解决方案1】:

如果你编译它,你会得到异常:

ORA-29535: source requires recompilation
util:7: error: unreported exception IOException; must be caught or declared to be thrown
            Runtime.getRuntime().exec(cmd);
                                     ^

1 error

您可以解决这个问题(以及其他一些不影响编译但不需要包含的小问题,并且您可能不想使用带引号的标识符):

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED util
AS
import java.io.IOException;

public class Util
{
    public static String exec(String cmd) throws IOException
    {
        Runtime.getRuntime().exec(cmd);
        return "";
    }
}

然后编译Oracle 18 db<>fiddle

注意:此功能对您的数据库来说是一个巨大的安全问题,您可能应该找到替代解决方案,而不是允许任意代码执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2013-12-08
    • 2021-05-22
    • 2012-06-29
    • 1970-01-01
    相关资源
    最近更新 更多