aspose file tools*
The moose likes Java in General and the fly likes problem running sqlplus using Runtime.exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem running sqlplus using Runtime.exec()" Watch "problem running sqlplus using Runtime.exec()" New topic
Author

problem running sqlplus using Runtime.exec()

An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
I have to run a sql command on sqlplus inside java. So i have this simple sql:

Now, I run this sql using Runtime.exec:


This hangs somewhere and doesnt output anything.. I even tried uncommenting the following line
but no use

When i check the process, i see that its still running:


Kindly help me. It seems the process is still active and hence no output comes. I have given exit in the sql file still no use

In short, I want to run a sqlplus sql script using Runtime.exec()
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Search for the article on Javaworld.com called "when Runtime.exec() won't". In short, the process' output buffer is full, and you need to read from proc.getInputStream(). The code is still trying to read from proc.getErrorStream() though.

You'll need to set up two threads to do the reading in parallel, adding to a StringBuilder object.

The Runnable code you could use:
You will have to call proc.waitFor() after starting these threads, to make sure that the threads read all contents. Afterwards, you can call getContents() to retrieve the contents and getException() to retrieve any IOException that occurred.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
I have read and tried whats given in javaworld. I wonder its not because of buffer overflow but because Runtime isnt able to recognize sqlplus command.
So as a workaround i created a shell script (.bat for windows) and then wrote the sqlplus command in the script file
then i ran the script file using Runtime.exec(). It ran fine finally
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem running sqlplus using Runtime.exec()