views:

144

answers:

1

I'm making a C Assessment Program through Java, which has a bunch of programming questions for C, and it lets the user input an answer in the form of C code, and then press a "Compile" button, which is linked to a bat file that runs the user input code through gcc.

I've got the input and compiling working, but I need to get the output from the compiler and get that to print textarea within the program. I can get a simple "Hello, world" compiling, but I'm having trouble getting programs that require a user input with scanf, for example, to be printed.

else if(e.getSource().equals(compile)){



 if(questionNumber<1){
    JOptionPane.showMessageDialog(programFrame, "Please start the assessment", "Compile Error", JOptionPane.ERROR_MESSAGE);
   }
   else{
    FileOutputStream fileWrite;
    try {
     fileWrite = new FileOutputStream("demo/demo.c");
     new PrintStream(fileWrite).println(input.getText());//saves what the user has entered in to a C source file
     fileWrite.close();
     @SuppressWarnings("unused")
     Process process = Runtime.getRuntime().exec("cmd /c compile.bat");//runs the batch file to compile the source file
     compileCode();
     try{
      fileStream = new FileInputStream("demo/output.txt");
      inputStream = new DataInputStream(fileStream);
      bufferRead = new BufferedReader(new InputStreamReader(inputStream));

      while((stringLine = bufferRead.readLine())!=null){
       compiled.append(stringLine);
       compiled.append("\n");
      }
      inputStream.close();


     }
     catch(IOException exc){
      System.err.println("Unable to read file");
      System.exit(-1);
     }


    } 
    catch (IOException exc) {
     JOptionPane.showMessageDialog(programFrame, "Demo file not found", "File Error", JOptionPane.ERROR_MESSAGE);

    }
   }

This is the actionPerformed method for the "Compile" button, the compileCode() is the JFrame that displays the output and "compiled" is the textArea for the output.

My batch file is:

C:
cd dev-cpp\bin
gcc.exe H:\workspace\QuestionProgram\demo\demo.c -o demo > H:\workspace\QuestionProgram\demo\compilestatus.txt
demo > H:\workspace\QuestionProgram\demo\output.txt

I'm not sure how I can do it, so the frame is created for the output of the code if the code requires a user input as the command prompt doesn't open without adding "START" to .exec(), but then the frame appears before the program has finished running.

Also, how would I get the output of the compiler if the compile fails because of an error? The way I've got it in my batch file at the moment doesn't put anything in a text file if it fails.

+1  A: 

The compiler probably writes its error messages to stderr instead of stdout. Since you aren't redirecting stderr you obviously see nothing in the file. You can redirect stderr by using 2> instead of > (which is an implied 1>).

If the program requires user input and shouldn't do so you can redirect NUL into the program call (essentially providing no input):

demo < nul > output.txt

Given that you obviously want some control over what gets executed I'd suggest you shouldn't use a batch file here. Instead you can launch the individual processes (the compiler and the program itself) through Java, capturing their respective output directly. Going through files here is actually unnecessary. You can use

gcc -x c -o demo -

to read the program directly from stdin.

Joey
Thanks, I'll have a play with them. The "demo < nul > output.txt" stops the program from hanging because of process.waitFor() as the compiler was waiting for user input with scanf, so that's the only problem I have with it at the moment.
JohnBore