Communication with a program in C ++ from java

I want to execute an external .exe program from inside Java. .Exe is a CLI application that receives data at runtime (scanf ()) and displays it depending on the input. I can call a program to execute from Java using

Process p = Runtime.getRuntime().exec("cmd /c start a.exe");

instead

Process p = Runtime.getRuntime().exec("cmd /c start a.exe");

But I think that you can also call the program from within Java. I have the whole program written in C ++, I just need a graphical interface written in Java. There are several things worth paying attention to: =

1) Communication with .exe should be performed at runtime (not via main (args)). 2) The Java program should receive the output and save it in some variable / panel for future use. 3) The program that should be executed may be different (for example, the user can choose .exe, which does not accept any data at all) ........ Thus, basically the Java GUI will act as 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 what I know: ");
                e.printStackTrace();
                System.exit(-1);
            }

       }

I know that there are many questions on this topic. But I can not find any of them very useful.

+4
source share
7 answers

, . , Runtime.getRuntime().exec, String . , ( ) 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;
    }
+3

JNI IPC ++.

Update:

JNI - Java , JRE. DLL, JRE, Java-. JNI DLL , Java-, JNI DLL, ++ .

CreateNamedPipe Win32 API. JNI DLL , , , ++ . , , .

, . , JNA ++ stdin.

+1

JNI, @linuxuser27, SWIG, Java → ++ .

+1

Google Java/++.

- Google , , - , XML, , . , , , - Java, ++ Python.

+1

:

  • , , : Runtime.exec
  • , , Java , , , - . ?
  • JNI JNA, , ++ CLI, , .NET dll, Windows dll. ? , Java ++.
0
  • .exe. . .

  • : ,

0

All Articles