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, String[] envp) throws IOException 

Source Link

Document

Executes the specified command and arguments in a separate process with the specified environment.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//w  w  w  .  j  ava 2 s.c  o  m

        String[] envArray = new String[2];
        envArray[0] = "";
        envArray[1] = "";
        // create a process and execute notepad.exe and currect environment
        Runtime runTime = Runtime.getRuntime();

        Process process = runTime.exec("notepad.exe", envArray);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   w ww . ja  v  a 2  s.  c o  m*/
        String[] cmdArray = new String[2];

        // the program to open
        cmdArray[0] = "notepad.exe";

        // txt file to open with notepad
        cmdArray[1] = "data.txt";

        String[] envArray = new String[2];
        envArray[0] = "";
        envArray[1] = "";

        Runtime runTime = Runtime.getRuntime();
        Process process = runTime.exec(cmdArray, envArray);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.wendal.java.dex.decomplier.toolkit.IO_Tool.java

public static void dexdump(String filepath) throws IOException {
    Runtime runtime = Runtime.getRuntime();
    runtime.exec("dexdump", new String[] { filepath, ">classes.txt" });
}

From source file:org.wso2.carbon.greg.utils.StoreTestUtils.java

/**
 * Executes an ant command given the file location and command
 *
 * @param location location of the ant build file
 * @param command  command to be executed
 *//*w w  w .j  a  v  a2  s. c o m*/
public static void runAnt(String location, String command) throws StoreTestException {
    Runtime runTime = Runtime.getRuntime();
    String osName = "";
    try {
        log.info("Executing ant script for getting started samples");
        osName = System.getProperty("os.name");
        Process process;
        if (osName.startsWith("Windows")) {
            process = runTime.exec("cmd.exe /C ant -f " + location + (command != null ? " " + command : ""),
                    null);
        } else {
            process = runTime.exec("ant -f " + location + (command != null ? " " + command : ""), null);
        }
        log.info(printAntShellOutput(process));

    } catch (IOException e) {
        throw new StoreTestException("Could not execute the ant script", e);
    }
    log.info("Finished executing ant script for getting started samples");
    log.info("Thread is sleeping for 10 seconds");
    try {
        Thread.sleep(1000 * 40);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.info("Thread is running");
}

From source file:org.ensembl.healthcheck.util.StreamGobbler.java

private static int exec(String command, Appendable out, Appendable err, boolean discard, String[] environment)
        throws IOException {

    Runtime rt = Runtime.getRuntime();

    Process proc = rt.exec(command, environment);

    return waitForProcess(out, err, discard, proc);
}

From source file:org.ensembl.healthcheck.util.StreamGobbler.java

public static int exec(String[] command, Appendable out, Appendable err, boolean discard, String[] environment)
        throws IOException {

    Runtime rt = Runtime.getRuntime();

    Process proc = rt.exec(command, environment);

    return waitForProcess(out, err, discard, proc);
}

From source file:org.globus.workspace.accounting.impls.dbdefault.DelayedAccountingFileLogger.java

private static boolean setFilePermissions(String file, int mode) {

    final Runtime runtime = Runtime.getRuntime();
    final String[] cmd = new String[] { "chmod", String.valueOf(mode), file };
    Process process = null;/*w  w  w . ja  va 2s.c  om*/
    try {
        process = runtime.exec(cmd, null);
        return process.waitFor() == 0 ? true : false;
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.error(e.getMessage(), e);
        } else {
            logger.error(e.getMessage());
        }
        return false;
    } finally {
        if (process != null) {
            try {
                process.getErrorStream().close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            try {
                process.getInputStream().close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            try {
                process.getOutputStream().close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.smartfrog.avalanche.client.sf.exec.simple.StartComponent.java

public void startApplication() throws IOException {
    Runtime run;

    log.info("Starting Component " + componentName);
    run = Runtime.getRuntime();/*w w  w . j a va  2 s . c o  m*/
    log.info("Starting application " + command);

    int exitVal = 0;
    try {
        System.out.println("Running the command : " + command);
        proc = run.exec(command, env);
        stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

        readOutput();
        exitVal = proc.waitFor();
        if (exitVal != 0) {
            log.error("Return for the command " + command + " is " + exitVal);
            String err = "Error : ";
            String s = null;
            while ((s = stdError.readLine()) != null) {
                err += s;
            }
            throw new IOException("Return value for the command " + command + " is  not 0." + err);
        }
    } catch (IOException e) {
        log.error("Error while executing the command : " + command, e);
        throw new IOException(e.toString());
    } catch (InterruptedException ie) {
        log.error("Error while executing the command : " + command, ie);
        throw new IOException(ie.toString());
    }
}

From source file:org.wso2.carbon.registry.app.test.FileSystemImportExportTestCase.java

@Test(groups = { "wso2.greg" }, dependsOnMethods = { "FileExportTest" })
public void JarFileExportTest() throws RegistryException, FileNotFoundException {
    String jarFileName = "jcr-2.0.jar";
    String filePath = getTestArtifactLocation() + "artifacts" + File.separator + "GREG" + File.separator + "jcr"
            + File.separator + jarFileName;

    File file = new File(filePath);
    RegistryClientUtils.importToRegistry(file, "/framework", registry);

    assertTrue(registry.resourceExists("/framework/" + jarFileName), "Resource not found.");

    Process process;/*  w  w  w  .  j  a  v  a2s .  c o  m*/
    Runtime runTime = Runtime.getRuntime();
    String fileExportPath = EXPORT_PATH + "/target/export/";
    String osName = "";
    if (osName.startsWith("Windows")) {
        try {
            process = runTime.exec("cmd.exe /C" + "" + "mkdir" + "" + fileExportPath, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        try {
            process = runTime.exec("mkdir" + " " + fileExportPath, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    File file2 = new File(fileExportPath);
    RegistryClientUtils.exportFromRegistry(file2, "/framework", registry);

    File f1 = new File(fileExportPath + jarFileName);
    assertTrue(f1.exists(), "File doesn't exist at the location");

    if (osName.startsWith("Windows")) {
        try {
            process = runTime.exec("cmd.exe /C" + "" + "del" + "" + fileExportPath, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        try {
            process = runTime.exec("rm -rf" + " " + fileExportPath, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.wso2.carbon.registry.sample.test.SampleDataPopulatorTestCase.java

private void runAnt(String location, String command) {
    Runtime runTime = Runtime.getRuntime();
    String osName = "";
    try {//w w w.  ja v  a  2  s.c o  m
        osName = System.getProperty("os.name");
    } catch (Exception e) {
        System.out.println("Exception caught =" + e.getMessage());
    }
    try {
        Process p;
        if (osName.startsWith("Windows")) {
            p = runTime.exec("cmd.exe /C ant -f " + location + (command != null ? " " + command : ""), null);
        } else {
            p = runTime.exec("ant -f " + location + (command != null ? " " + command : ""), null);
        }
        // Print ant output.
        System.out.println(printOutput(p));
    } catch (IOException e) {
        e.printStackTrace();
    }
}