Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can I get the exit code of a .bat script from Java back to PL/SQL?

HonestSeekerFeb 2 2015 — edited Feb 6 2015

When calling a .bat script, only the exit code 1 returns. How can I get 7 to return to PL/SQL in the sample code below?

C:\Users\pwatson\src\java>type vv.sql

set echo on

-- call dbms_java.grant_permission('SYSTEM', 'SYS:java.io.FilePermission', '<<ALL FILES>>', 'execute');

HOST ECHO @EXIT /B 7 >vv.bat

HOST TYPE vv.bat

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED VV AS

import java.lang.Runtime;

public class VV

{

    public static int execute()

        throws java.io.IOException, java.lang.InterruptedException

    {

        int exitCode = 0;

        Runtime rt = java.lang.Runtime.getRuntime();

        Process proc = rt.exec("cmd.exe /C v.bat 1>NUL 2>NUL");

        proc.waitFor();

        exitCode = proc.exitValue();

        return exitCode;

    }

}

/

CREATE OR REPLACE FUNCTION JavaRunShellCommand

    RETURN NUMBER

AS LANGUAGE JAVA

    NAME 'VV.execute() return oracle.sql.NUMBER';

/

SHOW ERRORS

VARIABLE n NUMBER;

CALL JavaRunShellCommand() INTO :n;

PRINT n

SHOW ERRORS

SELECT JavaRunShellCommand() AS R FROM DUAL;

SHOW ERRORS

HOST DEL v.bat

EXIT

This post has been answered by Billy Verreynne on Feb 4 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 6 2015
Added on Feb 2 2015
10 comments
2,202 views