VishalDevgire
VishalDevgire

Reputation: 4278

how to get stdin and stdout of a running c program

i want to code a manager program in java which will execute a c program and can read it's input stream and write to output stream.

the client will request c exe then server will handle it's request like following :

  1. the manager program will execute.
  2. it'll execute a c program.
  3. can send c program's output to browser.(input stream).
  4. should be able to accept input from browser and supply to c program.

i have tried executing c code using Runtime and Process. but it gives exception

---------------------------
16 bit MS-DOS Subsystem
---------------------------
Error while setting up environment for the application. Choose 'Close' to terminate the application.
---------------------------
Close   
---------------------------

help?

code : import java.io.*;

class Dev
{
public static void main(String args[])
{
    try
    {
        Process p = Runtime.getRuntime().exec("dev.exe");
        InputStream is = p.getInputStream();
        // from her i'll do the stuff but it gives error.

    }
    catch(Exception e)
    {
        System.out.print("\n\n\t Error : "+e);
    }
}
}

Upvotes: 0

Views: 763

Answers (1)

user377628
user377628

Reputation:

You can execute a program by creating a class that implements Runnable, then passing it a Process, which is described in this answer. The answer there also describes how to get the process's output in an InputStream. As for passing in some input, this can be done by piping a string in the command line. On the Process-making line from that answer, you can add:

//...
String someInput, command; // Set these to the appropriate values.
Process p = Runtime.getRuntime().exec("echo \"" + someInput + "\"|" + command);
//...

This will pipe the value of someInput to command. This is based on the command line and is technically platform specific, but should work without changes on Linux, OS X, and Windows.

Upvotes: 1

Related Questions