Java exec execProcess(String[] cmdline, final long timeout)

Here you can find the source of execProcess(String[] cmdline, final long timeout)

Description

execProcess

License

Open Source License

Parameter

Parameter Description
cmdline a parameter
timeout a parameter

Exception

Parameter Description
IOException an exception

Return

int

Declaration

private static int execProcess(String[] cmdline, final long timeout) throws IOException 

Method Source Code

//package com.java2s;
/**/*  w ww .  j av  a 2 s . c  om*/
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.io.IOException;

public class Main {
    /**
     * execProcess
     * 
     * @param cmdline
     * @param timeout
     * @return int
     * @throws IOException
     */
    private static int execProcess(String[] cmdline, final long timeout) throws IOException {
        Process process = Runtime.getRuntime().exec(cmdline);
        final Thread thread = Thread.currentThread();
        Thread waitTimeout = new Thread() {
            public void run() {
                try {
                    Thread.sleep(timeout);
                    thread.interrupt();
                } catch (InterruptedException ignore) {
                }
            }
        };

        int exitcode = 0;
        if (timeout != -1) {
            try {
                waitTimeout.start();
                exitcode = process.waitFor();
                waitTimeout.interrupt();
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
            process.destroy();
        }
        try {
            exitcode = process.waitFor();
        } catch (InterruptedException e) {
        }
        return exitcode;
    }
}

Related

  1. execLocalhostCmd(String cmd)
  2. exeCmdByOs(String cmd)
  3. execName()
  4. execPrint(final boolean out, final String... command)
  5. execProcess(String process)
  6. Execption2Strings(boolean rep, Throwable... execptions)
  7. execScript(File shellScriptFile, String[] scriptCommand, PrintWriter execLog)
  8. execShell(String cmd)
  9. execShell(String shell)