java 调用sqlplus 执行sql脚本我开启了两个线程可是在这里还是被挂起exitVal = proc.waitFor(); 不知道是为什么!请高手指点! 代码: cmd= "cmd.exe /c sqlplus system/system@orcl @d:\\sql\\database.sql"Process proc = rt.exec(cmd); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(),"Error"); 
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),"Output");  
                      
errorGobbler.start(); 
outputGobbler.start(); System.out.println("----------------"); exitVal = proc.waitFor(); System.out.println(exitVal+"到这里你就成功了!"); 执行到这里还被挂起:proc.waitFor();
这是那个线程类
class StreamGobbler extends Thread {

 InputStream is;
 String type;  StreamGobbler(InputStream is, String type) {
  this.is = is;
  this.type = type;
 }  public void run() {
  try {
   InputStreamReader isr = new InputStreamReader(is);
   BufferedReader br = new BufferedReader(isr);
   String line = null;
   while ((line = br.readLine()) != null) {
    if (type.equals("Error"))
    {
     System.out.println("错误:"+line);
    }
    else
    {
     System.out.println("-->:"+line);
    }
   }
   br.close();
   isr.close();
  } catch (IOException ioe) {
   ioe.printStackTrace();
  }
 }
}

解决方案 »

  1.   

    执行到这里还被挂起你当前线程正在等待proc结束
      

  2.   

    java2000_net谢谢你的关注请问有什么办法可以解决吗?我在网上找了很多这方面的,可是
    没有一个是可用的我现在用了个exitVal = proc.exitValue()
    的方法,但会报异常,所以想请教一下有什么办法可以不让proc.waitFor(); 挂起呢?
      

  3.   

    waitFor方法就是等待你调用的进程退出就会退出,并返回程序的返回值。
    官方说明如下:
    causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits. 不过,我注意到,你只是抓取了error和input,为什么不抓standard out呢?如果不抓你启动的进程的输出,程序的输出缓冲会被填满导致子进程挂起,waitFor永远无法返回。
    Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. 
      

  4.   

    用法明显错误。。
    还sqlplus呢!怎么不抓toad呢!