Example usage for java.lang Process destroy

List of usage examples for java.lang Process destroy

Introduction

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

Prototype

public abstract void destroy();

Source Link

Document

Kills the process.

Usage

From source file:Main.java

public static boolean runRootCommand(Context context, String command) {
    Process process = null;
    DataOutputStream os = null;//  w w w  .  ja va  2 s  .  c o  m
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {
        Log.d("*** DEBUG ***", "Error - " + e.getMessage());
        return false;
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {

        }
    }
    return true;
}

From source file:Main.java

public static boolean tempRunRootCommand(Context context, String command) {
    Process process = null;
    DataOutputStream os = null;/*from   w w  w  . ja v a  2s.co m*/
    try {
        process = Runtime.getRuntime().exec("su1");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {
        Log.d("*** DEBUG ***", "Error - " + e.getMessage());
        return false;
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {

        }
    }
    return true;
}

From source file:com.insightml.utils.io.IoUtils.java

private static String process(final Process proc) throws IOException {
    final StringBuilder out = new StringBuilder();
    try (final BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));) {
        String s;/*from  ww w  .jav a  2 s.  c o  m*/
        while ((s = stdInput.readLine()) != null) {
            out.append(s);
            out.append('\n');
        }
    } finally {
        proc.destroy();
    }
    return out.toString();
}

From source file:Main.java

public static int exec(String command) {
    Process proc = null;
    BufferedReader error = null;/*from w w  w. j av a 2 s  . com*/
    try {
        proc = Runtime.getRuntime().exec(command);
        error = toReader(proc.getErrorStream());
        int retVal = proc.waitFor();
        if (retVal != 0) {
            String line;
            while ((line = error.readLine()) != null) {
                Log.d(TAG, "exec error:" + line);
            }
        }
        return retVal;
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
        if (proc != null) {
            proc.destroy();
        }
    }
    return -1;
}

From source file:Main.java

private static String executeTop() {
    Process p = null;
    BufferedReader in = null;//  ww  w.  j a v a2s .  c  o m
    String returnString = null;
    try {
        p = Runtime.getRuntime().exec("top -n 1");
        in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while (returnString == null || returnString.contentEquals("")) {
            returnString = in.readLine();
        }
    } catch (IOException e) {
        Log.e("executeTop", "error in getting first line of top");
        e.printStackTrace();
    } finally {
        try {
            in.close();
            p.destroy();
        } catch (IOException e) {
            Log.e("executeTop", "error in closing and destroying top process");
            e.printStackTrace();
        }
    }
    return returnString;
}

From source file:de.knowwe.visualization.util.Utils.java

private static boolean isFileClosedUnix(File file) {
    try {//from  ww  w.ja v a  2 s  .com
        Process plsof = new ProcessBuilder(new String[] { "lsof", "|", "grep", file.getAbsolutePath() })
                .start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(plsof.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains(file.getAbsolutePath())) {
                reader.close();
                plsof.destroy();
                return false;
            }
        }
        reader.close();
        plsof.destroy();
    } catch (Exception ignore) {
    }
    return true;
}

From source file:com.ebiznext.flume.source.TestMultiLineExecSource.java

private static List<String> exec(String command) throws Exception {
    String[] commandArgs = command.split("\\s+");
    Process process = new ProcessBuilder(commandArgs).start();
    BufferedReader reader = null;
    try {/*from  w  ww .j  av  a 2 s . c  om*/
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        List<String> result = Lists.newArrayList();
        String line;
        while ((line = reader.readLine()) != null) {
            result.add(line);
        }
        return result;
    } finally {
        process.destroy();
        if (reader != null) {
            reader.close();
        }
        int exit = process.waitFor();
        if (exit != 0) {
            throw new IllegalStateException("Command [" + command + "] exited with " + exit);
        }
    }
}

From source file:com.utdallas.s3lab.smvhunter.enumerate.UIEnumerator.java

public static String execSpecial(String command) throws Exception {
    String cmd[] = { "/bin/bash", "-c", command };

    logger.info("command executed: " + command);
    Process pr = Runtime.getRuntime().exec(cmd);

    String result = IOUtils.toString(pr.getInputStream());
    pr.destroy();
    return StringUtils.strip(result);
}

From source file:com.utdallas.s3lab.smvhunter.enumerate.UIEnumerator.java

/**
 * exec external command//from ww w  .j  a v a 2s  .c  o  m
 * @param command
 * @return
 * @throws IOException
 */
public static String execCommand(String command) throws IOException {
    logger.info("command executed " + command);
    Process pr = Runtime.getRuntime().exec(command);
    String result = IOUtils.toString(pr.getInputStream());
    pr.destroy();
    return result;
}

From source file:io.appium.java_client.service.local.AppiumServiceBuilder.java

private static File findNodeInCurrentFileSystem() {
    Runtime rt = Runtime.getRuntime();
    String instancePath;/*from   ww w  . ja  v  a 2  s  . c o  m*/
    Process p = null;
    try {
        p = rt.exec(returnCommandThatSearchesForDefaultNode());
        p.waitFor();
        instancePath = getProcessOutput(p.getInputStream());
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (p != null)
            p.destroy();
    }

    File result;
    if (StringUtils.isBlank(instancePath)
            || !(result = new File(instancePath + File.separator + NODE_MODULES_FOLDER + APPIUM_NODE_MASK))
                    .exists())
        throw new InvalidServerInstanceException(
                "There is no installed nodes! Please install "
                        + " node via NPM (https://www.npmjs.com/package/appium#using-node-js) or download and "
                        + "install Appium app (http://appium.io/downloads.html)",
                new IOException("The installed appium node package has not been found."));

    return result;
}