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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:08:10+00:00 2026-06-01T08:08:10+00:00

This is my code to start a process in Windows via java (and gobble

  • 0

This is my code to start a process in Windows via java (and gobble the output).

    public static void main(String[] args) throws Exception {
    String[] command = new String[3];
    command[0] = "cmd";
    command[1] = "/C";
    command[2] = "test.exe";
    final Process child = Runtime.getRuntime().exec(command);
    new StreamGobbler(child.getInputStream(), "out").start();
    new StreamGobbler(child.getErrorStream(), "err").start();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
            child.getOutputStream()));
    out.write("exit\r\n");
    out.flush();
    child.waitFor();
}

private static class StreamGobbler extends Thread {
    private final InputStream inputStream;
    private final String name;

    public StreamGobbler(InputStream inputStream, String name) {
        this.inputStream = inputStream;
        this.name = name;
    }

    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    inputStream));
            for (String s = in.readLine(); s != null; s = in.readLine()) {
                System.out.println(name + ": " + s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Somehow the program in question (process) is recieving an EOF right away (as in right after I step pas the “exec” line) and thus throwing an error ( detected, invalid) message immediately after runtime.exec is called. I can run this program manually via command prompt without this issue, but have confirmed that sending a ctrl-z on windows is what causes this message.

Anyone know what could be causing this?

If it matters, I have tried running the process directly as “test.exe” instead of cmd /c test.exe, but when I do that I can’t see the output via the inputStream. And when I do cmd test.exe without the /c, there is no difference.

  • 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-01T08:08:10+00:00Added an answer on June 1, 2026 at 8:08 am

    Your code looks like it should work (with one caveat, see below).

    I took your code verbatim and replaced test.ext with sort, which can read from piped stdin.

    If I run the code as-is, it starts the sort command, which waits for input. It hangs at child.waitFor() because you don’t close the output stream to indicate EOF. When I add the close() call, everything works correctly.

    I suggest you look at test.exe and determine if it is capable of reading from piped stdin, or is expecting console input.

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

Sidebar

Related Questions

I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
I'm launching the path C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools\IIS Manager.lnk via Process.Start , but it fails
I can run this fine: $msbuild = C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe start-process $msbuild -wait But when I
this code works fine until I start scrolling: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Hola check this code. var watch = new Stopwatch(); watch.Start(); var request = HttpWebRequest.Create(new
Suppose code like this: class Base: def start(self): pass def stop(self) pass class A(Base):
I have been seeing code like this usually in the start of header files:
I have a situation like this: start(); <Some code> end(); I want start() function
I wrote this code and compiled it, #include <windows.h> #include <stdio.h> #include <stdlib.h> #include
Requirements I want to publish a Java GUI application on Windows 7. This application

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.