zizhang602486qq
zizhang602486qq

Reputation: 41

How to throw error if Runtime.getRuntime().exec fail

i try following:

package ro.gd.ex;

/**
 * Created by roroco on 11/17/14.
 */
public class Ex {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("echo -n $(date)|xclip --selection clipboard");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

if i run this cmd in terminal, it will raise

roroco@roroco ~/Dropbox/jvs/ro-gd $ echo -n $(date)|xclip --selection clipboard
xclip: --selection: No such file or directory
roroco@roroco ~/Dropbox/jvs/ro-gd $ echo $?
1

but in java, it raise nothing, my question is how to make this cmd throw error. i found it's exit status is 1.

my os:

roroco@roroco ~/Dropbox/jvs/ro-gd $ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17 Qiana
Release:    17
Codename:   qiana

Upvotes: 4

Views: 5964

Answers (2)

Juan carlos Sanchez
Juan carlos Sanchez

Reputation: 21

How to throw error if Runtime.getRuntime().exec fail

process.waitFor();, exitCode is 0, why? When: 0=Exit normal. 1=Exit with Error.

the exit value of the subprocess represented by this Process object. By convention, the value 0 indicates normal termination and 1 bad termination..

some code be like this:

Process p=Runtime.getRuntime().exec("Seguridad.bat");

        int exitCode = p.waitFor();
        if (exitCode != 0) {
            JOptionPane.showConfirmDialog(null,"Error Execute when exitCode=1");
        }else{
            JOptionPane.showConfirmDialog(null,"fine Execute when exitCode=0");

        }

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347184

process.waitFor() which will return the error level of the command, then simply create a new Exception (probably an IOException)...

public class Ex {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("echo -n $(date)|xclip --selection clipboard");
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new IOException("Command exited with " + exitCode);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

You might also consider using ProcessBuilder over Runtime.getRuntime().exec

public static void main(String[] args) {
    try {
        ProcessBuilder pb = new ProcessBuilder(
                        "echo",
                        "-n",
                        "$(date)|xclip",
                        "--selection",
                        "clipboard"
        );
        pb.redirectError();
        pb.inheritIO();
        Process process = pb.start();
        // Really should be reading the Process's InputStream
        int exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new IOException("Command exited with " + exitCode);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Upvotes: 2

Related Questions