Java exec exec(ArrayList args, File path)

Here you can find the source of exec(ArrayList args, File path)

Description

exec

License

Open Source License

Declaration

public static void exec(ArrayList<String> args, File path)
            throws RuntimeException, IOException, InterruptedException 

Method Source Code

//package com.java2s;
/*//from  ww w .  j a  va2s .co m
 * Tencent is pleased to support the open source community by making Tinker available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * 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.File;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {
    public static void exec(ArrayList<String> args, File path)
            throws RuntimeException, IOException, InterruptedException {
        ProcessBuilder ps = new ProcessBuilder(args);
        ps.redirectErrorStream(true);
        if (path != null) {
            ps.directory(path);
        }
        Process pr = ps.start();
        BufferedReader ins = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line;
        while ((line = ins.readLine()) != null) {
            System.out.println(line);
        }
        if (pr.waitFor() != 0) {
            throw new RuntimeException("exec cmd failed! args: " + args);
        }
        ins.close();
    }
}

Related

  1. exec(File dir, String cmd)
  2. exec(final String cmd, final String[] args, final byte[] in, final File dir)
  3. exec(List cmd, File binDir, File workDir, boolean parseOutput, boolean tossErrorOutput)
  4. exec(String args)