Example usage for java.lang Process getErrorStream

List of usage examples for java.lang Process getErrorStream

Introduction

In this page you can find the example usage for java.lang Process getErrorStream.

Prototype

public abstract InputStream getErrorStream();

Source Link

Document

Returns the input stream connected to the error output of the process.

Usage

From source file:com.liferay.blade.tests.BladeCLI.java

public static String execute(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);//from  w w  w. ja v  a2 s . c o  m

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    String errors = new String(IO.read(errorStream));

    assertTrue(errors, errors == null || errors.isEmpty());

    output = StringUtil.toLowerCase(output);

    return output;
}

From source file:com.liferay.blade.tests.BladeCLI.java

public static String startServerWindows(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("start");
    command.add("/b");
    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);/*from w w  w .  java 2 s.  co  m*/

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    String errors = new String(IO.read(errorStream));

    assertTrue(errors, errors == null || errors.isEmpty());

    return output;
}

From source file:dk.netarkivet.common.utils.ProcessUtils.java

/** Runs an external process that takes no input, discarding its output.
 *
 * @param environment An environment to run the process in (may be null)
 * @param programAndArgs The program and its arguments.
 * @return The return code of the process.
 *///  w w  w .  j a  va  2s. c o m
public static int runProcess(String[] environment, String... programAndArgs) {
    try {
        log.debug("Running external program: " + StringUtils.conjoin(" ", programAndArgs) + " with environment "
                + StringUtils.conjoin(" ", environment));

        Process p = Runtime.getRuntime().exec(programAndArgs, environment);
        discardProcessOutput(p.getInputStream());
        discardProcessOutput(p.getErrorStream());
        while (true) {
            try {
                return p.waitFor();
            } catch (InterruptedException e) {
                // Ignoring interruptions, we just want to try waiting
                // again.
            }
        }
    } catch (IOException e) {
        throw new IOFailure("Failure while running " + Arrays.toString(programAndArgs), e);
    }
}

From source file:com.cprassoc.solr.auth.SolrAuthActionController.java

public static String doPushConfigToSolrAction(SecurityJson json) {
    String result = "";
    try {/*from  ww  w . j  a v a 2 s .c  o m*/
        String mime = "sh";
        if (Utils.isWindows()) {
            mime = "bat";
        }
        String pathToScript = System.getProperty("user.dir") + File.separator + "solrAuth." + mime;
        String jsonstr = json.export();
        String filePath = SolrAuthManager.getProperties().getProperty("solr.install.path") + File.separator
                + "security.json";
        File backup = new File("backup");
        if (!backup.exists()) {
            backup.mkdirs();
        }

        File file = new File(filePath);
        if (file.exists()) {
            // String newFilePath = SolrAuthManager.getProperties().getProperty("solr.install.path") + File.separator + System.currentTimeMillis() + "_security.json";
            // File newFile = new File(newFilePath);
            // file.renameTo(newFile);
            FileUtils.copyFileToDirectory(file, backup);
        }
        FileUtils.writeByteArrayToFile(file, jsonstr.getBytes());
        Thread.sleep(500);
        ProcessBuilder pb = new ProcessBuilder(pathToScript);

        Log.log("Run PUSH command");

        Process process = pb.start();
        if (process.waitFor() == 0) {
            result = "";
        } else {
            result = Utils.streamToString(process.getErrorStream());
            result += "\n" + Utils.streamToString(process.getInputStream());
            Log.log(result);
        }

    } catch (Exception e) {
        e.printStackTrace();
        result = e.getLocalizedMessage();
    }

    return result;
}

From source file:com.ltb.Main.java

public static void displayIt(File node) throws IOException, InterruptedException {
    String filePath = node.getAbsoluteFile().getAbsolutePath();
    System.out.println(filePath);

    if (node.isDirectory()) {
        String[] subNote = node.list();
        for (String filename : subNote) {
            displayIt(new File(node, filename));
        }/*  w ww .  ja v a2s.  com*/
    } else {

        //Process pr = Runtime.getRuntime().exec("avconv -i //home//juanma//Videos//License.avi"); // Working fine
        //Process pr = Runtime.getRuntime().exec(outLine); // Working fine without spaces
        String[] args1 = { "avconv", "-i", filePath };
        Process pr = Runtime.getRuntime().exec(args1);
        //filePath = filePath.replace("/" , "//");
        //filePath = filePath.replace(" " , "\\ ");
        //String commandline ="avconv -i \""+filePath+"\"";
        //String commandline ="avconv -i "+filePath;
        //Process pr = Runtime.getRuntime().exec(commandline);

        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
        //BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line;
        // Start FilmFactory
        String extension = FilenameUtils.getExtension(node.getName());
        if (isFilmExtension(extension) == false) {
            return;
        } else {

            Film film = new Film(node.getName(), filePath);
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                if (line.contains("Invalid data")) {
                    // TODO Most probably no film - Change method below
                    film.isNotFilm();
                } else if (line.contains("Audio")) {
                    film.addAudioTrack(line);
                } else if (line.contains("No such file")) {
                    System.out.println("Error?");
                }

            }
            pr.waitFor();
            in.close();
        }
    }

}

From source file:energy.usef.environment.tool.security.VaultService.java

/**
 * Executes a class's static main method with the current java executable and classpath in a separate process.
 * //from   ww w.j  av  a 2 s.  c o m
 * @param klass the class to call the static main method for
 * @param params the parameters to provide
 * @return the exit code of the process
 * @throws IOException
 * @throws InterruptedException
 */
public static int exec(@SuppressWarnings("rawtypes") Class klass, List<String> params)
        throws IOException, InterruptedException {
    String javaHome = System.getProperty("java.home");
    String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
    String classpath = System.getProperty("java.class.path");
    String className = klass.getCanonicalName();

    // construct the command line
    List<String> command = new ArrayList<String>();
    command.add(javaBin);
    command.add("-cp");
    command.add(classpath);
    command.add(className);
    command.addAll(params);
    LOGGER.debug("executing class '{}' with params '{}' in classpath '{}' with java binary '{}'", className,
            params.toString(), classpath, javaBin);

    // build and start the Vault's process
    ProcessBuilder builder = new ProcessBuilder(command);
    Process process = builder.start();
    process.waitFor();

    // get the input and error streams of the process and log them
    InputStream in = process.getInputStream();
    InputStream en = process.getErrorStream();
    InputStreamReader is = new InputStreamReader(in);
    InputStreamReader es = new InputStreamReader(en);
    BufferedReader br = new BufferedReader(is);
    BufferedReader be = new BufferedReader(es);

    String read = br.readLine();
    while (read != null) {
        LOGGER.debug(read);
        read = br.readLine();
    }
    read = be.readLine();
    while (read != null) {
        LOGGER.debug(read);
        read = be.readLine();
    }

    br.close();
    is.close();
    in.close();

    return process.exitValue();
}

From source file:edu.mit.genecircuits.GcUtils.java

static public void exec(String command) {

    try {/*from   w  w w  .  ja  va 2  s .  c om*/

        // Execute command
        System.out.println(command);
        Process p;
        p = Runtime.getRuntime().exec(command);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        // read the output from the command
        String s;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }

    } catch (IOException e) {
        GcMain.error(e);
    }

}

From source file:com.microsoftopentechnologies.intellij.helpers.AndroidStudioHelper.java

private static void exec(String[] cmd, String tmpdir) throws IOException, InterruptedException {
    String[] env = new String[] { "PRECOMPILE_STREAMLINE_FILES=1" };

    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(cmd, env, new File(tmpdir));

    // any error message?
    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), true);

    // kick them off
    errorGobbler.start();//from   ww  w.  j ava2 s.c  o  m

    proc.waitFor();
}

From source file:com.liferay.blade.samples.integration.test.utils.BladeCLIUtil.java

public static String execute(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);/*  w w w  . j  a  va2s .c om*/

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    List<String> errorList = new ArrayList<>();

    String stringStream = null;

    if (errorStream != null) {
        stringStream = new String(IO.read(errorStream));

        errorList.add(stringStream);
    }

    List<String> filteredErrorList = new ArrayList<>();

    for (String string : errorList) {
        String exclusion = "(.*setlocale.*)";

        Pattern p = Pattern.compile(exclusion, Pattern.DOTALL);

        Matcher m = p.matcher(string);

        while (m.find()) {
            filteredErrorList.add(string);
        }

        if (string.contains("Picked up JAVA_TOOL_OPTIONS:")) {
            filteredErrorList.add(string);
        }
    }

    errorList.removeAll(filteredErrorList);

    Assert.assertTrue(errorList.toString(), errorList.size() <= 1);

    if (errorList.size() == 1) {
        Assert.assertTrue(errorList.get(0), errorList.get(0).isEmpty());
    }

    return output;
}

From source file:com.yahoo.sql4dclient.Main.java

/**
 * Run the command synchronously and return clubbed standard error and 
 * output stream's data./*w  w  w. j a va2 s .  co m*/
 */
private static String runCommand(final String[] cmd) throws IOException, InterruptedException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Process process = Runtime.getRuntime().exec(cmd);
    int c;
    InputStream in = process.getInputStream();
    while ((c = in.read()) != -1) {
        baos.write(c);
    }
    in = process.getErrorStream();
    while ((c = in.read()) != -1) {
        baos.write(c);
    }
    process.waitFor();
    return new String(baos.toByteArray());
}