Home » RDBMS Server » Server Utilities » Running OS command using java procedure in Oracle
Running OS command using java procedure in Oracle [message #207533] Tue, 05 December 2006 22:56 Go to next message
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 #207677 is a reply to message #207533] Wed, 06 December 2006 09:30 Go to previous messageGo to next message
tahpush
Messages: 961
Registered: August 2006
Location: Stockholm/Sweden
Senior Member

Hmm dont know that you can do it that way....

But if you only want to open windows calculator

Just type

SQL> host calc

in the SQL*Plus prompt and it will pop up
Re: Running OS command using java procedure in Oracle [message #207759 is a reply to message #207533] Wed, 06 December 2006 21:57 Go to previous messageGo to next message
reena_ch30
Messages: 100
Registered: December 2005
Senior Member
Hi,

I want to call that using pl/sql. When i check the windows active processes in the taskbar ,i can see the calc.exe but it doesn't pop up!!!


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 Go to previous messageGo to next message
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

Re: Running OS command using java procedure in Oracle [message #208491 is a reply to message #207533] Mon, 11 December 2006 01:18 Go to previous messageGo to next message
reena_ch30
Messages: 100
Registered: December 2005
Senior Member
Hi,

I tried that option.And it worked!!! Thanks a lot for spending time on this.
Reena
Re: Running OS command using java procedure in Oracle [message #208494 is a reply to message #208491] Mon, 11 December 2006 01:27 Go to previous message
tahpush
Messages: 961
Registered: August 2006
Location: Stockholm/Sweden
Senior Member

Excellent Smile
Glad it worked out for you
Previous Topic: Optimised solution for Data Migration
Next Topic: exporting data from oracle to excel worksheet
Goto Forum:
  


Current Time: Sun Dec 11 00:32:32 CST 2016

Total time taken to generate the page: 0.05743 seconds