Example usage for java.lang Process getInputStream

List of usage examples for java.lang Process getInputStream

Introduction

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

Prototype

public abstract InputStream getInputStream();

Source Link

Document

Returns the input stream connected to the normal output of the process.

Usage

From source file:name.martingeisse.phunky.equivalence.SingleSourceFileEquivalenceTests.java

/**
 * @param file//from   www  . j  ava2  s . c  o  m
 */
private void runTest(File file) throws Exception {

    // run with Phunky
    String phunkyOutput;
    {
        StringWriter phunkyStringWriter = new StringWriter();
        PhpRuntime runtime = new PhpRuntime(true);
        runtime.setOutputWriter(phunkyStringWriter);
        runtime.getInterpreter().execute(file);
        phunkyOutput = phunkyStringWriter.toString();
    }

    // run with PHP
    String phpOutput;
    {
        Process process = Runtime.getRuntime().exec("php " + file.getPath());
        phpOutput = IOUtils.toString(process.getInputStream(), "iso-8859-1");
    }

    // compare them
    Assert.assertEquals(phpOutput, phunkyOutput);

}

From source file:eu.scape_project.archiventory.identifiers.UnixFileIdentification.java

/**
 * Run unix file identification on file/*from w ww.j  a  va 2  s . co  m*/
 *
 * @param filePath Absolute file path
 * @return Result list
 * @throws FileNotFoundException
 * @throws IOException
 */
private synchronized String executeProcess(String command) throws FileNotFoundException, IOException {
    Process p = Runtime.getRuntime().exec(command);
    InputStream is = p.getInputStream();
    return IOUtils.copyInputStreamToString(is);
}

From source file:com.photon.phresco.framework.param.impl.IosDeviceTypeParameterImpl.java

private String cureentXcodeVersion() throws IOException {
    String version = "";
    ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "xcodebuild -version");
    pb.redirectErrorStream(true);/*  ww w . j  ava2  s .c om*/
    pb.directory(new File("/"));
    Process process = pb.start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
        String[] splited = line.split("\\s");
        version = splited[1];
        break;
    }
    return version;
}

From source file:com.web.controller.ToolController.java

@ResponseBody
@RequestMapping(value = "/tool/verifyapk", method = RequestMethod.POST)
public String verifyApk(@RequestParam("apkfile") MultipartFile file) {

    //keytool -list -printcert -jarfile d:\weixin653android980.apk
    //keytool -printcert -file D:\testapp\META-INF\CERT.RSA
    //System.out.println("12345");
    try {/*from  w  w  w  .j a va2 s . c o  m*/
        OutputStream stream = new FileOutputStream(new File(file.getOriginalFilename()));
        BufferedOutputStream outputStream = new BufferedOutputStream(stream);
        outputStream.write(file.getBytes());
        outputStream.flush();
        outputStream.close();

        Runtime runtime = Runtime.getRuntime();
        String ccString = "keytool -list -printcert -jarfile C:\\Users\\Administrator\\Desktop\\zju1.1.8.2.apk";
        Process p = runtime.exec(ccString);

        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

        StringBuilder sb = new StringBuilder();
        while (br.readLine() != null) {
            sb.append(br.readLine() + "<br/>");
        }
        p.destroy();
        p = null;
        return sb.toString();
    } catch (FileNotFoundException fe) {
        return fe.getMessage();
    } catch (IOException ioe) {
        return ioe.getMessage();
    }

}

From source file:com.github.sakserv.minicluster.yarn.util.ExecShellCliParser.java

public int runCommand() throws Exception {
    String command = getCommand();
    String stdoutFile = getStdoutPath();
    String stderrFile = getStderrPath();

    Process p = Runtime.getRuntime().exec(command.split(" "));

    String stdout = getOutput(p.getInputStream());
    String stderr = getOutput(p.getErrorStream());

    writeOutputToFile(stdout, new File(stdoutFile));
    writeOutputToFile(stderr, new File(stderrFile));

    p.waitFor();//from www .  j  a  v  a 2s .c om
    return p.exitValue();
}

From source file:gr.aueb.dmst.istlab.unixtools.actions.impl.ExecuteCustomCommandAction.java

@Override
public void execute(ActionExecutionCallback<DataActionResult<InputStream>> callback)
        throws IOException, InterruptedException {
    DataActionResult<InputStream> result;
    List<String> arguments = EclipsePluginUtil.getSystemShellInfo();

    ProcessBuilder pb;/*from ww  w . jav a2s. com*/

    if (SystemUtils.IS_OS_WINDOWS) {
        pb = new ProcessBuilder(arguments.get(0), arguments.get(1), arguments.get(2) + "\"cd "
                + this.commandToExecute.getShellDirectory() + ";" + this.commandToExecute.getCommand() + "\"");
    } else {
        arguments.add(this.commandToExecute.getCommand());
        pb = new ProcessBuilder(arguments);
        pb.directory(new File(this.commandToExecute.getShellDirectory()));
    }
    pb.redirectErrorStream(true);

    Process p;
    try {
        p = pb.start();
        p.waitFor();
        InputStream cmdStream = p.getInputStream();
        result = new DataActionResult<>(cmdStream);
    } catch (IOException e) {
        logger.fatal("IO problem occurred while executing the command");
        result = new DataActionResult<>(e);
        throw new IOException(e);
    } catch (InterruptedException e) {
        logger.fatal("The current thread has been interrupted while executing the command");
        result = new DataActionResult<>(e);
        throw new InterruptedException();
    }

    callback.onCommandExecuted(result);
}

From source file:com.formkiq.core.service.sign.MacWrapperConfig.java

/**
 * Attempts to find an executable in the system path.
 * @param executable {@link String}/*  w  w w.  java  2s .co m*/
 * @return {@link String}
 */
public String findExecutable(final String executable) {
    try {
        String osname = System.getProperty("os.name").toLowerCase();

        String cmd = osname.contains("windows") ? "where " + executable : "which " + executable;

        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();

        String text = IOUtils.toString(p.getInputStream(), Charset.defaultCharset());

        if (text.isEmpty()) {
            throw new RuntimeException();
        }

        return text;

    } catch (InterruptedException e) {
        return null;
    } catch (IOException e) {
        return null;
    } catch (RuntimeException e) {
        return null;
    }
}

From source file:eu.scape_project.up2ti.identifiers.UnixFileIdentification.java

/**
 * Run unix file identification on file/*from w  w  w.j a  va2  s  .co m*/
 *
 * @return Result list
 * @throws FileNotFoundException
 * @throws IOException
 */
private synchronized String executeProcess(String command) throws FileNotFoundException, IOException {
    Process p = Runtime.getRuntime().exec(command);
    InputStream is = p.getInputStream();
    try {
        return org.apache.commons.io.IOUtils.toString(is);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.l2jfree.gameserver.util.DatabaseBackupManager.java

public static void makeBackup() {
    File f = new File(Config.DATAPACK_ROOT, Config.DATABASE_BACKUP_SAVE_PATH);
    if (!f.mkdirs() && !f.exists()) {
        _log.warn("Could not create folder " + f.getAbsolutePath());
        return;//from   w w  w.  j a  va2  s  . c  o m
    }

    _log.info("DatabaseBackupManager: backing up `" + Config.DATABASE_BACKUP_DATABASE_NAME + "`...");

    Process run = null;
    try {
        run = Runtime.getRuntime().exec("mysqldump" + " --user=" + Config.DATABASE_LOGIN + " --password="
                + Config.DATABASE_PASSWORD
                + " --compact --complete-insert --default-character-set=utf8 --extended-insert --lock-tables --quick --skip-triggers "
                + Config.DATABASE_BACKUP_DATABASE_NAME, null, new File(Config.DATABASE_BACKUP_MYSQLDUMP_PATH));
    } catch (Exception e) {
    } finally {
        if (run == null) {
            _log.warn("Could not execute mysqldump!");
            return;
        }
    }

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        Date time = new Date();

        File bf = new File(f, sdf.format(time) + (Config.DATABASE_BACKUP_COMPRESSION ? ".zip" : ".sql"));
        if (!bf.createNewFile())
            throw new IOException("Cannot create backup file: " + bf.getCanonicalPath());
        InputStream input = run.getInputStream();
        OutputStream out = new FileOutputStream(bf);
        if (Config.DATABASE_BACKUP_COMPRESSION) {
            ZipOutputStream dflt = new ZipOutputStream(out);
            dflt.setMethod(ZipOutputStream.DEFLATED);
            dflt.setLevel(Deflater.BEST_COMPRESSION);
            dflt.setComment("L2JFree Schema Backup Utility\r\n\r\nBackup date: "
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(time));
            dflt.putNextEntry(new ZipEntry(Config.DATABASE_BACKUP_DATABASE_NAME + ".sql"));
            out = dflt;
        }

        byte[] buf = new byte[4096];
        int written = 0;
        for (int read; (read = input.read(buf)) != -1;) {
            out.write(buf, 0, read);

            written += read;
        }
        input.close();
        out.close();

        if (written == 0) {
            bf.delete();
            BufferedReader br = new BufferedReader(new InputStreamReader(run.getErrorStream()));
            String line;
            while ((line = br.readLine()) != null)
                _log.warn("DatabaseBackupManager: " + line);
            br.close();
        } else
            _log.info("DatabaseBackupManager: Schema `" + Config.DATABASE_BACKUP_DATABASE_NAME
                    + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000
                    + " s.");

        run.waitFor();
    } catch (Exception e) {
        _log.warn("DatabaseBackupManager: Could not make backup: ", e);
    }
}

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  ww . 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();
    }
}