0

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; } 
2
  • 1
    If you are serious about this, please consider how others will read your question; your question is poorly formatted and very hard to read for others. Usually, this results in low quality answers or no answers at all. If you want proper answers to your question, please try to write your questions accordingly :) Commented May 3, 2011 at 14:38
  • In what way is it not working? Are you not getting the results you expect or are you getting exceptions, etc? Commented May 3, 2011 at 14:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@krico: Yes. I'd actually thought the OP was doing that, but since the out.close() occurs before process.waitFor(), the OutputStream is not being properly consumed.
0

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.