0

I have a C++ program which uses command line as its mean for IO. I don't know C++, nor do I have the program's source code. I want my java application to open the C++ program , give some input and gather the result from the C++ code. Is there a way?

UPDATE: I need to enter the input at runtime.

dark32
  • 259
  • 3
  • 14
  • 1
    Opening the program should not be the problem. Providing input depends if the the input is read from run arguments or from stdin. Reading output depends if it is written to file or on stdout. For concrete solution, pleas provide more details. – Krever Jan 29 '15 at 06:17
  • 2
    You are looking for `ProcessBuilder` and related classes. – user253751 Jan 29 '15 at 06:19
  • Will giving a link to the program's download work @Krever ? – dark32 Jan 29 '15 at 06:19

1 Answers1

1

You can use java.lang.Runtime

For example:

 public class TestRuntime {
        public static void main(String[] args) {
            try {
                Process p = Runtime.getRuntime().exec("test.bat");
                // test.bat or test.sh in linux is script with command to run (c++) program 
                //  or direct path to application's exec
                BufferedReader in = new BufferedReader(
                                    new InputStreamReader(p.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

In addition, you can read about difference between Runtime and ProcessBuilder in this topic.

Community
  • 1
  • 1
Vitaly
  • 2,552
  • 2
  • 18
  • 21