Skip to Main Content

Java and JavaScript in the Database

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!

Trouble Running Host Command

807467Oct 22 2010 — edited Nov 7 2010
Hello folks I am trying to create a procedure to run linux host commands. I have found and used code available throughout the internet and it compiles and runs, but no matter what command I send to the host I get a 'Not Found' exception, even if I send as the command the full path to the executable. Here is my code:

*1- Java Procedure*
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Host" AS
import java.io.*;
public class Host {
public static void executeCommand(String command) {
try {
String[] finalCommand;
if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
finalCommand = new String[4];
finalCommand[0] = "C:\\winnt\\system32\\cmd.exe";
finalCommand[1] = "/y";
finalCommand[2] = "/c";
finalCommand[3] = command;
} else { // Linux or Unix System
finalCommand = new String[3];
//finalCommand[0] = "/bin/sh";
//finalCommand[1] = "-c";
//finalCommand[2] = "'" + command + "'";
finalCommand[0] = command;
finalCommand[1] = "";
finalCommand[2] = "";

}

// Execute the command...
final Process pr = Runtime.getRuntime().exec(finalCommand);

// Capture output from STDOUT...
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("stdout: " + buff);
try {Thread.sleep(100); } catch(Exception e) {}
}
br_in.close();
} catch (IOException ioe) {
System.out.println("Error printing process output.");
ioe.printStackTrace();
} finally {
try {
br_in.close();
} catch (Exception ex) {}
}

// Capture output from STDERR...
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("stderr: " + buff);
try {Thread.sleep(100); } catch(Exception e) {}
}
br_err.close();
} catch (IOException ioe) {
System.out.println("Error printing execution errors.");
ioe.printStackTrace();
} finally {
try {
br_err.close();
} catch (Exception ex) {}
}
}
catch (Exception ex) {
System.out.println("Exception: " + ex.getLocalizedMessage());
ex.printStackTrace();
}
}

};
/

*2- PL/SQL Package*
CREATE OR REPLACE PACKAGE CIF_HOST_PKG IS

PROCEDURE host (p_command IN VARCHAR2);

END CIF_HOST_PKG;
/

PROCEDURE host (p_command IN VARCHAR2)
AS LANGUAGE JAVA
NAME 'Host.executeCommand (java.lang.String)';

END CIF_HOST_PKG;
/

*3- Java Permission*
EXEC Dbms_Java.Grant_Permission('CIF', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
EXEC Dbms_Java.Grant_Permission('CIF', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
exec dbms_java.grant_permission( 'CIF', 'SYS:java.io.FilePermission', '/bin/sh', 'execute' );
exec dbms_java.grant_permission( 'CIF', 'SYS:java.io.FilePermission', '/bin/ps', 'read , execute' );
exec dbms_java.grant_permission( 'CIF', 'SYS:java.io.FilePermission', '/bin/ls', 'read , execute' );

*4- Test Code*
set serveroutput on size 1000000
exec dbms_java.set_output(1000000)
exec CIF_HOST_PKG.host('/bin/ls -l');

*5- Test Output*
anonymous block completed
Exception: Exception during creation of the process: java.io.IOException: '/bin/ls -l' not found
java.io.IOException: Exception during creation of the process: java.io.IOException: '/bin/ls -l' not found
at java.lang.OracleProcess.start(OracleProcess.java:259)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:483)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:464)
at Host.executeCommand(Host:24)

Any idea on what am I doing wrong?

Thank you in advance,
Andre
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2010
Added on Oct 22 2010
1 comment
1,180 views