...

Guides for PDF library

How to use Flyingbee PDF Conversion SDK.


Want to Try?

Try Flyingbee PDF Converter Online for Free, play with PDF to MS Office (.docx, .xlsx, .pptx) conversion on Web.

Launch Web Demo

Powered by Flyingbee PDF Conversion SDK

How to invoke a console program in JAVA?


james wei 2024-09-29 17:34:39

In Java, if you want to invoke a console program and output its parameters or execution results in real-time, you can use the ProcessBuilder class. This class allows you to create operating system processes. Here's an example of how to use ProcessBuilder to invoke a console program and read its output in real-time:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
​
public class ConsoleProgramInvoker {
​
    public static void main(String[] args) {
        // Path to the console program and its arguments
        String programPath = "path/to/your/console/program";
        String[] programArguments = {"arg1", "arg2", "arg3"};
​
        // Create a ProcessBuilder instance
        ProcessBuilder processBuilder = new ProcessBuilder(programPath, programArguments);
​
        try {
            // Start the process
            Process process = processBuilder.start();
​
            // Read the output from the process
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
​
            // Wait for the process to complete
            int exitCode = process.waitFor();
            System.out.println("Program exited with code: " + exitCode);
​
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create a ProcessBuilder instance with the path to the console program and its arguments. We then start the process using processBuilder.start(). To read the output from the process in real-time, we create a BufferedReader to read from the process's input stream. We read each line of output and print it to the console. Finally, we wait for the process to complete and print its exit code.



How to invoke a console program in .NET programming?
How you can invoke a console program in PHP?

Apple, the Apple logos, MacBook, iPad, iPhone, Apple Watch, and Apple Vision Pro are trademarks of Apple Inc., registered in the U.S. and other countries. App Store and Mac App Store is a service mark of Apple Inc., registered in the U.S. and other countries.
© 2014- 2024 Flyingbee Software, All Right Reserved.
United StatesUnited States

We uses cookies to give you the best experience, analyze traffic, and personalize content. By continuing using our Site, you agree to our use of cookies. The information collected might relate to you, your preferences, or your device, and is mostly used to make the site work as you expect it to and to provide a more personalized web experience. However, you can choose not to allow certain types of cookies, which may impact your experience of the site and the services we are able to offer. Read our Privacy Policy or manage your cookie preferences. If you would like to submit an opt-out request with respect to your non-cookie personal information (e.g., your email address), find our support email address to opt-out of sale/sharing/targeting with respect to non-cookie personal information.