Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem running sqlplus using Runtime.exec()

 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic