Example usage for java.lang Runtime exec

List of usage examples for java.lang Runtime exec

Introduction

In this page you can find the example usage for java.lang Runtime exec.

Prototype

public Process exec(String cmdarray[]) throws IOException 

Source Link

Document

Executes the specified command and arguments in a separate process.

Usage

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

@Override
protected File findDefaultExecutable() {
    validateNodeJSVersion();// w  ww .  ja va 2 s. c o m
    Runtime rt = Runtime.getRuntime();
    Process p;
    try {
        p = rt.exec(NODE_COMMAND_PREFIX + " node");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    try {
        OutputStream outputStream = p.getOutputStream();
        PrintStream out = new PrintStream(outputStream);
        out.println("console.log(process.execPath);");
        out.close();

        return new File(getProcessOutput(p.getInputStream()));
    } catch (Throwable t) {
        throw new RuntimeException(t);
    } finally {
        p.destroy();
    }
}

From source file:org.uoa.eolus.template.Nest.java

public void removeUserTemplate(String user, String template) throws DirectoryException {
    String cmd = "rm -rf " + repo + "/" + user + "/" + template;
    System.out.println("CMD: " + cmd);
    Runtime run = Runtime.getRuntime();
    try {/*w  w  w  .j av a  2s .c  o m*/
        Process child = run.exec(cmd);
    } catch (Exception e) {
        throw new DirectoryException("Cannot execute rm command.", e);
    }
}

From source file:br.unb.bionimbuz.storage.bucket.methods.CloudMethodsAmazonGoogle.java

@Override
public void CheckStorageLatency(BioBucket bucket) throws Exception {

    if (!bucket.isMounted())
        throw new Exception("Cant check latency! Bucket not mounted: " + bucket.getName());

    float latency = 0;
    int i;//from   w  w  w  .jav  a  2 s  . c  om

    for (i = 0; i < LATENCY_CHECKS; i++) {
        //Upload
        String command = "/bin/dd if=/dev/zero of=" + bucket.getMountPoint() + "/pingfile-" + myId
                + " bs=64 count=1 oflag=dsync";

        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(command);
        //System.out.println("\nRunning command: " + command);
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;

        List<String> output = new ArrayList<>();

        while ((line = br.readLine()) != null) {
            output.add(line);
            //System.out.println("[command] " + line);
        }

        int exitVal = proc.waitFor();
        //System.out.println("[command] Process exitValue: " + exitVal);

        if (exitVal != 0) {
            throw new Exception("Error in command: " + command);
        }
        int pos1, pos2;

        pos1 = output.get(output.size() - 1).indexOf(" copied, ");
        pos1 += 9;

        pos2 = output.get(output.size() - 1).indexOf(" s, ");

        String aux;

        aux = output.get(output.size() - 1).substring(pos1, pos2);
        aux = aux.replace(',', '.');

        float value = Float.parseFloat(aux);

        latency += value;
        //System.out.println("[current] Latency: " + (latency / (i + 1)));

        File faux = new File(bucket.getMountPoint() + "/pingfile-" + myId);
        faux.delete();
    }

    bucket.setLatency(latency / (i + 1));
}

From source file:fish.payara.maven.plugins.micro.StopMojo.java

private String getLineFromJpsOutput(final Runtime re, Predicate<String> linePredicate) throws IOException {
    String jpsPath = "jps";
    if (toolchain != null) {
        jpsPath = toolchain.findTool("jps");
    }//from  ww  w . j  ava  2  s. c o  m
    Process jpsProcess = re.exec(jpsPath + " -v");
    InputStream inputStream = jpsProcess.getInputStream();
    try (BufferedReader in = new BufferedReader(new InputStreamReader(inputStream))) {
        String line;
        while ((line = in.readLine()) != null) {
            if (linePredicate.test(line)) {
                return line;
            }
        }
        return null;
    }
}

From source file:com.doomy.padlock.MainActivity.java

public void androidDebugBridge(String mPort) {
    Runtime mRuntime = Runtime.getRuntime();
    Process mProcess = null;//from w  w  w .  ja va  2 s  .  c  o  m
    OutputStreamWriter mWrite = null;

    try {
        mProcess = mRuntime.exec("su");
        mWrite = new OutputStreamWriter(mProcess.getOutputStream());
        mWrite.write("setprop service.adb.tcp.port " + mPort + "\n");
        mWrite.flush();
        mWrite.write("stop adbd\n");
        mWrite.flush();
        mWrite.write("start adbd\n");
        mWrite.flush();
        mWrite.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:cz.muni.pdfjbim.Jbig2enc.java

/**
 * run jbig2enc with symbol coding used and output in format suitable for PDF
 *
 * @param basename base// w  ww.java 2 s.  co  m
 * @param imageList list of images to be compressed
 * @throws PdfRecompressionException if any problem occurs while running jbig2enc
 */
public void run(List<String> imageList, String basename) throws PdfRecompressionException {
    if (basename == null) {
        basename = "output";
    }

    if (imageList == null) {
        throw new NullPointerException("imageList");
    }

    if (imageList.isEmpty()) {
        log.info("there are no images for running jbig2enc at (given list is empty)");
        return;
    }

    List<String> toRun = new ArrayList<String>();

    toRun.add(jbig2enc);
    toRun.add("-s");
    toRun.add("-p");
    toRun.add("-b");
    toRun.add(basename);
    toRun.add("-t");
    toRun.add(String.valueOf(defaultThresh));
    toRun.add("-T");
    toRun.add(String.valueOf(bwThresh));

    if (segment) {
        toRun.add("-S");
    }

    if (autoThresh) {
        toRun.add("--auto-thresh");

        if (useOcr) {
            toRun.add("--use-ocr");
            if (lang != null) {
                toRun.add("--lang");
                toRun.add(lang);
            }
        }
    }

    if (forced) {
        toRun.add("-ff");
    }

    toRun.addAll(imageList);

    String[] run = new String[toRun.size()];
    run = toRun.toArray(run);

    Runtime runtime = Runtime.getRuntime();
    Process pr1;
    BufferedReader reader = null;
    try {
        log.debug("Executing {}", toRun);
        pr1 = runtime.exec(run);
        OutputRedirector errRedirectThread = new OutputRedirector(pr1.getErrorStream());
        OutputRedirector outRedirectThread = new OutputRedirector(pr1.getInputStream());
        errRedirectThread.start();
        outRedirectThread.start();

        int exitValue = pr1.waitFor();

        if (exitValue != 0) {
            log.warn("jbig2enc ended with error " + exitValue);
            Tools.deleteFilesFromList(imageList);
            throw new PdfRecompressionException("jbig2enc ended with error " + exitValue);
        }
    } catch (IOException ex) {
        log.warn("running jbig2enc caused IOException", ex);
    } catch (InterruptedException ex2) {
        log.warn("running jbig2enc was interupted", ex2);
    } finally {
        IOUtils.closeQuietly(reader);
        Tools.deleteFilesFromList(imageList);
    }
}

From source file:br.unb.cic.bionimbuz.services.storage.bucket.methods.CloudMethodsAmazonGoogle.java

private void CheckStorageUpBandwith(BioBucket bucket) throws Exception {

    // Upload/*  w w w . j  a v a2  s .c o m*/
    final String command = "/bin/dd if=/dev/zero of=" + bucket.getMountPoint() + "/testfile-" + myId
            + " bs=30M count=1 iflag=nocache oflag=nocache";

    final Runtime rt = Runtime.getRuntime();
    final Process proc = rt.exec(command);
    // System.out.println("\nRunning command: " + command);
    final InputStream stderr = proc.getErrorStream();
    final InputStreamReader isr = new InputStreamReader(stderr);
    final BufferedReader br = new BufferedReader(isr);
    String line;

    final List<String> output = new ArrayList<>();

    while ((line = br.readLine()) != null) {
        output.add(line);
        // System.out.println("[command] " + line);
    }

    final int exitVal = proc.waitFor();
    // System.out.println("[command] Process exitValue: " + exitVal);

    if (exitVal != 0) {
        throw new Exception("Error in command: " + command);
    }

    int pos1, pos2;

    pos1 = output.get(output.size() - 1).indexOf(" copied, ");
    pos1 += 9;

    pos2 = output.get(output.size() - 1).indexOf(" s, ");

    String aux;

    aux = output.get(output.size() - 1).substring(pos1, pos2);
    aux = aux.replace(',', '.');

    final float value = Float.parseFloat(aux);

    bucket.setUpBandwith(31 * 1024 * 1024 / value);
}

From source file:br.unb.cic.bionimbuz.services.storage.bucket.methods.CloudMethodsAmazonGoogle.java

private void CheckStorageDlBandwith(BioBucket bucket) throws Exception {

    // Download//from   w  w  w  .  j  av a 2 s .  c  o  m
    final String command = "/bin/dd if=" + bucket.getMountPoint() + "/testfile-" + myId
            + " of=/tmp/testfile bs=30M count=1 iflag=nocache oflag=nocache";

    final Runtime rt = Runtime.getRuntime();
    final Process proc = rt.exec(command);
    // System.out.println("\nRunning command: " + command);
    final InputStream stderr = proc.getErrorStream();
    final InputStreamReader isr = new InputStreamReader(stderr);
    final BufferedReader br = new BufferedReader(isr);
    String line;

    final List<String> output = new ArrayList<>();

    while ((line = br.readLine()) != null) {
        output.add(line);
        // System.out.println("[command] " + line);
    }

    final int exitVal = proc.waitFor();
    // System.out.println("[command] Process exitValue: " + exitVal);

    if (exitVal != 0) {
        throw new Exception("Error in command: " + command);
    }

    int pos1, pos2;

    pos1 = output.get(output.size() - 1).indexOf(" copied, ");
    pos1 += 9;

    pos2 = output.get(output.size() - 1).indexOf(" s, ");

    String aux;

    aux = output.get(output.size() - 1).substring(pos1, pos2);
    aux = aux.replace(',', '.');

    final float value = Float.parseFloat(aux);

    bucket.setDlBandwith(31 * 1024 * 1024 / value);
}

From source file:org.uoa.eolus.template.Nest.java

public void copyUserToUserTemplate(String fromuser, String touser, String template, String target, boolean move)
        throws DirectoryException {
    String cmd = "mv";
    if (move)/* w ww  .j a  va2s. co m*/
        cmd = "mv " + repo + "/" + fromuser + "/" + template + " " + repo + "/" + touser + "/" + target
                + "; exit; \n";
    else
        cmd = "cp -r " + repo + "/" + fromuser + "/" + template + " " + repo + "/" + touser + "/." + target
                + "; mv " + repo + "/" + touser + "/." + target + " " + repo + "/" + touser + "/" + target
                + "; exit; \n";

    System.out.println("CMD: " + cmd);
    Runtime run = Runtime.getRuntime();
    try {
        Process child = run.exec("/bin/bash");
        BufferedWriter outCommand = new BufferedWriter(new OutputStreamWriter(child.getOutputStream()));
        outCommand.write(cmd);
        outCommand.flush();
        // child.waitFor();
        // if (child.exitValue() != 0)
        // return false;
        // else
    } catch (Exception e) {
        throw new DirectoryException("Cannot execute cp/mv command.", e);
    }
}

From source file:org.sipfoundry.sipxconfig.admin.WebCertificateManagerImpl.java

public void generateCSRFile() {
    try {/*from   w ww  .j a  va  2s  .c  o  m*/
        Runtime runtime = Runtime.getRuntime();
        String[] cmdLine = new String[] { m_binDirectory + GEN_SSL_KEYS_SH, "--csr", WEB_ONLY, DEFAULTS_FLAG,
                PARAMETERS_FLAG, PROPERTIES_FILE, WORKDIR_FLAG, m_certDirectory };
        Process proc = runtime.exec(cmdLine);
        LOG.debug("Executing: " + StringUtils.join(cmdLine, BLANK));
        proc.waitFor();
        if (proc.exitValue() != 0) {
            throw new UserException(SCRIPT_ERROR, SCRIPT_EXCEPTION_MESSAGE + proc.exitValue());
        }
    } catch (IOException e) {
        throw new UserException(SCRIPT_ERROR, e.getMessage());
    } catch (InterruptedException e) {
        throw new UserException(SCRIPT_ERROR, e.getMessage());
    }
}