Solved

Runtime.getRuntime().exec(cmd) Problem

Posted on 2002-07-01
7
3,615 Views
Last Modified: 2013-11-18
/*
JDK1.3.1 / Oracle8.1.6 / Windows 2000

[1]. I want to do export of a user database, then create a new user in oracle and then import the exported database into this new user and finally run a .sql script on this new user database to update it. That is 4 STEPS. All these steps I want to perform from within my java program. I wrote following program to export the scott/tiger database into a file using "Runtime.getRuntime().exec(cmd);" but this program hangs at a point "starting...". What is wrong here?

[2]. Does oracle provide some internal packages for doing these operations in better way. If it does then what are those procedures that I could call from JDBC CallableStatement. I just don't want to rely on "Runtime.getRuntime().exec(cmd);"
*/

import java.io.*;

class Exec {  
    public static void main(String[] args) throws IOException {
        // Get the command
        String cmd;
        if (args.length == 0) {
            cmd = "exp scott/tiger buffer=4096 file=C:\\temp\\scott.dmp";
        }
        else {
            cmd = args[0];
        }
           
        // Execute command
        Runtime runtime = Runtime.getRuntime();
        Process pr = runtime.exec(cmd);

        // Print command output onto standard output
        InputStream is = pr.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String str;
        System.out.println("starting...");
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
        is.close();
        System.out.println("over...");

        // Wait for the process to complete.
        int status = -1;
        System.out.println("waiting starts");
        try {
            status = pr.waitFor();
        }
        catch (InterruptedException e) {
            System.out.println(e);
        }
        System.out.println("waiting over");
    }
}
0
Comment
Question by:javaq092999
7 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 50 total points
ID: 7123798
1.       System.out.println("starting...");
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }
Your code is blocked here in the while statement and you don't receive any information on the inputStream (your command does not write anything). Remember that you can obtain from a process 3 streams : 2 inputStreams ( the process outputstream which is an input stream for your application and the error stream) and a outputStream which is the outputstream for your application and inputstream for the proccess. In your current implementation you only can read from the inputstream (proccess out) which will block totally your application, until the external process quits.

Try to modify this example to fit your needs.
import java.io.*;

public class RuntimeTest {

  public static void main(String args[]) {
    try{
      Runtime rt = Runtime.getRuntime();

      final Process p = rt.exec("/usr/lib/jdk1.4.0/bin/javac Top.java");
        new Thread(new Runnable() {
       public void run() {
            try {
              BufferedReader br_in = new BufferedReader(new InputStreamReader(p.getInputStream()));
              String buff = null;
              while ((buff = br_in.readLine()) != null) {
             System.out.println("Process out :" + buff);
             try {Thread.sleep(100); } catch(Exception e) {}
           }
              br_in.close();
            } catch (IOException ioe) {
              System.out.println("Exception caught printing javac result");
              ioe.printStackTrace();
            }
       }
     }).start();

        new Thread(new Runnable() {
       public void run() {
            try {
              BufferedReader br_err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
              String buff = null;
              while ((buff = br_err.readLine()) != null) {
                   System.out.println("Process err :" + buff);
             try {Thread.sleep(100); } catch(Exception e) {}
           }
              br_err.close();
            } catch (IOException ioe) {
              System.out.println("Exception caught printing javac result");
              ioe.printStackTrace();
            }
       }
     }).start();
    } catch (Exception e) {
      System.out.println("Exception");
      e.printStackTrace();
    }
  }
}


2. I don't know, perhaps you should ask to oracle topic in this site.
0
 
LVL 3

Expert Comment

by:mjzalewski
ID: 7125556
The solution proposed has one small problem.

The main thread does not wait for completion of the command. Works well for running a single command. But javag wants to exp a database then imp to a new user. The imp must wait for the exp to finish.

This doesn't mean that the task to be executed will be interrupted. It just means that the method main() will complete before the Process object does.

I think you need to join the two worker threads you created to handle the Process object's output. By join I mean call the thread objects join() method from main().

Of course, the short solution is to put all four commands into a batch file (or shell script), then just execute the script.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7125926
mjzalewski : "... Try to modify this example to fit your needs. ..." - is the statement right above my code. Did you ever test my program? I believe not if I read again your comment. The program will exit only when the streams are closed, because the threads are blocked in waiting for input. Here is a modified version which prints messages when enters and exit's from the main method. After exit from main you will see the results of the command. But anyway, the proposed program is an exemplification of how to handle external commands (in this case it tries to compile a file called Top.java). I will post the Top.java source also, is a short one, filled with errors, so my threads have something to read. For windows systems there should be modified the path to the java compiler.

import java.io.*;

public class RuntimeTest {

  public static void main(String args[]) {
    System.out.println("Entering main method ....");
    try{
      Runtime rt = Runtime.getRuntime();

      final Process p = rt.exec("/usr/lib/jdk1.4.0/bin/javac Top.java");
        new Thread(new Runnable() {
       public void run() {
            try {
              BufferedReader br_in = new BufferedReader(new InputStreamReader(p.getInputStream()));
              String buff = null;
              while ((buff = br_in.readLine()) != null) {
             System.out.println("Process out :" + buff);
             try {Thread.sleep(100); } catch(Exception e) {}
           }
              br_in.close();
            } catch (IOException ioe) {
              System.out.println("Exception caught printing javac result");
              ioe.printStackTrace();
            }
       }
     }).start();
     
        new Thread(new Runnable() {
       public void run() {
            try {
              BufferedReader br_err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
              String buff = null;
              while ((buff = br_err.readLine()) != null) {
                   System.out.println("Process err :" + buff);
             try {Thread.sleep(100); } catch(Exception e) {}
           }
              br_err.close();
            } catch (IOException ioe) {
              System.out.println("Exception caught printing javac result");
              ioe.printStackTrace();
            }
       }
     }).start();
    } catch (Exception e) {
      System.out.println("Exception");
      e.printStackTrace();
    }
    System.out.println("Exiting main method .... Done!");
  }
}

//Top.java
class Top {
 void methodwitherrors(){
   System.out.prin();
 }
}    
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 92

Expert Comment

by:objects
ID: 7126442
Try simply changing:

InputStream is = pr.getInputStream();


to:

InputStream is = pr.getErrorStream();
0
 

Author Comment

by:javaq092999
ID: 7137607
Thanks Ovi and all
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7139705
me too -:)
0
 

Expert Comment

by:akirekadu
ID: 7622721

I observed that "WMIC" command hangs when invoked with Runtime.exec().  (WMIC is the WMI command line utility that comes with MS WMI toolkit)

Does anybody know what is special with WMIC?

Here's the code
--------------
Runtime runtime = Runtime.getRuntime();
String commands[] = new String[2];
commands[0] = "wmic";
commands[1]="/?";
Process proc = runtime.exec(commands);
ProcStreamReader stdoutReader =
     new ProcStreamReader(proc.getInputStream());
ProcStreamReader stderrReader =
     new ProcStreamReader(proc.getErrorStream());
stdoutReader.start();
stderrReader.start();
int returnCode = proc.waitFor();
System.out.println(stdoutReader.getData());
---------------------

public class ProcStreamReader extends Thread {    
     private InputStream in;
     private StringBuffer data;
     public ProcStreamReader(InputStream argIn) {
          in = argIn;
          data = new StringBuffer();
     }
     
     public void run() {
          try {
               BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));
               String line = null;
               
               while( (line = reader.readLine()) != null) {
                    data.append(line);
               }
          } catch(IOException ioe) {
               ioe.printStackTrace();
          }
         
     }
     public String getData() {
          return data.toString();
     }    

}
------------

Ashwath Akirekadu
PANACYA Inc
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Basic Java Case or If-Else statement... 3 50
servlet filter example 37 58
type mismatch (Object[] to double[] 4 23
hibernate example issues from command prompt 10 40
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to count occurrences of each item in an array.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question