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 6221947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:13:33+00:00 2026-05-24T08:13:33+00:00

I know there are similar threads, and I have read them all. However none

  • 0

I know there are similar threads, and I have read them all. However none of them have been of any help.

I have this simple batch file:

@echo off
set /p UserInput=Enter a number: 
echo Number was %UserInput%

I want to run this batch file from java, send the number to it and get the input.

I have a strange problem. I started the batch file using cmd /c, opened the input and output streams, but it still won’t work. When I run a program, for example “cmd.exe”, it returns the actual output that you get when you open a CMD window:

Microsoft Windows… Copyright (c) 2009 Microsoft
Corporation. All rights reserved.

So at least the code is partially working. However it simply does not give me ANY output whatsoever when I use "cmd /c C:\\test.bat" (where test.bat is a valid batch file).

This is the java code. What’s wrong with it?

      import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;

    public class Test {

        private static BufferedReader bufIn;
        private static BufferedWriter printOut;
        private static Process p;

        public static void main(String[] args) throws IOException {

            Runtime rt = Runtime.getRuntime() ;

            p = rt.exec("cmd.exe /c C:\\test.bat");
            InputStream in = p.getInputStream() ;
            OutputStream out = p.getOutputStream ();

            bufIn = new BufferedReader(new InputStreamReader(in));
            printOut = new BufferedWriter(new OutputStreamWriter(out));

            int ch = 0;
            ch = bufIn.read();

            while (ch != 0) 
            {
                System.out.print((char) ch);
                ch = bufIn.read();
            }

            //send a command to 

            printOut.write("209");
            printOut.flush();

            while (ch != 0) 
            {
                System.out.print((char) ch);
                ch = bufIn.read();
            }



    //p.destroy() ;

    }


}

I should be getting:

Please enter a number:
Number was 209

Edit: I edited the code because it was apparently getting stuck at the readLine while there was no line 🙂

Anyway, I still have a problem.
I’m getting:

Enter a number: 

and nothing else, almost as if the output stream is not working at all.

  • 1 1 Answer
  • 1 View
  • 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-24T08:13:34+00:00Added an answer on May 24, 2026 at 8:13 am

    Instead of using a PrintWriter, try an OutputStreamWriter wrapped in a BufferedWriter:

    printOut = new BufferedWriter(new OutputStreamWriter(out));
    

    Obviously you’ll need to change printOut to a BufferedWriter as well.

    It also looks like you’re flushing and printing in the wrong order to me, I’d do:

    printOut.println("209");
    printOut.flush();
    

    As a side point, you can use readLine() on BufferedReader rather than just read()!

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

Sidebar

Related Questions

I know there have been some similar questions to this, but they haven't helped
I know there are many questions similar but any of them didn't help. in
I know there a similar threads around, but this is really the first time
I've seen a couple similar threads to this question, but none of them really
First of, I know there are similar questions already on stackoverflow ( this ,
I know there are questions similar to this one, but I've not found a
I know there a lot of similar questions to this, but I didn't find
I know there are a lot of similar questions to this one around Stack
First of all, I know there are a few quite similar questions here on
Before voting to close, please read, I know that there are similar questions (:

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.