我想从java内部执行一个外部.exe程序。.exe是一个CLI应用程序,它在运行时接受输入( scanf() ),并根据输入进行输出。我可以使用以下命令从java调用要执行的程序
Process p = Runtime.getRuntime().exec("cmd /c start a.exe");而不是
Process p = Runtime.getRuntime().exec("cmd /c start a.exe");但我认为在java中调用程序也是可能的。我有我的整个程序写在C++只需要一个图形用户界面,这是用java写的。notice:=有几件事
1)与.exe的通信应该是运行时的(不是通过主(Args)) 2) java程序应该获取输出并存储在某个变量/面板中以供将来使用3)要执行的程序可以不同(例如,用户可以选择根本不接受任何输入的.exe ) ........So基本上java GUI将充当RuntimeEnv
 public void runEXE() 
       {
            String s = null;
            try {
                Process p = Runtime.getRuntime().exec("cmd /c a.exe");
                System.exit(0);
            }
            catch (IOException e) {
                System.out.println("exception happened - here's what I know: ");
                e.printStackTrace();
                System.exit(-1);
            }
       }我知道有很多关于这个话题的问题。但是我找不到他们中的任何一个有用的。
发布于 2011-05-08 02:17:18
我使用的是一个相当丑陋的小函数。它接收要传递给Runtime.getRuntime().exec的命令,然后将结果保存到一个字符串中,并在最后返回该字符串。您可以选择是否只需要最后一行(或所有输出),以及是否要保存进程中的stdout或stderr字符串。
private static String systemResult(String cmd, boolean append, boolean useErr)
    {
    String result = "";
    try{
        // spawn the external process
        //printCmd(cmd);
        Process proc = Runtime.getRuntime().exec(cmd);
        LineNumberReader lnr1 = new LineNumberReader(new InputStreamReader(proc.getErrorStream()));
        LineNumberReader lnr2 = new LineNumberReader(new InputStreamReader(proc.getInputStream()));
        String line;
        int done = 0;
        while(lnr1 != null || lnr2 != null){
        try{
            if(lnr1.ready()){
            if((line = lnr1.readLine()) != null){
                //System.err.println("A:" +line);
                if(useErr){
                if(append) result = result + line + "\n";
                else result = line;
                }
            }
            }else if(done == 1){
            done = 2;
            }
        }catch(Exception e1){
            try{ lnr1.close(); }catch(Exception e2){}
            lnr1 = null;
        }
        try{
            if(lnr2.ready()){
            if((line = lnr2.readLine()) != null){
                //System.err.println("====>Result: " + line);
                if(!useErr){
                if(append) result = result + line + "\n";
                else result = line;
                }
            }
            }else if(done == 2){
            break;
            }
        }catch(Exception e1){
            try{ lnr2.close(); }catch(Exception e2){}
            lnr2 = null;
        }
        try{
            proc.exitValue();
            done = 1;
        }catch(IllegalThreadStateException itsa){}
        }
        if(lnr1 != null) lnr1.close();
        if(lnr2 != null) lnr2.close();
        try{
        proc.waitFor();
        }catch(Exception ioe){
        }finally{
        try{
            proc.getErrorStream().close();
            proc.getInputStream().close();
            proc.getOutputStream().close();
        }catch(Exception e){}
        proc = null;
        }
    }catch(Exception ioe){
    }
    return result;
    }https://stackoverflow.com/questions/5923011
复制相似问题