jaiswal
jaiswal

Reputation: 1

Issues with running Runtime.getRuntime().exec

I'm using process = Runtime.getRuntime().exec(cmd,null,new File(path)); to execute some SQL in file (abz.sql)

Command is:

"sqlplus "+ context.getDatabaseUser()     +   "/"
          + context.getDatabasePassword() +   "@"
          + context.getDatabaseHost()     +   ":"
          + context.getDatabasePort()     +   "/"
          + context.getSid()              +   " @"
          + "\""
          + script  + "\"";

 String path=context.getReleasePath()+ "/Server/DB Scripts";

It is executing that file but not getting exit. Hence I tried using:

Writer out = new OutputStreamWriter(process.getOutputStream());
out.append("commit;\r\n");
out.append("exit \r\n");
System.out.println("---------"+out);
out.close();

This it complete block that I m using:

if(context.getConnectionField()=="ORACLE")
{

     String cmd=
    "sqlplus "+ context.getDatabaseUser()     + "/" 
              + context.getDatabasePassword() + "@"
              + context.getDatabaseHost()     + ":"
              + context.getDatabasePort()     + "/"
              + context.getSid()              + " @" 
              + "\""
              + script  +"\"";

   String path=context.getReleasePath()+ "/Server/DB Scripts";
   process = Runtime.getRuntime().exec(cmd,null,new File(path));
   out = new OutputStreamWriter(process.getOutputStream());
   out.append("commit;\r\n");
   out.append("exit \r\n");
   System.out.println("---------"+out);
   out.close();    

        Integer result1 = null;
    while (result1 == null) {
        try {
            result1 = process.waitFor();
    } 
            catch (InterruptedException e) {}
    }

    if(process.exitValue() != 0)
             return false;
       return true;
}

Upvotes: 0

Views: 1761

Answers (2)

sfkleach
sfkleach

Reputation: 807

I can see a few issues here. The version of 'exec' that you are using will tokenize the command string using StringTokenizer, so unusual characters in the password (like spaces) or the other parameters being substituted are accidents waiting to happen. I recommend switching to the version

Process exec(String[] cmdarray, String[] envp, File dir) throws IOException

It is a bit more work to use but much more robust.

The second issue that there are all kinds of caveat about whether or not exec will run concurrently with the Java process (see http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html). So you need to say which operating system you're on. If it does not run concurrently then your strategy of writing to the output stream cannot work!

The last bit of the program is written rather obscurely. I suggest ...

for (;;) {
    try {
        process.waitFor();
        return process.exitValue() == 0;
    } catch ( InterruptedException _ ) {
        System.out.println( "INTERRUPTED!" ); // Debug only.
    }
}

This eliminates the superfluous variable result1, eliminates the superfluous boxing and highlights a possible cause of endless looping.

Hope this helps & good luck!

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168845

The code shown fails to read the error stream of the Process. That might be blocking progress. ProcessBuilder was introduced in Java 1.5 and has a handy method to redirectErrorStream() - so that it is only necessary to consume a single stream.

For more general tips, read & implement all the recommendations of When Runtime.exec() won't.

Upvotes: 1

Related Questions