Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 799223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:05:35+00:00 2026-05-14T23:05:35+00:00

I’m making a C Assessment Program through Java, which has a bunch of programming

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:05:35+00:00Added an answer on May 14, 2026 at 11:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 463k
  • Answers 463k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's an example of placing an image that responds to… May 16, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Usually, a blank table is cause by returning an incorrect… May 16, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer There is a lot of information to digest, but the… May 16, 2026 at 12:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.