Comments on: Stored Java to run an OS command, copy a file and get a directory listing in Oracle http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/ Friends of Oracle and Java Tue, 21 Oct 2014 18:48:09 +0000 hourly 1 http://wordpress.org/?v=4.0 By: swetha http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1708 Thu, 04 Sep 2008 22:49:09 +0000 /?p=394#comment-1708 We are trying to do similar as explained by you. We want to execute “java -jar “C:\\Documents and Settings\\skulkar\\Desktop\\TestExec.jar\” ” through Runtime.getRuntime().exec(); but always ending error – java doesn’t exist … do i need to provide some permissions for PL/SQL to understand the java command ? Please advice

]]>
By: Manju http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1707 Thu, 15 Mar 2007 04:49:22 +0000 /?p=394#comment-1707 Hi

Is there any way to call the AcroRd .exe from this program .since the program uses the Process class i thought is workable .but the pc hangs
Please advice

Thanks

]]>
By: Kenny http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1706 Thu, 30 Nov 2006 03:22:06 +0000 /?p=394#comment-1706 This was a great tip! I followed all the steps and was able to invoke a shell script from Stored java on the database. However, the script that gets invoked runs some commands such as touch files and `date`. The script runs and then stops at the first place where it gets to such a command. It does not give me any error messages. I am positive that the script is actually running. I have changed permissions on everything to allow access, so I am sure that is not it either. Any ideas?

]]>
By: GURWINDER SINGH http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1705 Tue, 14 Nov 2006 22:16:39 +0000 /?p=394#comment-1705 i have done above steps ,all are working fine but on last step
i received error SQL> exec rc(‘C:WINDOWSsystem32cmd.EXE /c dir C:SCOTT’)
BEGIN rc(‘C:WINDOWSsystem32cmd.EXE /c dir C:SCOTT’); END;

*
ERROR at line 1:
ORA-06501: PL/SQL: program error
ORA-06512: at “SCOTT.RC”, line 10
ORA-06512: at line 1
steps

please help.I will be verythankful to you for this

1.) create or replace type STRARRAY as table of varchar2(255)
/

2.)CREATE OR REPLACE PACKAGE UTIL AS
FUNCTION RUN_CMD(p_cmd IN VARCHAR2) RETURN NUMBER;
PROCEDURE COPY_FILE(p_srcFile IN VARCHAR2, p_destDir IN VARCHAR2);
PROCEDURE GET_DIR_LIST(p_dir IN VARCHAR2, p_dirList OUT STRARRAY);
END UTIL;
/

3.)CREATE OR REPLACE PACKAGE BODY UTIL AS
FUNCTION RUN_CMD(p_cmd IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA NAME ‘nl.amis.util.RunCmd.runThis(java.lang.String) return int';
PROCEDURE COPY_FILE(p_srcFile IN VARCHAR2, p_destDir IN VARCHAR2)
AS LANGUAGE JAVA NAME ‘nl.amis.util.FileCopy.copy(java.lang.String, java.lang.String)';
PROCEDURE GET_DIR_LIST(p_dir IN VARCHAR2, p_dirList OUT STRARRAY)
AS LANGUAGE JAVA NAME ‘nl.amis.util.DirList.getList(java.lang.String, oracle.sql.ARRAY[])';
END UTIL;
/
4.)
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED “RunCmd” AS
package nl.amis.util;

import java.io.*;

public class RunCmd
{
private static void pass( java.lang.String p_in,
java.lang.String[] p_out )
{
if ( p_in != null ){
p_out[ 0 ] = p_in.toUpperCase();
}
}

public static int runThis(String cmd)
{
Runtime rt = Runtime.getRuntime();
int rc = -1;

String[] args = new String[ 4096 ];
pass(cmd, args);

try
{
Process p = rt.exec(args[ 0 ]);

int bufSize = 4096;
BufferedInputStream bis =
new BufferedInputStream(p.getInputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize];

// Echo back what the program spit out
while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);

rc = p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
rc = -1;
}
return rc;
}
}
/
5.)BEGIN
dbms_java.grant_permission( ‘scott’, SYS:java.io.FilePermission’, ‘C:WINDOWSSYSTEM32CMD.EXE’, ‘execute’ );
dbms_java.grant_permission( ‘scott’, ‘SYS:java.lang.RuntimePermission’, ‘*’, ‘writeFileDescriptor’ );
END;
/

6.)CREATE OR REPLACE PROCEDURE RC(
P_CMD IN VARCHAR2)
AS
X NUMBER;
BEGIN
X := UTIL.RUN_CMD (P_CMD);

IF (X 0)
THEN
RAISE PROGRAM_ERROR;
END IF;
END RC;
/
7.)SQL> set serveroutput on size 1000000
SQL> exec dbms_java.set_output(1000000)

PL/SQL procedure successfully completed.

8.)
SQL> exec rc(‘C:WINDOWSsystem32cmd.EXE /c dir C:SCOTT’)
BEGIN rc(‘C:WINDOWSsystem32cmd.EXE /c dir C:SCOTT’); END;

*
ERROR at line 1:
ORA-06501: PL/SQL: program error
ORA-06512: at “SCOTT.RC”, line 10
ORA-06512: at line 1

]]>
By: Karnail J Singh http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1704 Sat, 25 Feb 2006 04:44:16 +0000 /?p=394#comment-1704 I tried running the above procedure to execute the cmd but it just hangs the session
and eventually I have to kill it.

I want to use the above procedure to run the oracle reports (rwcli60.exe) to print
reports from a procedure.

Please advice me if you had encounted similar problem and if so is there a solution.

Thanks
Karnail

]]>
By: Arun http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1703 Mon, 27 Jun 2005 19:52:51 +0000 /?p=394#comment-1703 Can i use zip and unzip commands in this ?
my project requirements, want me to unzip a file from folder and then process it.
Can i use runcmd for that?

]]>
By: Vasso http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1702 Thu, 02 Jun 2005 10:36:32 +0000 /?p=394#comment-1702 Hello! Congratulations. Your solution has been very helpful because utl_file.fcopy couldn’t copy pdf files correctly (in 9i)
Thank you!

]]>
By: harm http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1701 Sat, 12 Feb 2005 10:33:32 +0000 /?p=394#comment-1701 Hi Gregory,

UTL_FILE is only capable of reading on and writing to the file system of the database server. How about your functions? I see that you perform I/O on your local C: drive. Can they I/O from any client PC or do you happen to run a local database?
The 10g version of UTL_FILE has a copy (utl_file.fcopy) procedure that can do a whole or partial copy of a file, but it is restricted only to the server file system.

*********************************************************************************************************

Hello Harm,

Thanks for your comment.
These (my) functionalities are also restricted to the database server filesystem.
I know about the Utl_File.Fcopy (since Oracle 9.2.0), but I needed it in 8.1.7.

Gregory

]]>
By: Lucas http://technology.amis.nl/2005/02/11/stored-java-to-run-an-os-command-copy-a-file-and-get-a-directory-listing/#comment-1700 Fri, 11 Feb 2005 14:27:41 +0000 /?p=394#comment-1700 Can you make the sources available as zip-file or perhaps even a JDev project, please?

]]>