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:com.panet.imeta.trans.steps.gpbulkloader.GPBulkLoader.java

public boolean execute(GPBulkLoaderMeta meta, boolean wait) throws KettleException {
    Runtime rt = Runtime.getRuntime();

    try {/*from w ww  . j a v a  2  s .c  o  m*/
        psqlProcess = rt.exec(createCommandLine(meta, true));
        // any error message?
        StreamLogger errorLogger = new StreamLogger(psqlProcess.getErrorStream(), "ERROR");

        // any output?
        StreamLogger outputLogger = new StreamLogger(psqlProcess.getInputStream(), "OUTPUT");

        // kick them off
        errorLogger.start();
        outputLogger.start();

        if (wait) {
            // any error???
            int exitVal = psqlProcess.waitFor();
            logBasic(Messages.getString("GPBulkLoader.Log.ExitValuePsqlPath", "" + exitVal)); //$NON-NLS-1$
        }
    } catch (Exception ex) {
        // Don't throw the message upwards, the message contains the password.
        throw new KettleException("Error while executing psql \'" + createCommandLine(meta, false) + "\'");
    }

    return true;
}

From source file:com.panet.imeta.trans.steps.pgbulkloader.PGBulkLoader.java

public boolean execute(PGBulkLoaderMeta meta, boolean wait) throws KettleException {
    Runtime rt = Runtime.getRuntime();

    try {/*from w w  w.  j  a  v a 2  s  . c  o  m*/
        String cmd = createCommandLine(meta, true);
        logBasic("Executing command: " + cmd);
        data.psqlProcess = rt.exec(cmd);

        // any error message?
        //
        data.errorLogger = new StreamLogger(data.psqlProcess.getErrorStream(), "ERROR");

        // any output?
        data.outputLogger = new StreamLogger(data.psqlProcess.getInputStream(), "OUTPUT");

        // Where do we send the data to? --> To STDIN of the psql process
        //
        data.pgOutputStream = data.psqlProcess.getOutputStream();

        // kick them off
        new Thread(data.errorLogger).start();
        new Thread(data.outputLogger).start();

        // OK, from here on, we need to feed in the COPY command followed by the data into the pgOutputStream
        //
    } catch (Exception ex) {
        throw new KettleException("Error while executing psql : " + createCommandLine(meta, false), ex);
    }

    return true;
}

From source file:ch.puzzle.itc.mobiliar.business.shakedown.control.ShakedownTestRunner.java

private void copyThroughSCP(String filepath, STS sts)
        throws IOException, InterruptedException, ShakedownTestException {
    Runtime r = Runtime.getRuntime();
    File f = new File(filepath);
    String copyCommand = "scp " + filepath + " " + sts.getUser() + "@" + sts.getRemoteHost() + ":"
            + sts.getRemoteSTPPath() + File.separator + f.getName();
    log.info("EXECUTE: " + copyCommand);
    Process p = r.exec(copyCommand);
    if (p.waitFor() == 0) {
        log.info("Copied STP " + filepath + " to host " + sts.getRemoteHost());
    } else {/* w w  w  . j  a  v a2 s .co m*/
        throw new ShakedownTestException(
                "Was not able to copy STP " + filepath + " to host " + sts.getRemoteHost());
    }
}

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

private InputStream runCommand(String[] command) {
    try {/* w ww  .  ja v a2s.  co  m*/
        Runtime runtime = Runtime.getRuntime();

        Process proc = runtime.exec(command);
        LOG.debug("Executing: " + StringUtils.join(command, BLANK));
        proc.waitFor();
        if (proc.exitValue() != 0) {
            throw new ScriptExitException(proc.exitValue());
        }
        return proc.getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.pentaho.di.trans.steps.gpbulkloader.GPBulkLoader.java

public boolean execute(GPBulkLoaderMeta meta, boolean wait) throws KettleException {
    Runtime rt = Runtime.getRuntime();

    try {/*from   ww  w  . jav  a 2  s . c  o  m*/
        psqlProcess = rt.exec(createCommandLine(meta, true));
        // any error message?
        StreamLogger errorLogger = new StreamLogger(psqlProcess.getErrorStream(), "ERROR");

        // any output?
        StreamLogger outputLogger = new StreamLogger(psqlProcess.getInputStream(), "OUTPUT");

        // kick them off
        errorLogger.start();
        outputLogger.start();

        if (wait) {
            // any error???
            int exitVal = psqlProcess.waitFor();
            logBasic(BaseMessages.getString(PKG, "GPBulkLoader.Log.ExitValuePsqlPath", "" + exitVal));
        }
    } catch (KettleException ke) {
        // Re-throw the exception to the caller.
        throw ke;
    } catch (Exception ex) {
        // The message below doesn't include the password since passwords aren't supported.
        throw new KettleException("Error while executing psql \'" + createCommandLine(meta, false) + "\'", ex);
    }

    return true;
}

From source file:org.pentaho.di.trans.steps.pgbulkloader.PGBulkLoader.java

public boolean execute(PGBulkLoaderMeta meta, boolean wait) throws KettleException {
    Runtime rt = Runtime.getRuntime();

    try {/*  w  w w  . j  a  va 2 s . c  o m*/
        String cmd = createCommandLine(meta, true);
        logBasic("Executing command: " + cmd);
        data.psqlProcess = rt.exec(cmd);

        // any error message?
        //
        data.errorLogger = new StreamLogger(log, data.psqlProcess.getErrorStream(), "ERROR {0}");

        // any output?
        data.outputLogger = new StreamLogger(log, data.psqlProcess.getInputStream(), "OUTPUT {0}");

        // Where do we send the data to? --> To STDIN of the psql process
        //
        data.pgOutputStream = data.psqlProcess.getOutputStream();

        // kick them off
        new Thread(data.errorLogger).start();
        new Thread(data.outputLogger).start();

        // OK, from here on, we need to feed in the COPY command followed by the data into the pgOutputStream
        //
    } catch (Exception ex) {
        throw new KettleException("Error while executing psql : " + createCommandLine(meta, false), ex);
    }

    return true;
}

From source file:Breaker.VideoTool.java

private void Convert(File convertFile) {
    int OSX = 0, MS = 1;
    String[] HBLoc = { "./Resources/HandBrakeCLI", ".\\Resources\\HandBrakeCLI.exe" };
    String OS = System.getProperty("os.name");

    try {//from  w  w  w. ja  v  a  2  s .  c om
        Runtime RT = Runtime.getRuntime();

        // Gets default path 
        String Execute = GetStringByOS(OS, HBLoc);

        Execute += " -i " + convertFile.getPath();
        String newFileName = convertFile.getName();
        if (BreakView.cleanFileName) {
            newFileName = CleanName(newFileName);
        }

        Execute += " -o " + BreakView.Converted + "/" + newFileName;

        if (BreakView.Preset != null)
            Execute += " --preset=\"" + BreakView.Preset + "\"";

        BV.SetWarningText(Execute);

        Process PC = RT.exec("ls");
        PC.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(PC.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.syncany.cli.CommandLineClient.java

private int execManPageAndExit(String manPage) {
    try {/* w w  w .j a  va2s .com*/
        Runtime runtime = Runtime.getRuntime();
        Process manProcess = runtime.exec(new String[] { "sh", "-c", "man " + manPage + " > /dev/tty" });

        int manProcessExitCode = manProcess.waitFor();

        if (manProcessExitCode == 0) {
            return 0;
        }
    } catch (Exception e) {
        // Don't care!
    }
    return 1;
}

From source file:com.hp.avmon.home.service.LicenseService.java

public void callWindowsCpuId() {
    Runtime rt = Runtime.getRuntime();
    Process p = null;/*from w  ww  . ja va2s  . co  m*/
    try {
        String exportPath = getLicensePath();
        String vbexepath = "cmd.exe /c" + exportPath + "/" + "vbcpuid_server.exe";
        p = rt.exec(vbexepath);

        InputStreamReader isr_normal = new InputStreamReader(p.getInputStream());
        int ch = 0;
        StringBuffer strbuf = new StringBuffer();
        while ((ch = isr_normal.read()) != -1) {
            strbuf.append((char) ch);
        }
        p.waitFor();

    } catch (Exception e) {
        // e.printStackTrace();
    }
}

From source file:com.cfets.door.yarn.jboss.JBossApplicationMaster.java

/**
 * Dump out contents of $CWD and the environment to stdout for debugging
 */// w w w  .j av a 2  s.  c  om
private void dumpOutDebugInfo() {

    LOG.info("Dump debug output");
    Map<String, String> envs = System.getenv();
    for (Map.Entry<String, String> env : envs.entrySet()) {
        LOG.info("System env: key=" + env.getKey() + ", val=" + env.getValue());
        System.out.println("System env: key=" + env.getKey() + ", val=" + env.getValue());
    }

    String cmd = "ls -al";
    Runtime run = Runtime.getRuntime();
    Process pr = null;
    try {
        pr = run.exec(cmd);
        pr.waitFor();

        BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = "";
        while ((line = buf.readLine()) != null) {
            LOG.info("System CWD content: " + line);
            System.out.println("System CWD content: " + line);
        }
        buf.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}