Java Shell Command runCommandAndWait(String command, String workingDir, String extraPath)

Here you can find the source of runCommandAndWait(String command, String workingDir, String extraPath)

Description

Run command in another process and wait for it to finish.

License

Apache License

Parameter

Parameter Description
command - Command to be run in another process, e.g. "mvn clean install"
workingDir - Working directory in which to run the command.
extraPath a parameter

Exception

Parameter Description
Exception an exception

Return

exit value.

Declaration

private static Integer runCommandAndWait(String command, String workingDir, String extraPath) throws Exception 

Method Source Code


//package com.java2s;
/*/*from w w  w. j  a v  a 2  s. co m*/
 * Copyright (C) 2012 Red Hat, Inc. (jcasey@redhat.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    private static final String MVN_LOCATION = System.getProperty("maven.home");

    /**
     * Run command in another process and wait for it to finish.
     *
     * @param command - Command to be run in another process, e.g. "mvn clean install"
     * @param workingDir - Working directory in which to run the command.
     * @param extraPath
     * @return exit value.
     * @throws Exception
     */
    private static Integer runCommandAndWait(String command, String workingDir, String extraPath) throws Exception {
        String path = System.getenv("PATH");
        if (extraPath != null) {
            path = extraPath + System.getProperty("path.separator") + path;
        }

        Process proc = Runtime.getRuntime().exec(command,
                new String[] { "M2_HOME=" + MVN_LOCATION, "PATH=" + path }, new File(workingDir));
        File buildlog = new File(workingDir + "/build.log");

        BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(buildlog, true)));

        String line = null;
        String errline = null;
        while ((line = stdout.readLine()) != null || (errline = stderr.readLine()) != null) {
            if (line != null) {
                out.println(line);
            }
            if (errline != null) {
                out.println(errline);
            }
        }

        stdout.close();
        stderr.close();
        out.close();

        return proc.waitFor();
    }
}

Related

  1. runCommand(String[] args)
  2. runCommand(String[] args)
  3. runCommand(String[] cmdArray, String extraPath, String libPath)
  4. runCommand(String[] command)
  5. runCommand(String[] command)
  6. runCommandArray(String[] command)
  7. runCommandGetOutput(String startFolder, String[] args, int[] cmdReturnValue)
  8. runCommandPrompt(final String commandLine, final IPath path)
  9. runCommandWithOutput(String cmd)