Calling java host command in trigger/PLSQL
I created a java call to execute a linux host command that calls a shell script that will echo out a result. It is owned by SYS and has granted execute to SYSTEM. SYSTEM has a table that monitors accesses to the RDBMS. When a user logs on from a remote server vis sqlplus, this LOGON trigger write to the SYSTEM table (successfully). SYSTEM has a trigger that runs a linux command to execute a shell script that pulls the actual IP address from the remote system. I can run this call from a PLSQL block (outside the trigger) and get a response back like "user:101.101.101.111" but when I have the same user log on, the trigger fires - no errors or exceptions yet no rows are returned. Is there some restriction in a trigger versus just a plsql block call? The java code used is what I found on (http://www.oracle-base.com/articles/8i/ ... mPLSQL.php) and it works perfectly OUTSIDE the trigger but nothing is returned in the trigger firing steps. Any idea?
rdbms: 11.1.0.7, Redhat 4
I know the code works because I can write the host command output to a file. Later in the trigger I can open the file and can read the data that should have been returned in the java host call.
FYI - here is the code from your site that I used:
DROP JAVA SOURCE SYS."Host";
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED SYS."Host" as import java.io.*;
public class Host {
public static void executeCommand(String command) {
try {
String[] finalCommand;
if (isWindows()) {
finalCommand = new String[4];
// Use the appropriate path for your windows version.
finalCommand[0] = "C:\\windows\\system32\\cmd.exe"; // Windows XP/2003
//finalCommand[0] = "C:\\winnt\\system32\\cmd.exe"; // Windows NT/2000
finalCommand[1] = "/y";
finalCommand[2] = "/c";
finalCommand[3] = command;
else {
finalCommand = new String[3];
finalCommand[0] = "/bin/sh";
finalCommand[1] = "-c";
finalCommand[2] = command;
final Process pr = Runtime.getRuntime().exec(finalCommand);
pr.waitFor();
new Thread(new Runnable(){
public void run() {
BufferedReader br_in = null;
try {
br_in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String buff = null;
while ((buff = br_in.readLine()) != null) {
System.out.println("Cmd results: " + buff);
try {Thread.sleep(100); } catch(Exception e) {}
br_in.close();
catch (IOException ioe) {
System.out.println("Exception caught printing process output.");
ioe.printStackTrace();
finally {
try {
br_in.close();
} catch (Exception ex) {}
}).start();
new Thread(new Runnable(){
public void run() {
BufferedReader br_err = null;
try {
br_err = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String buff = null;
while ((buff = br_err.readLine()) != null) {
System.out.println("Cmd Error: " + buff);
try {Thread.sleep(100); } catch(Exception e) {}
br_err.close();
catch (IOException ioe) {
System.out.println("Exception caught printing process error.");
ioe.printStackTrace();
finally {
try {
br_err.close();
} catch (Exception ex) {}
}).start();
catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
public static boolean isWindows() {
if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1)
return true;
else
return false;
/
Similar Messages
-
PL/SQL Procedure Calling Java Host Command Problem
This is my first post to this forum so I hope I have chosen the correct one for my problem. I have copied a java procedure to call Unix OS commands from within a PL/SQL procedure. This java works well for some OS commands (Eg ls -la) however it fails when I call others (eg env). Can anyone please give me some help or pointers?
The java is owned by sys and it looks like this
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "ExecCmd" AS
//ExecCmd.java
import java.io.*;
import java.util.*;
//import java.util.ArrayList;
public class ExecCmd {
static public String[] runCommand(String cmd)
throws IOException {
// set up list to capture command output lines
ArrayList list = new ArrayList();
// start command running
System.out.println("OS Command is: "+cmd);
Process proc = Runtime.getRuntime().exec(cmd);
// get command's output stream and
// put a buffered reader input stream on it
InputStream istr = proc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
// read output lines from command
String str;
while ((str = br.readLine()) != null)
list.add(str);
// wait for command to terminate
try {
proc.waitFor();
catch (InterruptedException e) {
System.err.println("process was interrupted");
// check its exit value
if (proc.exitValue() != 0)
System.err.println("exit value was non-zero: "+proc.exitValue());
// close stream
br.close();
// return list of strings to caller
return (String[])list.toArray(new String[0]);
public static void main(String args[]) throws IOException {
try {
// run a command
String outlist[] = runCommand(args[0]);
for (int i = 0; i < outlist.length; i++)
System.out.println(outlist);
catch (IOException e) {
System.err.println(e);
The PL/SQL looks like so:
CREATE or REPLACE PROCEDURE RunExecCmd(Command IN STRING) AS
LANGUAGE JAVA NAME 'ExecCmd.main(java.lang.String[])';
I have granted the following permissions to a user who wishes to run the code:
drop public synonym RunExecCmd
create public synonym RunExecCmd for RunExecCmd
grant execute on RunExecCmd to FRED
grant javasyspriv to FRED;
Execute dbms_java.grant_permission('FRED','java.io.FilePermission','/bin/env','execute');
commit
Execute dbms_java.grant_permission('FRED','java.io.FilePermission','/opt/oracle/live/9.0.1/dbs/*','read, write, execute');
commit
The following test harness has been used:
Set Serverout On size 1000000;
call dbms_java.set_output(1000000);
execute RunExecCmd('/bin/ls -la');
execute RunExecCmd('/bin/env');
The output is as follows:
SQL> Set Serverout On size 1000000;
SQL> call dbms_java.set_output(1000000);
Call completed.
SQL> execute RunExecCmd('/bin/ls -la');
OS Command is: /bin/ls -la
total 16522
drwxrwxr-x 2 ora9sys dba 1024 Oct 18 09:46 .
drwxrwxr-x 53 ora9sys dba 1024 Aug 13 09:09 ..
-rw-r--r-- 1 ora9sys dba 40 Sep 3 11:35 afiedt.buf
-rw-r--r-- 1 ora9sys dba 51 Sep 3 09:52 bern1.sql
PL/SQL procedure successfully completed.
SQL> execute RunExecCmd('/bin/env');
OS Command is: /bin/env
exit value was non-zero: 127
PL/SQL procedure successfully completed.
Both commands do work when called from the OS command line.
Any help or assistance would be really appreciated.
Regards,
Bernard.Kamal,
Thanks for that. I have tried to use getErrorStream and it does give me more info. It appears that some of the commands cannot be found. I suspected that this was the case but I am not sure about how this can be as they all appear to reside in the same directory with the same permissions.
What is more confusing is output like so:
SQL> Set Serverout On size 1000000;
SQL> call dbms_java.set_output(1000000);
Call completed.
SQL> execute RunExecCmd('/usr/bin/id');
OS Command is: /usr/bin/id
exit value was non-zero: 1
id: invalid user name: ""
PL/SQL procedure successfully completed.
SQL> execute RunExecCmd('/usr/bin/which id');
OS Command is: /usr/bin/which id
/usr/bin/id
PL/SQL procedure successfully completed.
Regards,
Bernard -
Dear Friends,
Is it posible to call a host command (windows) from PLSQL (Oracle 9i) ?
Thanks.
Jai> [email protected]
[email protected]
[email protected]
So.. the one who dies with the most e-mail addresses wins?
Or what exactly is the purpose of posting all your e-mail addresses - besides as food for web bot e-mail harvesters used by spammers? -
How to execute host command in trigger
dear professional:
i'm trying to create a trigger that when a certain value inserts into a table , it fires some UNIX command to do the job following
here is what i tried ,create a trigger that gives host command , but when i tried insert , it fails to work
i also tried dbms_pipe ,but seems same when using dynamic sql to do the job
any idea of how to accomplish this task ?
many thanks ~
br/ricky
SQL> CREATE OR REPLACE TRIGGER price_exec
2 BEFORE INSERT ON omc.price_test
FOR EACH ROW
BEGIN
IF :NEW.price = 4 THEN
execute immediate 'host ll';
END IF;
END price_exec;
/ 3 4 5 6 7 8 9
Trigger created.
SQL> insert into price values ('test',4);
insert into price values ('test',4)
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> insert into price_test values ('test',4);
insert into price_test values ('test',4)
ERROR at line 1:
ORA-00900: invalid SQL statement
ORA-06512: at "OMC.PRICE_EXEC", line 3
ORA-04088: error during execution of trigger 'OMC.PRICE_EXEC'hi,
my db is 9.2
i'm aware of schedule you mentioned , but if it's up to the task , you don't know when a row with certain value is inserted
simple way :
when a row inserted into a table with value "catch me" you want to know who inserted this test value
so you tried to create a trigger to catch program, machine, module ,and terminal info from v$session
so you can figure out where it is coming from
and i want to go a step further , by executing some os command to digging out more
and i come up with this , when value 4 is inserted i exec a procedure host to execute command,
the problem is it's not working as i exepcted , so spare me the lecture of reading docs and just tell me what to do
i'd really appreciate it , thanks
CREATE OR REPLACE TRIGGER price_exec
BEFORE INSERT ON omc.price_test
FOR EACH ROW
BEGIN
IF :NEW.price = 4 THEN
execute immediate 'host('echo')';
END IF;
END;
Warning: Trigger created with compilation errors.
SQL> show errors
Errors for TRIGGER PRICE_EXEC:
LINE/COL ERROR
3/30 PLS-00103: Encountered the symbol "ECHO" when expecting one of
the following:
. ( * @ % & = - + ; < / > at in is mod not rem return
returning <an exponent (**)> <> or != or ~= >= <= <> and or
like between into using || bulk
The symbol ". was inserted before "ECHO" to continue. -
How to call a host command/program within a PL/SQL Procedure OR Block
Hello ,
I want to call a host program (in my case it is unix shell program) from within a PL/SQL
Procedure..Please let me know if you have a clue...Thanks a bunch...AjeetAlternatively you could create a PL/SQL procedure that wraps a Java Runtime object.
You can find an example of this in the Ask Tom column of Oracle Magazine. You can get there from OTN home page. Type "Java Runtime" into the Search Archive engine.
HTH, APC -
Hello All;
Is it possible to call the host command in Jdeveloper like it's called in Oracle Form such as host(command_1,NO_SCREEN)?
Thanks in advance for your reply.
TDTry the Runtime.exec method
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html -
Call HOST command FROM FORMS 4.5 AND 6.0
I have a problem that I found out about yesterday, and have no idea how to fixe it.
I created a form to run on form 4.5.9.7 In this form i call up the host command, but it does not work properly, because by using the host oracle stops there until i close the application that i called with the host, it hangs there. But now it doesn't do it, once the host is executed, it continues the rest of the code that i have, which should not.
On the same computer i created another form on forms 6.0.5.2 and call the host command again, and finally it worked perfect! I mean until the program called by host command is running forms does not continue, it hangs there.
Could someone explains me that please, is there some patch I need on forms 4.5?
nullWe converted forms from 4.5 to 6.0. Basically all you have to do is recompile the forms. but, the very first thing you have to do is recompile all the libraries. The forms will not work if you don't recompile the libraries first. So, open and compile the libraries in 6.0, then open and compile the forms in 6.0. They should then work fine. If they do not, try deleting and reattaching the libraries and compile again.
Good luck. -
Hi,
Suppose that I need to run 'zip' in the mean time when running Java, what should I use for calling such host command from O/S?
Thks & Bst Rgds,
HuaminRuntime.exec()
-
Hi,
I'm running windows 2000 O/S on my desktop to connect to an IBM AIX server to run Forms6. If you call the host command from within forms, does it go to the my desktop (client) or to AIX (server). Also, if I need a username and password to log onto AIX will I be prompted to for the username and password by the OS after issuing the host command? Please provide example if you can.
Thanks,
Leonard905When you execute a HOST command the code is executed on the machine on which your Forms engine is running.
In client server this will be the client.
In Web deployed Forms this will be the middle tier application server.
Regards
Grant Ronald
FOrms Product Management -
Within SQL*Plus, get error code of host command
Hello everyone,
I am currently writing an SQL*Plus (Oracle 10g) that has a big logic and somewhere in the middle, I have to call a host command, which is an C++ function.
I call it using
SQL> HOST cd
SQL> HOST cd bins
SQL> HOST ./my_procedureBut, I need to get the return code of my procedure to see if it finished correctly?
I checked [the manual|http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch4.htm#sthref883] but they don't say anything about return codes.
When I try:
SQL> host echo $?
0It always returns 0, even if I SIGTERM or SIGKILL the child process. Anyone has an idea on how is it possible to retrieve the return code?
Thanksuser13117585 wrote:
Hello guys and thanks for your answers.
Let me tell you a little more about my problem... I have a 3 steps process. The first step is done in the database. It updates a few tables. Then, when that step successfully ends, I need to start a second step. That second step is done on the server where I have my SQL Plus session. Because I actually have 2 different machines. The first one is where the database is running and the second one where I have SQL Plus (Oracle client 10g) and the program that I have to start in this second step. Then, once this process is finished and his return code is 0, then I have to execute the third step in the database.
Basically, I wanted to have something like:
VARIABLE returnCode
BEGIN
:returnCode := some_package.do_step_1;
-- do whatever I want with returnCode.
EXCEPTION WHEN ...
END;
HOST execute_external_program
BEGIN
IF external_job_succeed THEN
some_package.do_step_3
END IF;
EXCEPTION...
END;
/I wanted to use the Oracle Scheduler to create an EXTERNAL JOB. The problem is that the external program is not deployed on the same machine as the database.I know that on 11g, we have external remote jobs. But I'm on 10g and I have no agent on the server where the external program is. So, what are my options? SQL*Plus and the host command... Or I can also write a bash script to do that... Or any hight level programming language...
Any other idea?
Thanks again guys,move functionality of second step into PL/SQL procedure resident inside DB. -
Run like Host command in plsql
Hi,
We are using Host command in sql*plus , but i need to implement like host command in Plsql. I refered previous post and some other links , that most of them are prefered only java stored procedure, And even that restricted previllage.
Can you give some sample example launchiing Win OS command in plsql
venkiTo run a batch file, you need to use the Command Shell (and please do not call it a DOS shell like some people tend to do as it is not DOS). E.g.
c:\windows\system32\cmd.exe /c c:\temp\test.bat
We're running cmd.exe with switch /c that tells cmd to execute the command we're passing it, and then to terminate.
Note that we cannot interact with the shell from the PL/SQL side - we cannot answer prompts and so on. The script run, must be a proper batch/non-interactive script.
Also, the Oracle Server process (Win32 thread actually) that is servicing our Oracle client connection, is running the command for us. This thread itself is a background service process. It could run in Windows configured environment (VM/Virtual Machine) that is not allowed to interact with the desktop - or have limited or no access to certain files, folders and programs.
Technically speaking, this is what happens:
1) we pass the command to execute from our client to the Oracle server session servicing us
2) this Oracle thread uses the Java VM to make a Win32 call called CreateProcess()
3) the Win32 kernel executes that process
4) the process starts, runs and terminates
5) the Java VM inside the Oracle Server process regains control and pass the exit code and standard output of that process to PL/SQL
6) PL/SQL in turns, returns that very same data to our client
If you for example run a program that pauses and expects input, or hangs.. it will cause the above series of steps to stop at step 4. With the Java VM waiting on it to complete, and we waiting for the Java VM and PL/SQL call to complete. -
Is it possible to use a host command from inside an oracle trigger? I am trying to execute a unix script with a trigger.
Hi
There are few options.
In 7.x
Using the dbms_pipe send ur command to the pipe and use a cron job to read the pipe and execute the command.
In 8.x, it is more interesting.
Using External libraries/procedures you can map a c or java executable/dll to a procedure and call that from the trigger.
HTH
Arvind Balaraman
null -
Calling java class/procedure inside a trigger
Hi all
I want like to call a java procedure from a trigger.
i know that i should load the class then write call specification and then call the java stored procedure.
Before that what are the prerequisites ..
Do i need to install java on my local machine?
secondly for sample testing can you provide me a sample java code that will connect to my db and inserts some sample data into sample table
regards
rajHi Here are the Details: I have the following
Java File : Helloworld.java
Class File : HelloWorld.class
Location of Java and Class Files: C:\Program Files\Java\jdk1.6.0_13\
Method : main(java.lang.String[])
Helloworld.java
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
loadjava -u HR/hr@ORCL C:\Program Files\Java\jdk1.6.0_13\HelloWorldApp.class ( From where should i run this command ?should i go to windows--start--cmd ? )
CREATE OR REPLACE PROCEDURE sayhello ()
AS LANGUAGE JAVA
NAME 'HelloWorld.main(java.lang.String[])';
CREATE OR REPLACE TRIGGER sal_trig
AFTER UPDATE OF sal ON emp
FOR EACH ROW
CALL sayhello(); ------------------ ( where do i see the output when i call the procedure?)
regards
raj
Edited by: raj_fresher on May 20, 2009 7:57 AM -
Calling SQL Loader from Forms 4.5 using HOST command in Win2000
One of my forms calls SQL Loader 7.3 using HOST command to load a csv file onto the database(Oracle 7.3) under Windows 2000 platform.
Only after sql loader finishes its operation the control should come to my form i.e. synchronous execution.
As of now its running asynchronously.
Is there any way to make it synchronous. If Not any other work arounds?
Environment Details
Forms [32 Bit] Version 4.5.10.6.0 (Production)
Windows 2000 Operating System
Oracle7 Release 7.3.2.3.2Forms6i running on W2000, Rdbms 8.1.7
in Forms I added a button TEST,
Trigger when-button-pressed : host('test.bat') ;
in directory .......\frm I added file test.bat :
REM ===============
cd /d C:\........\ldr
pause
sqlldr parfile=test.par
pause
type test.log
pause
exit
REM ================
now, pressing TEST button opens DOS window, telling me what's going on, running sqlldr, finally going back to forms
Are you using NO_PROMPT or NO_SCREEN option of HOST command ?
Had a look at Forms 4.5 manuals, there is no mentioning of (a)synchronously operation in connection with HOST command. -
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
PL/SQL Release 11.1.0.6.0 - Production
CORE 11.1.0.6.0 Production
TNS for HPUX: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production
Hi,
I am not familiar with Java setting in Oracle
I have a a stored procedure that call a java program to execute host command like
ssh user1@localhost '/home/user1/someprogram'
if we execute this directly using PuTTY
we can do the following with no problem
su - oracle
ssh user1@localhost '/home/user1/someprogram'
but when we execute the stored procedure,
we have to do
*/usr/bin/ssh* user1@localhost '/home/user1/someprogram'
does anybody where to set the path or environment to make the java know the path correctly.
thanks
Edited by: HGDBA on Mar 11, 2011 10:49 AMOK. You can try the following:
Login as the Unix user (e.g. oracle) on the pre-upgrade environment and run the following command.
echo $PATH
Repeat the above in the post-upgrade environment and compare the two outputs. Generally, PATH will be set using an environment script called from .profile of the user or it could be directly defined in .profile.
You can check this by examining the .profile after logging in as the OS user (e.g. oracle):
vi ~/.profile #i.e check the .profile in the user's home directory.
It's my best guess, as sometimes the PATH could be added later on (for. e.g by another script). Anyways, give it a try and tell what you find.
Maybe you are looking for
-
E72 connected to wifi but 'invalid server name' pr...
I had no problems connecting to the wifi at the mobile phone shop but have the 'invalid server name' problem at home. however, my husband who is using Nokia too has no problems using the home wifi on his phone. it shows that the phone is connected t
-
IPod Touch 5th Gen stuffing up!
Is anyone elses 5th Gen iPod Touch stuffing up or is it just mine? ):
-
Photo's out order when burned to disc
Hi, my photo's are in numbered order in the library but when burned to disc are all random, they have been edited from 3 cards and saved as a smart colllection, how can I get them to burn in order as library shows them !! thanks steve
-
How to get webservice result as "e4x" format in a professional solution?
Hi. I want to get data from a .Net webservice as "e4x" format. It works when i use <mx notation to define the service in flex. However i want to build a more professional solution and want to implement the webservice as a class. But I can't get it to
-
what's the most popular pdf library available in java , Is there any api which usses java 1.5 new features to generate PDF's ?