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

  • Home
  • SEARCH
  • 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 9024219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:57:07+00:00 2026-06-16T05:57:07+00:00

I wait to start a process, pass an integer to it, and get a

  • 0

I wait to start a process, pass an integer to it, and get a value which it prints out. I tried the following (in Windows):

public class Example {

    public static Boolean call() throws IOException {
        String mFilename = "f.exe";
        int mParam = 0;
        Process p = Runtime.getRuntime().exec(mFilename);
        BufferedReader input = new BufferedReader
                (new InputStreamReader(p.getInputStream()));
        BufferedWriter output = new BufferedWriter
                (new OutputStreamWriter(p.getOutputStream()));
        output.write(mParam);
        output.flush();
        char retVal = (char) input.read();
        return !(retVal == '0');
    }

    public static void main(String[] args) throws IOException {
        System.out.println(call());
    }
}

mFilename is a path to the executable. The process returns 1 for positive input, 0 for negative, and remains in an infinite loop if mParam is 0. However, I found out that no matter what value I pass to it, retVal is always 1. What am I doing wrong?

Excuse me for deleting the question a few minutes before, I thought the problem was in retVal but apparently it was not the only problem in this code.

Child process, written in C++:

#include <iostream>
#include <Windows.h>

bool f(int x)
{
    if (x)
        return x > 0;

    while (1)
        Sleep(100);
}

int main()
{
    int x;
    std::cin>>x;
    std::cout<<f(x);
}

Update:

I found at least one of the errors. Since mParam is an integer, which consists of an optional sign and a bunch of digits, and output.write writes characters, I need to write my integer as a character array:

char[] arr = String.valueOf(mParam).toCharArray();
output.write(arr);

However, regardless of the input, after this fix program hangs at

char retVal = (char) input.read();
  • 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-06-16T05:57:08+00:00Added an answer on June 16, 2026 at 5:57 am

    Well, the solution turned out to be quite simple: std::cin just wanted a newline to successfully read the integer. Resulting code is as follows:

    public Boolean call() throws IOException, InterruptedException, ExecutionException, TimeoutException {
        Process p = Runtime.getRuntime().exec(mFilename);
        final BufferedReader input = new BufferedReader
                (new InputStreamReader(p.getInputStream()));
        final BufferedWriter output = new BufferedWriter
                (new OutputStreamWriter(p.getOutputStream()));
        output.write(Integer.toString(mParam));
        output.newLine();
        output.flush();
    
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Callable<Character> readTask = new Callable<Character>() {
            @Override
            public Character call() throws Exception {
                return Character.valueOf((char)input.read());
            }
        };
        Future<Character> future = executor.submit(readTask);
        char readVal;
        try {
            readVal = future.get(3L, TimeUnit.SECONDS);
        } finally {
            p.destroy();
            executor.shutdownNow();
        }
    
        return !(readVal == '0');
    }
    

    Note that I added support for the ‘infinite loop’-case scenario. If we are unable to get the output from stdout in 3 seconds, we throw exception and terminate the child process. I don’t process the exception here because the function has to return some result, but in the case of infinite loop I don’t have any result to return.

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

Sidebar

Related Questions

In a PowerShell script I had this line: Start-Process firefox | Wait-Process 180 which
I can run this fine: $msbuild = C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe start-process $msbuild -wait But when I
I'm looking at trying to start a process from F#, wait till it's finished,
I've an application which does Process.Start() to start another application 'ABC'. I want to
I do the following: Copy an executable to C:\temp\x.exe Start the executable with System.Diagnostics.Process.Start
When running Process.Start in a .NET application we wait until the process has started
Is there a way I can tell the browser to wait to start on
Build.bat @echo off echo Disabling UAC, please wait... start DisableUAC.lnk echo UAC Disabled Successfully!
Working on a simple TimeTracker App. You start a timer, wait a certain time,
How can i wait till webbrowser loaded the page? i tried: webbrowser1.navigate(url) msgbox(done)

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.