My Oracle Support Banner

How to Perform a System Call From a Java Stored Procedure and Capture the Output. (Doc ID 109095.1)

Last updated on DECEMBER 19, 2023

Applies to:

Oracle Database - Enterprise Edition - Version 10.1.0.2 and later
Oracle Database Cloud Schema Service - Version N/A and later
Oracle Database Exadata Cloud Machine - Version N/A and later
Oracle Cloud Infrastructure - Database Service - Version N/A and later
Oracle Database Cloud Exadata Service - Version N/A and later
Generic UNIX

Purpose

How to Perform a System Call From a Java Stored Procedure and capture the output.

Scope

The sample program in this article is provided for educational purposes only and is NOT supported by Oracle Support Services. It has been tested internally, however, and works as documented. We do not guarantee that it will work for you, so be sure to test it in your environment before relying on it.

As of RDBMS 10.2, it is recommended to use the package DBMS_SCHEDULER to execute any Operating System Commands.


This article illustrates how to call an OS command from a Java stored procedure (JSP). It will specifically answer the following 3 questions:
1. How do you create a JSP that executes a system command?
2. How do you verify that the OS command was executed?
3. How do you see the output from the command?

Often, running a system command from a Java stored procedure appears to return no error and the SQL*Plus message states that the procedure was successfully executed. As there are differences in each OS, executing a command from within Oracle may or may not work in any given OS. One case that is very OS dependent is with calling suid executables on UNIX. If you are familiar with UNIX, you might know that it is possible to assume an effective uid by setting the suid bit on an executable. If the owner of the executable is root, the effective uid will be root even if the executable is started as a non-root user. For security reasons, the LD_LIBRARY_PATH or SHLIBPATH or LIBPATH (depending on the unix version) are ignored.
If executables are not 'hard linked' but are using shared libraries, only the /usr/lib directory is used for shared libraries.

This is just one instance of why a command may not work. If after following this note and you are able to issue a command such as '/bin/ls /tmp' but others fail, involve your System Admin to determine the resolution.

This article illustrates how to create the JSP and how to verify that the OS command was executed. This article offers an overview for developers, including some common problems that are encountered. It contains the Java source and the setup instructions so that it is not necessary to know Java.

The exec() method in java.lang.Runtime is overloaded and can be called in different forms. This note illustrates the simplest form of exec() that takes a string as a parameter.


Details

To view full details, sign in with your My Oracle Support account.

Don't have a My Oracle Support account? Click to get started!


In this Document
Purpose
Scope
Details

My Oracle Support provides customers with access to over a million knowledge articles and a vibrant support community of peers and Oracle experts.