Running OS command using java procedure in Oracle [message #207533] |
Tue, 05 December 2006 22:56 |
reena_ch30
Messages: 100 Registered: December 2005
|
Senior Member |
|
|
Hi,
I tried the java option to run OS command. Following are the steps performed :-
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "OSCommand" AS
import java.io.*;
public class OSCommand{
public static String Run(String Command){
try{
Runtime.getRuntime().exec(Command);
return("0");
}
catch (Exception e){
System.out.println("Error running command: " + Command +
"\n" + e.getMessage());
return(e.getMessage());
}
Java created.
SQL> CREATE or REPLACE FUNCTION OSCommand_Run(Command IN STRING)
2 RETURN VARCHAR2 IS
3 LANGUAGE JAVA
4 NAME 'OSCommand.Run(java.lang.String) return int';
5 /
Function created.
Execute dbms_java.grant_permission( 'SCOTT','SYS:java.io.FilePermission', '<<ALL FILES>>','execute');
execute dbms_java.grant_permission( 'SCOTT','SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '*' );
execute dbms_java.grant_permission( 'SCOTT','SYS:java.lang.RuntimePermission', 'readFileDescriptor', '*' );
SQL> Declare
2 x Varchar2(2000);
3 Begin
4 x := OSCommand_Run('c:\windows\system32\calc');
5 DBMS_OUTPUT.Put_Line(x);
6 End;
7 /
0
PL/SQL procedure successfully completed.
Now when i execute the function, it returns 0 and completes successfully, but the executable is not opened. How to do that??Please help.
Thanks
Reena
|
|
|
|
|
Re: Running OS command using java procedure in Oracle [message #207819 is a reply to message #207533] |
Thu, 07 December 2006 02:40 |
tahpush
Messages: 961 Registered: August 2006 Location: Stockholm/Sweden
|
Senior Member |
|
|
Windows Specific Information
============================
Runtime.getRuntime().exec(Command) within Java makes a call to the WIN32 API function CreateProcess which launches another process or application(an executable).
Commands like COPY and RENAME are not applications, they are
shell commands that are implemented with the command interpreter (cmd.exe).
In order to execute these shell comannds, you will need to pass the name of the command interpeter with a /c parameter. For Example:
Set Serverout On
Declare
x Varchar2(2000);
Begin
x := OSCommand_Run('cmd.exe /c copy a.txt b.txt');
DBMS_OUTPUT.Put_Line(x);
End;
/
Trying to invoke applications such as Notepad.exe require the ability for the Oracle Datbase Service to interact with the Desktop. In the Services applet in Control Panel, click on the Oracle Database Service, click Startup, and ensure that 'Allow Service to Interact with Desktop' is checked.
....laborate
I have never done this before but it should be possible to do
[Updated on: Thu, 07 December 2006 02:51] Report message to a moderator
|
|
|
|
|