0

I need my program to execute a program with command line args and then return the output the program gives.

I've gotten this far, but I haven't quite figured out how to get the command line args in.

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            appendLog("### Command # reboot ###");
            process = new ProcessBuilder(adb.toString() + "reboot");
            process.redirectErrorStream(true);
            Process pr = process.start();

            InputStream stream = pr.getInputStream();
            InputStreamReader streamReader = new InputStreamReader(stream);
            BufferedReader reader = new BufferedReader(streamReader);

            String line;
            int exit = -1;

            while ((line = reader.readLine()) != null) {
                appendLog("### ADB output # Command: reboot ###\n" + line);
                exit = pr.exitValue();
                if (exit == 0) {
                    appendLog("### Process finished # Command: reboot ###\n");
                }
            }
        } catch (Exception ex) {
            appendLog("### ERROR:\n" + ex + " ###");
            appendLog("### Process finished # Command: reboot ###\n");
        }
    }

Any help with this matter is much appreciated. :)

1 Answer 1

1

ProcessBuilder takes as parameter varargs (undefined number of arguments) where the first argument is the command to be executed (adb, in your case) and followed by arguments. So, something like, where adb.toString() is the full path to adb:

process = new ProcessBuilder(adb.toString(), "reboot");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.