Frage

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. :)

War es hilfreich?

Lösung

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");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top