Is there a way to send a command to CP (like run an exe file)? In C++ you can use the system() function, but i think in java you use Runtime.

Thanx in advanced!

Recommended Answers

All 2 Replies

Process p = Runtime.getRuntime().exec(cmdline);

You can get an inputstream from that and read it if that's what your heading towards.

hi c++

you can try this:

try {

                Process process=Runtime.getRuntime().exec("<your exe>");
                InputStream inputStream = process.getInputStream();
                InputStream errStream = process.getErrorStream();

                ConsoleErrReader err = new ConsoleErrReader(errStream);
                ConsoleInputReader input = new ConsoleInputReader(inputStream);

                Thread th = new Thread(err);
                Thread th1 = new Thread(input);
                th.start();
                th1.start();
}
catch(Exception e)
{
                e.printStackTrace();
}


class ConsoleInputReader implements Runnable
{
        InputStream in;
        ConsoleInputReader(InputStream in)
        {
            this.in=in;
        }
        public void run() {
            try
            {
                BufferedReader bufferreader = new BufferedReader(new
                        InputStreamReader(in));
                String line = bufferreader.readLine();
                while(line!=null)
                {
                    System.out.println(line);
                    line=bufferreader.readLine();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
}
class ConsoleErrReader implements Runnable
    {
        InputStream in;
        ConsoleErrReader(InputStream in)
        {
            this.in=in;
        }

        public void run() {
            try {
                System.out.println("err reader run");
                BufferedReader bufferreader = new BufferedReader(new
                        InputStreamReader(in));
                String line = bufferreader.readLine();
                while (line != null) {
                    System.out.println(line);
                    line = bufferreader.readLine();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.