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:org.freeeed.util.OsUtil.java

public static List<String> runCommand(String command, boolean addErrorStream, long timeout) throws IOException {
    LOGGER.debug("Running command: {} with addErrorStream = {} and process timeout in sec {}", command,
            addErrorStream, timeout);//from   w ww . j  a va2  s  .c o m
    Process p = Runtime.getRuntime().exec(command);
    try {
        if (!p.waitFor(timeout, TimeUnit.SECONDS)) {
            p.destroy();
            throw new IOException("Process timed out");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    // read the output from the command
    List<String> output = IOUtils.readLines(p.getInputStream(), Charset.defaultCharset());
    List<String> errorOutput = IOUtils.readLines(p.getErrorStream(), Charset.defaultCharset());
    for (String line : errorOutput) {
        LOGGER.info(line);
    }
    if (addErrorStream) {
        output.addAll(errorOutput);
    }
    return output;
}

From source file:MD5.java

public static String getRecoveryMD5() {
    String MD5string = "";
    String recoveryFilename = "/dev/mtd/mtd1";
    try {//from   w  w  w .j  av a2s .  c  om
        Process p = Runtime.getRuntime().exec("su");
        OutputStream os = p.getOutputStream();
        os.write(("md5sum " + recoveryFilename).getBytes());
        os.flush();
        os.close();
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str = br.readLine();
        MD5string = str.split("  ")[0].trim();
        is.close();
        br.close();
        p.destroy();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
    System.out.println(MD5string);
    return MD5string;
}

From source file:com.qubit.terra.docs.util.helpers.OpenofficeInProcessConverter.java

public static synchronized byte[] convert(final byte[] odtContent, final String tempDirFullPath,
        final String mimeTypeAbbreviation) {

    try {/*  w  w  w. j  av  a2  s  .c om*/
        long currentTimeMillis = System.currentTimeMillis();
        if (System.getProperty("os.name").startsWith("Windows")) {
            final String odtFilename = tempDirFullPath + "openofficeConversion-" + currentTimeMillis + ".odt";

            FileUtils.writeByteArrayToFile(new File(odtFilename), odtContent);

            final Process process = Runtime.getRuntime()
                    .exec(String.format("soffice --headless --convert-to %s --outdir %s %s",
                            mimeTypeAbbreviation, tempDirFullPath.subSequence(0, tempDirFullPath.length() - 1),
                            odtFilename));

            try {
                process.waitFor();
            } catch (InterruptedException e) {
            }

            process.destroy();

            final String outputFilename = tempDirFullPath + "openofficeConversion-" + currentTimeMillis + "."
                    + mimeTypeAbbreviation;
            final byte[] output = FileUtils.readFileToByteArray(new File(outputFilename));

            FileUtils.deleteQuietly(new File(odtFilename));
            FileUtils.deleteQuietly(new File(outputFilename));
            return output;

        } else {
            final String odtFilename = tempDirFullPath + "/openofficeConversion-" + currentTimeMillis + ".odt";

            FileUtils.writeByteArrayToFile(new File(odtFilename), odtContent);

            final Process process = Runtime.getRuntime()
                    .exec(String.format(
                            "soffice --headless --convert-to %s -env:UserInstallation=file://%s --outdir %s %s",
                            mimeTypeAbbreviation, tempDirFullPath, tempDirFullPath, odtFilename));

            try {
                process.waitFor();
            } catch (InterruptedException e) {
            }

            process.destroy();

            final String outputFilename = tempDirFullPath + "/openofficeConversion-" + currentTimeMillis + "."
                    + mimeTypeAbbreviation;
            final byte[] output = FileUtils.readFileToByteArray(new File(outputFilename));
            FileUtils.deleteQuietly(new File(odtFilename));
            FileUtils.deleteQuietly(new File(outputFilename));
            return output;

        }
    } catch (final Throwable e) {
        throw new OpenofficeInProcessConversionException(e);
    }

}

From source file:com.utdallas.s3lab.smvhunter.monkey.MonkeyMe.java

/**
 * Process to execute commands in command line
 * @param command/*from  w ww .  jav  a 2 s  .c  o m*/
 * @return
 * @throws IOException
 */
public static String execCommand(String command) throws IOException {
    if (logDebug)
        logger.debug(String.format("Executing command %s", command));
    System.out.println(String.format("Executing command %s", command));
    Process pr = Runtime.getRuntime().exec(command);
    String result = IOUtils.toString(pr.getInputStream());

    pr.destroy();
    return result;
}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method to execute command arrays./*from ww  w  .j a v  a 2 s  .  c om*/
 * 
 * @param command
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
public static Result executeCommand(String... command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method executeCommand./*from ww w .j  a v a2 s . c o m*/
 * 
 * @param command
 *            String
 * @return Result
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws InterruptedException
 *             the interrupted exception
 */
public static Result executeCommand(String command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:Main.java

public static boolean getRootPermission(Context context) {
    String packageCodePath = context.getPackageCodePath();
    Process process = null;
    DataOutputStream os = null;//from  w w  w .  ja  v  a2  s . c o m
    try {
        String cmd = "chmod 777 " + packageCodePath;
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {
        return false;
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}

From source file:edu.cmu.cs.lti.ark.fn.data.prep.ParsePreparation.java

public static void runExternalCommand(String command) {
    String s;/* ww  w . ja  v a2s.c  o m*/
    try {
        Process p = Runtime.getRuntime().exec(command);
        PrintStream errStream = System.err;
        System.setErr(System.out);
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
        p.destroy();
        System.setErr(errStream);
    } catch (IOException e) {
        System.out.println("exception happened - here's what I know: ");
        e.printStackTrace();
        System.exit(-1);
    }
}

From source file:com.floragunn.searchguard.util.SecurityUtil.java

public static boolean isRootUser() {

    boolean isRoot = false;

    int exitValue = -1;
    String result = null;/*from   w ww  . j a  v a  2 s .  c om*/

    try {
        final Process p = Runtime.getRuntime().exec("id -u");
        result = IOUtils.toString(p.getInputStream());
        exitValue = p.waitFor();
        p.destroy();
    } catch (final Exception e) {
        //ignore
    }

    if (exitValue == 0 && result != null) {
        isRoot = "0".equals(result.trim());
    }

    if (!isRoot) {
        return isWindowsAdmin();
    } else {
        return true;
    }
}

From source file:com.highcharts.export.util.SVGCreator.java

public static int executeCommandLine(final String commandLine, final long timeout)
        throws IOException, InterruptedException, TimeoutException {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(commandLine);

    Worker worker = new Worker(process);
    worker.start();//from   ww  w  . j a  v a  2s .c  o  m
    try {
        worker.join(timeout);
        if (worker.exit != null)
            return worker.exit;
        else
            throw new TimeoutException();
    } catch (InterruptedException ex) {
        worker.interrupt();
        Thread.currentThread().interrupt();
        throw ex;
    } finally {
        process.destroy();
    }

}