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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:54:08+00:00 2026-05-26T04:54:08+00:00

I need a java method that will read command prompt output and store it

  • 0

I need a java method that will read command prompt output and store it into a String to be read into Java.

This is what I have so far but isn’t working right.

public void testGetOutput() {
    System.out.println("\n\n****This is the testGetOutput Method!****");
    String s = null;
    String query = "dir " + this.desktop;
    try {
        Runtime runtime = Runtime.getRuntime();
        InputStream input = runtime.exec("cmd /c " + query).getInputStream();
        BufferedInputStream buffer = new BufferedInputStream(input);
        BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
        String line = "";
        try {
            while ((line = commandResult.readLine()) != null) {
                s += line + "\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(s);
    } catch (Exception e) {
        e.printStackTrace();
    }
}//end testGetOutput()

I think the problem is when I try to change the query to be a command which will execute HandBrakeCLI.exe. Looking at my system when the program is running (but seems to have paused), it shows me that HandBrakeCLI.exe is running under a cmd window which is being run under my IDE. All that makes sense, but the HandBrakeCLI.exe doesn’t exit, so I’m guessing that’s why I can’t read the output as input to my program.

So, after that background. My big question is this: How do I get HandBrakeCLI.exe to close after it’s finished with my query so I can get its output?
Just for extra info, the only difference between the method above and the scan DVD method I have for HandBrakeCLI is the query variable is different. Like this example:

String query = "C:\Users\Kent\Desktop\HBCLI\HandBrakeCLI -t --scan -i "C:\Users\Kent\Desktop\General Conference DVDs\Sources\174th October 2004\DVD 1"; //this is actually a variable in the DVD object, but here's an example'

Oh, and by the way, when I run that query in a regular command prompt, it does exactly what I want it to, giving me all the output I desperately desire!

Here’s the original problem (I’m not sure how to resubmit a question):

I’ve been looking everywhere and can’t figure this out. I’m not sure what I’ve found is even relevant to what I want to do. I don’t have a whole lot of code for it yet, so it wont do much to put code here and I think this should be pretty simple, so I’m going to give some screenshots here. So here’s my task:

  1. Scan folder which is full of ripped DVD folders (Video_TS folders with VOB files etc.) and store these folder names as the title of the DVD.

  2. Scan each folder using the HandBrakeCLI and store the output to a string.

  3. Regex the string to identify each title, chapter, and language.

  4. Generate queries to give back to HandBrakeCLI to bulk encode each language in each chapter in each title for each DVD (you can see why I want to automate this!)

  5. Store these queries in a *.bat file

The only part I’m not sure about is step 2! I can do everything else pretty easily. I’ve read a lot about OutputStreams, but I just can’t seem to understand how it works. I really just need to get the output to a string which I can regex to get the stuff I need. Here are the screenshots of what I need to input and what I need to strip from the output:

Input to HandBrakeCLI:

enter image description here

Output to scan:

enter image description here

  • 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-26T04:54:08+00:00Added an answer on May 26, 2026 at 4:54 am

    First you need a non-blocking way to read from Standard.out and Standard.err

    private class ProcessResultReader extends Thread
    {
        final InputStream is;
        final String type;
        final StringBuilder sb;
    
        ProcessResultReader(@Nonnull final InputStream is, @Nonnull String type)
        {
            this.is = is;
            this.type = type;
            this.sb = new StringBuilder();
        }
    
        public void run()
        {
            try
            {
                final InputStreamReader isr = new InputStreamReader(is);
                final BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null)
                {
                    this.sb.append(line).append("\n");
                }
            }
            catch (final IOException ioe)
            {
                System.err.println(ioe.getMessage());
                throw new RuntimeException(ioe);
            }
        }
    
        @Override
        public String toString()
        {
            return this.sb.toString();
        }
    }
    

    Then you need to tie this class into the respective InputStream and OutputStreamobjects.

        try
        {
            final Process p = Runtime.getRuntime().exec(String.format("cmd /c %s", query));
            final ProcessResultReader stderr = new ProcessResultReader(p.getErrorStream(), "STDERR");
            final ProcessResultReader stdout = new ProcessResultReader(p.getInputStream(), "STDOUT");
            stderr.start();
            stdout.start();
            final int exitValue = p.waitFor();
            if (exitValue == 0)
            {
                System.out.print(stdout.toString());
            }
            else
            {
                System.err.print(stderr.toString());
            }
        }
        catch (final IOException e)
        {
            throw new RuntimeException(e);
        }
        catch (final InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    

    This is pretty much the boiler plate I use when I need to Runtime.exec() anything in Java.

    A more advanced way would be to use FutureTask and Callable or at least Runnable rather than directly extending Thread which isn’t the best practice.

    NOTE:

    The @Nonnull annotations are in the JSR305 library. If you are using Maven, and you are using Maven aren’t you, just add this dependency to your pom.xml.

    <dependency>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>jsr305</artifactId>
      <version>1.3.9</version>
    </dependency>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a Regex that will match a java method declaration. I have come
I need to make some reflective method calls in Java. Those calls will include
I have a C++ application and a Java application that need to log messages
In Java, I need to return an Iterator from my method. My data comes
I need a java script function that converts the document object of the current
I need the java code snippet for the below logic: Following is a string
I'm trying to write a method that will get a private field in a
I have a method that creates a MessageDigest (a hash) from a file, and
Im working on a java project where i need to read some objects from
I have a some java code that simulates bank transfers. The account class simply

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.