问题描述:
用Process proc = Runtime.getRuntime().exec(command);调用外部程序。调用了几个都是正常。
但是再调用其中一个时,java却被这个外部程序终止。有人遇到过类似情况嘛〉?是如何解决的?

解决方案 »

  1.   

    没有,被调用的程序正常结束的。被调用的程序是用c写的。然后就把java关掉了
      

  2.   

    请看我的blog,关于Runtime。exec的陷阱
    blog.csdn.net/westwin
      

  3.   

    楼上的,非常感谢你们提供的资料。可能我没有正确的描述我的问题。我再说明一下,被调程序c,正常结束后会导致java关闭。猜想可能是c关闭了java这个进程。既然java都已经被关闭了,那里面的catch又怎么能捕捉异常呢?For example,被调程序c运行结束后,就把电源power off了。那java程序怎么捕获异常,或者怎么继续执行下面的程序,明白了吧?
      

  4.   

    runtime.exec有总多应该注意的地方,包括要等待系统的回复,使用cmd等。
    以下有两个例子,一个是执行与系统无关的命令java,另一个是执行dos,windows的命令,希望对楼主有帮助。
      

  5.   

    执行dos,windows的命令
    import java.util.*;
    import java.io.*;public class MediocreExecJavac
    {
        public static void main(String args[])
        {
            try
            {            
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec("javac");
                InputStream stderr = proc.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                System.out.println("<ERROR>");
                while ( (line = br.readLine()) != null)
                    System.out.println(line);
                System.out.println("</ERROR>");
                int exitVal = proc.waitFor();
                System.out.println("Process exitValue: " + exitVal);
            } catch (Throwable t)
              {
                t.printStackTrace();
              }
        }
    }
      

  6.   

    更正:上个是执行dos,windows的命令
    ,以下的是与系统无关的命令javaimport java.util.*;
    import java.io.*;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)
                    System.out.println(type + ">" + line);    
                } catch (IOException ioe)
                  {
                    ioe.printStackTrace();  
                  }
        }
    }public class GoodWindowsExec
    {
        public static void main(String args[])
        {
            if (args.length < 1)
            {
                System.out.println("USAGE: java GoodWindowsExec <cmd>");
                System.exit(1);
            }
            
            try
            {            
                String osName = System.getProperty("os.name" );
                String[] cmd = new String[3];
    System.out.println( osName);
                if( osName.equals( "Windows 2000" ) )
                {
                    cmd[0] = "cmd.exe" ;
                    cmd[1] = "/C" ;
                    cmd[2] = args[0];
                    
                }
                else if( osName.equals( "Windows 95" ) )
                {
                    cmd[0] = "command.com" ;
                    cmd[1] = "/C" ;
                    cmd[2] = args[0];
                }
                
                Runtime rt = Runtime.getRuntime();
                System.out.println("Execing " + cmd[0] + " " + cmd[1] 
                                   + " " + cmd[2]);
                Process proc = rt.exec(cmd);
                // any error message?
                StreamGobbler errorGobbler = new 
                    StreamGobbler(proc.getErrorStream(), "ERROR");            
                
                // any output?
                StreamGobbler outputGobbler = new 
                    StreamGobbler(proc.getInputStream(), "OUTPUT");
                    
                // kick them off
                errorGobbler.start();
                outputGobbler.start();
                                        
                // any error???
                int exitVal = proc.waitFor();
                System.out.println("ExitValue: " + exitVal);        
            } catch (Throwable t)
              {
                t.printStackTrace();
              }
        }
    }