Home > Software engineering >  Can't compile java in oracle: badly formed source
Can't compile java in oracle: badly formed source

Time:11-27

I'm executing the following code in 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 "";
    }
}

And it fails with:

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

The source code compiles correctly with javac, why won't it work in Oracle19?

CodePudding user response:

If you compile it you get the exception:

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

You can fix that (and a few other minor things that don't affect compilation but aren't necessary to include, and you probably don't want to use a quoted identifier):

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 "";
    }
}

Then it compiles Oracle 18 db<>fiddle.

Note: this function is a huge security issue for your database and you probably should find an alternative solution rather than allowing arbitrary code execution.

  • Related