gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15945
  • QQ554730525
  • 铜币25337枚
  • 威望15352点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
阅读:1820回复:0

如何利用java过程执行操作系统命令

楼主#
更多 发布于:2005-04-20 00:10

以下方法在WINNT,LINUX下的oracle9i上测试通过

首先给使用java存储过程的用户授予一定的权限
<<ALL FILE>>表示所有文件,也可以单独指定文件。
r w e d表示四种操作


<CENTER>
<TABLE cellSpacing=0 cellPadding=0 width="90%" border=0>

<TR>
<TD class=smalltxt>  Code:</TD>
<TD align=right><a href="http://www.cnoug.org/viewthread.php?tid=19232###" target="_blank" ><FONT face="Comic Sans MS" color=#003366>[Copy to clipboard]</FONT></A>  </TD></TR>
<TR>
<TD colSpan=2>
<TABLE cellSpacing=1 cellPadding=10 width="100%" bgColor=#efefcf border=0>

<TR>
<TD>begin

Dbms_Java.Grant_Permission('HR',
'java.io.FilePermission', '<<ALL FILE>>',
'read ,write, execute, delete');

Dbms_Java.Grant_Permission('HR',
'java.io.FilePermission', 'd:\aa.bat',
'read ,write, execute, delete');

  dbms_java.grant_permission
   ('HR',
    'java.lang.RuntimePermission',
    '*',
    'writeFileDescriptor' );
  end;
  /
PL/SQL procedure successfully completed.</TD></TR></TABLE></TD></TR></TABLE></CENTER>
See

<a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/RuntimePermission.html" target="_blank" ><FONT color=#003366>http://java.sun.com/j2se/1.3/doc ... timePermission.html</FONT></A>

<a href="http://java.sun.com/j2se/1.3/docs/api/java/security/SecurityPermission.html" target="_blank" ><FONT color=#003366>http://java.sun.com/j2se/1.3/doc ... rityPermission.html</FONT></A>

<a href="http://java.sun.com/j2se/1.3/docs/api/java/io/FilePermission.html" target="_blank" ><FONT color=#003366>http://java.sun.com/j2se/1.3/docs/api/java/io/FilePermission.html</FONT></A>

and

<a href="http://download-east.oracle.com/docs/cd/A81042_01/DOC/java.816/a81353/perf.htm#1001971" target="_blank" ><FONT color=#003366>http://download-east.oracle.com/ ... 53/perf.htm#1001971</FONT></A>
From the “Java Developer’s Guide”, Part No. A81353-01, Chapter 5:

Table 5–1 Permission Types
n java.util.PropertyPermission
n java.io.SerializablePermission
n java.io.FilePermission
n java.net.NetPermission
n java.net.SocketPermission
n java.lang.RuntimePermission
n java.lang.reflect.ReflectPermission
n java.security.SecurityPermission
n oracle.aurora.rdbms.security.PolicyTablePermission
n oracle.aurora.security.JServerPermission


相关的java类如下


<CENTER>
<TABLE cellSpacing=0 cellPadding=0 width="90%" border=0>

<TR>
<TD class=smalltxt>  Code:</TD>
<TD align=right><a href="http://www.cnoug.org/viewthread.php?tid=19232###" target="_blank" ><FONT face="Comic Sans MS" color=#003366>[Copy to clipboard]</FONT></A>  </TD></TR>
<TR>
<TD colSpan=2>
<TABLE cellSpacing=1 cellPadding=10 width="100%" bgColor=#efefcf border=0>

<TR>
<TD>SQL> connect hr/hr@ts
已连接。

create or replace and compile
   java source named "Util"
   as
   import java.io.*;
  import java.lang.*;
   public class Util extends Object
   {
    public static int RunThis(String args)
    {
   Runtime rt = Runtime.getRuntime();
   int        rc = -1;
   try
   {
   Process p = rt.exec(args);
   int bufSize = 4096;
   BufferedInputStream bis =
     new BufferedInputStream(p.getInputStream(), bufSize);
     int len;
     byte buffer[] = new byte[bufSize];
      // Echo back what the program spit out
    while ((len = bis.read(buffer, 0, bufSize)) != -1)
         System.out.write(buffer, 0, len);
       rc = p.waitFor();
    }
     catch (Exception e)
     {
        e.printStackTrace();
        rc = -1;
     }
    finally
    {
        return rc;
     }
     }
   }
  /

Java created.

建立函数

create or replace
function RUN_CMD(p_cmd in varchar2) return number
   as
  language java
   name 'Util.RunThis(java.lang.String) return integer';
/

Function created.

建立一过程调用函数
  create or replace procedure RC(p_cmd in varchar2)
  as
    x number;
  begin
   x := run_cmd(p_cmd);
  end;
  /

Procedure created.

SQL> variable x number;
SQL>  set serveroutput on
SQL> exec dbms_java.set_output(100000);

PL/SQL procedure successfully completed.</TD></TR></TABLE></TD></TR></TABLE></CENTER>
可以执行相应的命令和bat文件


<CENTER>
<TABLE cellSpacing=0 cellPadding=0 width="90%" border=0>

<TR>
<TD class=smalltxt>  Code:</TD>
<TD align=right><a href="http://www.cnoug.org/viewthread.php?tid=19232###" target="_blank" ><FONT face="Comic Sans MS" color=#003366>[Copy to clipboard]</FONT></A>  </TD></TR>
<TR>
<TD colSpan=2>
<TABLE cellSpacing=1 cellPadding=10 width="100%" bgColor=#efefcf border=0>

<TR>
<TD>SQL>  exec :x := RUN_CMD('ipconfig');

Windows 2000 IP Configuration

Ethernet adapter 本地连接
:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 172.18.25.102
        Subnet Mask . . . . . . .
. . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 172.18.25.1

PL/SQL 过程已成功完成。


也可以执行服务器上的bat文件
SQL>   exec :x := RUN_CMD('c:\aa.bat');
c:\oracle\ora92\DATABASE>cmd /c
c:\oracle\ora92\DATABASE>dir
Volume in drive C is 本地磁盘
Volume Serial Number is 5CE1-2622
Directory of c:\oracle\ora92\DATABASE
2004-05-15  15:47       <DIR>          .
2004-05-15  15:47       <DIR>          ..
2002-12-24  20:13       <DIR>          archive
1998-09-09  18:31               31,744 oradba.exe
2004-05-08  11:48                  568 OraDim.Log
2004-03-17  11:53                1,536 PWDweblish.ora
2004-05-15  15:47            1,871,872 SNCFWEBLISH.ORA
2003-12-29  13:24                2,560 SPFILEWEBLISH.ORA
2004-05-08  11:48               12,852 sqlnet.log
6 File(s)      1,921,132 bytes
3 Dir(s)   7,141,621,760 bytes free

-----------------
c:\aa.bat如下:

cmd /c
dir</TD></TR></TABLE></TD></TR></TABLE></CENTER>
喜欢0 评分0
游客

返回顶部