使用java来制作几个cmd命令

时间:2013-11-12 04:17:14

标签: java cmd runtime rman

您好我正在尝试使用来自java的rman进行备份,我尝试使用java中的Runtime,我能够打开cmd,访问rman但是之后我尝试提交的任何其他查询都被执行了。 我能够从cmd窗口和sqlplus窗口访问rman 我alredy尝试使用;没有它 这是代码,请帮帮我:

public void execute(JobExecutionContext context) throws JobExecutionException {
         command[0] = "cmd";
         command[1] = "/c";
         command[2] = "rman target / catalog rman/rman@xe;";
         command[3] = "backup as backupset database plus archivelog;";
         command[4] = "exit;";
         command[5] = "sqlplus.exe;";
         try {  
         Process p = Runtime.getRuntime().exec(command);

         BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
         String line = reader.readLine();
         while (line != null) {
             System.out.println(line);
             line = reader.readLine();
         }

这是输出:

Recovery Manager: Release 11.2.0.2.0 - Production on Lun Nov 11 22:11:12 2013

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

connected to target database: XE (DBID=2711152663)
connected to recovery catalog database

2 个答案:

答案 0 :(得分:0)

请参阅Problem with the output of a cmd command in java ...具体而言,如果您要执行多个命令cmd,则必须使用&符号将它们连接在一起。

答案 1 :(得分:0)

如果要像在“交互式”会话中一样运行命令,则需要生成“cmd.exe”,然后使用p.getOutputStream()“编写”命令来执行。

类似的东西:

ProcessBuilder pb = new ProcessBuilder("cmd.exe");
pb.redirectErrorStream(true);
Process p = pb.start();
PrintWriter writer = new PrintWriter(p.getOutputStream());
writer.println("rman target / catalog rman/rman@xe;");
writer.println("backup as backupset database plus archivelog;");
writer.println("exit;");
writer.println("sqlplus.exe;");

注意:如果您没有阅读可能会阻止的p.getInputStream()命令。