Example usage for java.lang Process getErrorStream

List of usage examples for java.lang Process getErrorStream

Introduction

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

Prototype

public abstract InputStream getErrorStream();

Source Link

Document

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

Usage

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Force Align database for Automatic labels 
 * @throws Exception//from  w  ww .j  a v  a2 s.co  m
 */
private void hviteMultiplePronunciationAligning(String hmmNumber, String alignedMlf, boolean labOutput,
        String labDir, boolean full, String logfile, boolean cmp) throws Exception {

    String hvite = getProp(HTKDIR) + File.separator + "HVite"; // -A -D -V -T 1 "; // to add -A -D -V -T 1 in every function
    File htkFile = new File(hvite);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";

    // Virtual sp change_ phoneList should be a member? 
    // Without sp: 
    /*String phoneList = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phone2.list";*/

    // Whit sp:

    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

    String hmmDef = getProp(HTDIR) + File.separator + "hmm" + File.separator + hmmNumber + File.separator
            + "hmmdefs";
    String macros = getProp(HTDIR) + File.separator + "hmm" + File.separator + hmmNumber + File.separator
            + "macros";

    // Virtual sp change_ phoneMlf should be a member?

    // Without sp: 
    /*String phoneMlf = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phones2.mlf";*/
    // Whit sp:
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.words3.mlf";

    String phoneDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.words.dict";

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell

    //when no sp use (-m)!
    String cmd;

    String alignout, mOptioon, oOption;

    if (labOutput)
        alignout = "";
    else
        alignout = " -i " + alignedMlf;

    if (full) {
        if (labOutput)
            mOptioon = "";
        else
            mOptioon = " -m";

        cmd = "( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + " -C "
                + configFile + mOptioon + " -a -H " + macros + " -H " + hmmDef + alignout + " -t 250.0 -y lab"
                + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > " + logfile
                + "; exit )\n";
    } else {
        if (cmp)
            oOption = " -o TS";
        else
            oOption = " -o W";

        cmd = "( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + oOption + " -C "
                + configFile + " -m -a -H " + macros + " -H " + hmmDef + alignout + " -t 250.0 -y lab" + " -I "
                + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > " + logfile
                + "; exit )\n";
    }

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println(cmd);
    pw.println(cmd);

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:edu.ku.brc.af.core.db.MySQLBackupService.java

/**
 * @param databaseName/* w ww  .ja  v  a 2 s. c om*/
 * @param restoreFilePath
 * @param glassPane
 * @param completionMsgKey
 */
protected boolean doRestoreInBackground(final String databaseName, final String restoreFilePath,
        final SimpleGlassPane glassPane, final String completionMsgKey, final PropertyChangeListener pcl,
        final boolean doSynchronously) {
    AppPreferences remotePrefs = AppPreferences.getLocalPrefs();
    final String mysqlLoc = remotePrefs.get(MYSQL_LOC, getDefaultMySQLLoc());

    getNumberofTables();

    SynchronousWorker backupWorker = new SynchronousWorker() {
        long dspMegs = 0;
        long fileSize = 0;

        /* (non-Javadoc)
         * @see javax.swing.SwingWorker#doInBackground()
         */
        @Override
        protected Integer doInBackground() throws Exception {
            FileInputStream input = null;
            try {
                String userName = itUsername != null ? itUsername : DBConnection.getInstance().getUserName();
                String password = itPassword != null ? itPassword : DBConnection.getInstance().getPassword();
                String port = DatabaseDriverInfo.getDriver(DBConnection.getInstance().getDriverName())
                        .getPort();
                String server = DBConnection.getInstance().getServerName();

                String cmdLine = String.format("%s -u %s --password=%s --host=%s %s %s", mysqlLoc, userName,
                        password, server, (port != null ? ("--port=" + port) : ""), databaseName);
                Vector<String> args = new Vector<String>();
                args.add(mysqlLoc);
                args.add("--user=" + userName);
                args.add("--password=" + password);
                args.add("--host=" + server);
                if (port != null) {
                    args.add("--port=" + port);
                }
                args.add(databaseName);

                Process process = Runtime.getRuntime().exec(args.toArray(new String[0]));

                Thread.sleep(100);

                OutputStream out = process.getOutputStream();

                // wait as long it takes till the other process has prompted.
                try {
                    File inFile = new File(restoreFilePath);
                    fileSize = inFile.length();
                    //System.out.println(fileSize);

                    double oneMB = (1024.0 * 1024.0);
                    double threshold = fileSize < (oneMB * 4) ? 8192 * 8 : oneMB;
                    long totalBytes = 0;

                    dspMegs = 0;

                    input = new FileInputStream(inFile);
                    try {
                        byte[] bytes = new byte[8192 * 4];
                        do {
                            int numBytes = input.read(bytes, 0, bytes.length);

                            totalBytes += numBytes;
                            if (numBytes > 0) {
                                out.write(bytes, 0, numBytes);

                                long megs = (long) (totalBytes / threshold);
                                if (megs != dspMegs) {
                                    dspMegs = megs;
                                    firePropertyChange(MEGS, dspMegs, (int) ((100.0 * totalBytes) / fileSize));
                                }

                            } else {
                                break;
                            }
                        } while (true);
                    } finally {
                        input.close();
                    }
                } catch (IOException ex) {
                    edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                    //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(MySQLBackupService.class, ex);
                    ex.printStackTrace();
                    errorMsg = ex.toString();
                    UIRegistry.showLocalizedError("MySQLBackupService.EXCP_RS");

                } catch (Exception ex) {
                    ex.printStackTrace();
                    if (pcl != null) {
                        pcl.propertyChange(new PropertyChangeEvent(MySQLBackupService.this, ERROR, 0, 1));
                    }
                }

                setProgress(100);

                out.flush();
                out.close();

                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    //System.err.println(line);
                }

                in = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                StringBuilder sb = new StringBuilder();
                while ((line = in.readLine()) != null) {
                    if (line.startsWith("ERR")) {
                        sb.append(line);
                        sb.append("\n");
                    }
                }
                errorMsg = sb.toString();

            } catch (Exception ex) {
                ex.printStackTrace();
                errorMsg = ex.toString();
                if (pcl != null) {
                    pcl.propertyChange(new PropertyChangeEvent(MySQLBackupService.this, ERROR, 0, 1));
                }
            }

            return null;
        }

        @Override
        protected void done() {
            super.done();

            JStatusBar statusBar = UIRegistry.getStatusBar();
            if (statusBar != null) {
                statusBar.setProgressDone(STATUSBAR_NAME);
            }

            if (glassPane != null) {
                UIRegistry.clearSimpleGlassPaneMsg();
            }

            if (StringUtils.isNotEmpty(errorMsg)) {
                UIRegistry.showError(errorMsg);
            }

            if (statusBar != null) {
                statusBar.setText(UIRegistry.getLocalizedMessage(completionMsgKey, dspMegs));
            }

            if (pcl != null) {
                pcl.propertyChange(new PropertyChangeEvent(MySQLBackupService.this, DONE, 0, 1));
            }
        }
    };

    if (glassPane != null) {
        glassPane.setProgress(0);
    }

    backupWorker.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(final PropertyChangeEvent evt) {
            if (MEGS.equals(evt.getPropertyName()) && glassPane != null) {
                int value = (Integer) evt.getNewValue();

                if (value < 100) {
                    glassPane.setProgress((Integer) evt.getNewValue());
                } else {
                    glassPane.setProgress(100);
                }
            }
        }
    });

    if (doSynchronously) {
        return backupWorker.doWork();
    }

    backupWorker.execute();
    return true;
}

From source file:de.huberlin.cuneiform.compiler.local.LocalDispatcher.java

protected Set<JsonReportEntry> dispatch(Invocation invocation)
        throws IOException, InterruptedException, NotDerivableException, JSONException {

    File scriptFile;//from  w w w  .ja  v  a  2s . co m
    Process process;
    int exitValue;
    Set<JsonReportEntry> report;
    String line;
    String[] arg;
    String value;
    int i;
    StringBuffer buf;
    File location;
    File reportFile;
    StreamConsumer stdoutConsumer, errConsumer;
    ExecutorService executor;
    String signature;
    Path srcPath, destPath;
    File successMarker;

    if (invocation == null)
        throw new NullPointerException("Invocation must not be null.");

    if (!invocation.isReady())
        throw new RuntimeException("Cannot dispatch invocation that is not ready.");

    location = new File(buildDir.getAbsolutePath() + "/" + invocation.getSignature());
    successMarker = new File(location.getAbsolutePath() + "/" + SUCCESS_FILENAME);
    reportFile = new File(location.getAbsolutePath() + "/" + Invocation.REPORT_FILENAME);

    if (!successMarker.exists()) {

        if (location.exists())
            FileUtils.deleteDirectory(location);

        if (!location.mkdirs())
            throw new IOException("Could not create invocation location.");

        scriptFile = new File(location.getAbsolutePath() + "/" + SCRIPT_FILENAME);

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(scriptFile, false))) {

            // write away script
            writer.write(invocation.toScript());

        }

        scriptFile.setExecutable(true);

        for (String filename : invocation.getStageInList()) {

            if (filename.charAt(0) != '/' && filename.indexOf('_') >= 0) {

                signature = filename.substring(0, filename.indexOf('_'));

                srcPath = FileSystems.getDefault()
                        .getPath(buildDir.getAbsolutePath() + "/" + signature + "/" + filename);
                destPath = FileSystems.getDefault()
                        .getPath(buildDir.getAbsolutePath() + "/" + invocation.getSignature() + "/" + filename);
                Files.createSymbolicLink(destPath, srcPath);
            }
        }

        arg = new String[] { "/usr/bin/time", "-a", "-o",
                location.getAbsolutePath() + "/" + Invocation.REPORT_FILENAME, "-f",
                "{" + JsonReportEntry.ATT_TIMESTAMP + ":" + System.currentTimeMillis() + ","
                        + JsonReportEntry.ATT_RUNID + ":\"" + invocation.getDagId() + "\","
                        + JsonReportEntry.ATT_TASKID + ":" + invocation.getTaskNodeId() + ","
                        + JsonReportEntry.ATT_TASKNAME + ":\"" + invocation.getTaskName() + "\","
                        + JsonReportEntry.ATT_LANG + ":\"" + invocation.getLangLabel() + "\","
                        + JsonReportEntry.ATT_INVOCID + ":" + invocation.getSignature() + ","
                        + JsonReportEntry.ATT_KEY + ":\"" + JsonReportEntry.KEY_INVOC_TIME + "\","
                        + JsonReportEntry.ATT_VALUE + ":" + "{\"realTime\":%e,\"userTime\":%U,\"sysTime\":%S,"
                        + "\"maxResidentSetSize\":%M,\"avgResidentSetSize\":%t,"
                        + "\"avgDataSize\":%D,\"avgStackSize\":%p,\"avgTextSize\":%X,"
                        + "\"nMajPageFault\":%F,\"nMinPageFault\":%R,"
                        + "\"nSwapOutMainMem\":%W,\"nForcedContextSwitch\":%c,"
                        + "\"nWaitContextSwitch\":%w,\"nIoRead\":%I,\"nIoWrite\":%O,"
                        + "\"nSocketRead\":%r,\"nSocketWrite\":%s,\"nSignal\":%k}}",
                scriptFile.getAbsolutePath() };

        // run script
        process = Runtime.getRuntime().exec(arg, null, location);

        executor = Executors.newCachedThreadPool();

        stdoutConsumer = new StreamConsumer(process.getInputStream());
        executor.execute(stdoutConsumer);

        errConsumer = new StreamConsumer(process.getErrorStream());
        executor.execute(errConsumer);

        executor.shutdown();

        exitValue = process.waitFor();
        if (!executor.awaitTermination(4, TimeUnit.SECONDS))
            throw new RuntimeException("Consumer threads did not finish orderly.");

        try (BufferedWriter reportWriter = new BufferedWriter(new FileWriter(reportFile, true))) {

            if (exitValue != 0) {

                System.err.println("[script]");

                try (BufferedReader reader = new BufferedReader(new StringReader(invocation.toScript()))) {

                    i = 0;
                    while ((line = reader.readLine()) != null)
                        System.err.println(String.format("%02d  %s", ++i, line));
                }

                System.err.println("[out]");
                try (BufferedReader reader = new BufferedReader(
                        new StringReader(stdoutConsumer.getContent()))) {

                    while ((line = reader.readLine()) != null)
                        System.err.println(line);
                }

                System.err.println("[err]");
                try (BufferedReader reader = new BufferedReader(new StringReader(errConsumer.getContent()))) {

                    while ((line = reader.readLine()) != null)
                        System.err.println(line);
                }

                System.err.println("[end]");

                throw new RuntimeException("Invocation of task '" + invocation.getTaskName()
                        + "' with signature " + invocation.getSignature()
                        + " terminated with non-zero exit value. Exit value was " + exitValue + ".");
            }

            try (BufferedReader reader = new BufferedReader(new StringReader(stdoutConsumer.getContent()))) {

                buf = new StringBuffer();
                while ((line = reader.readLine()) != null)
                    buf.append(line.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\"")).append('\n');

                value = buf.toString();
                if (!value.isEmpty())

                    reportWriter.write(new JsonReportEntry(invocation, JsonReportEntry.KEY_INVOC_STDOUT, value)
                            .toString());
            }
            try (BufferedReader reader = new BufferedReader(new StringReader(errConsumer.getContent()))) {

                buf = new StringBuffer();
                while ((line = reader.readLine()) != null)
                    buf.append(line.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\"")).append('\n');

                value = buf.toString();
                if (!value.isEmpty())

                    reportWriter.write(new JsonReportEntry(invocation, JsonReportEntry.KEY_INVOC_STDERR, value)
                            .toString());
            }

        }
    }

    // gather report
    report = new HashSet<>();
    try (BufferedReader reader = new BufferedReader(new FileReader(reportFile))) {

        while ((line = reader.readLine()) != null) {

            line = line.trim();

            if (line.isEmpty())
                continue;

            report.add(new JsonReportEntry(line));
        }

    }

    invocation.evalReport(report);

    if (!successMarker.exists())
        if (!successMarker.createNewFile())
            throw new IOException("Could not create success marker.");

    return report;
}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Flat-start initialization for automatic labeling
 * @throws Exception//from w w w .j  a  v a2  s.  c o m
 */
private void herestTraining() throws Exception {

    String herest = getProp(HTKDIR) + File.separator + "HERest";
    String hhed = getProp(HTKDIR) + File.separator + "HHEd";

    File htkFile = new File(herest);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }

    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String hhedconf = getProp(HTDIR) + File.separator + "config" + File.separator + "sil.hed";

    String hhedconf_vp = getProp(HTDIR) + File.separator + "config" + File.separator + "sil_vp.hed";

    String trainList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";
    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.list";

    String hmmDir = getProp(HTDIR) + File.separator + "hmm" + File.separator;
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones.mlf";

    int BEST_ITERATION = MAX_ITERATIONS;
    int SP_ITERATION = -1;
    int VP_ITERATION = -1;
    int FA_ITERATION = -1;
    int change_mix_iteration = -1;
    for (int iteration = 1; iteration <= MAX_ITERATIONS; iteration++) {

        System.out.println("Iteration number: " + iteration);

        File hmmItDir = new File(hmmDir + "hmm" + iteration);
        if (!hmmItDir.exists())
            hmmItDir.mkdir();

        Runtime rtime = Runtime.getRuntime();
        //get a shell
        Process process = rtime.exec("/bin/bash");
        //get an output stream to write to the shell
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));

        if (PHASE_NUMBER == 0) {

            if (iteration == (SP_ITERATION + 1)) {

                phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones2.mlf";
                phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone2.list";

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);

                //now we enter in PHASE 1 
                PHASE_NUMBER = 1;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                continue;
            }

            // check epsilon_array  
            if (iteration > 2) {
                if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                        || iteration == MAX_SP_ITERATION) {
                    SP_ITERATION = iteration;
                    insertShortPause(iteration);
                    String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                    String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                    FileUtils.copy(oldMacro, newMacro);

                    // copy of logProbFrame_array in current iteration 
                    logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                    epsilon_array.add(100000000.0);
                    continue;
                }
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 1) {
            if (iteration == (VP_ITERATION + 1)) {
                phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones3.mlf";
                phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_vp + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_vp + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);

                //now we enter in PHASE 2
                PHASE_NUMBER = 2;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                continue;
            }

            // check epsilon_array  
            if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                    || iteration == MAX_VP_ITERATION) {
                VP_ITERATION = iteration;
                insertVirtualPauseThreeStates(iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 2) {
            if (iteration == (FA_ITERATION + 1)) {
                String logfile = "log_hviteMultiplePronunciationAligning_" + iteration + ".txt";
                String labDir = "'*'";//getProp(HTDIR)+File.separator+"lab";
                String alignedMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";
                hviteMultiplePronunciationAligning("hmm" + (iteration - 1), alignedMlf, false, labDir, true,
                        logfile, false);
                phoneMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";

                System.out.println("Copy hmm" + (iteration - 1) + " in " + "hmm" + iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);
                String oldHmmdefs = hmmDir + "hmm" + (iteration - 1) + File.separator + "hmmdefs";
                String newHmmdefs = hmmDir + "hmm" + iteration + File.separator + "hmmdefs";
                FileUtils.copy(oldHmmdefs, newHmmdefs);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                //now we enter in PHASE 3
                PHASE_NUMBER = 3;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);

                continue;
            }

            // check epsilon_array  
            if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                    || iteration == MAX_FA_ITERATION) {
                FA_ITERATION = iteration;

                System.out.println("Copy hmm" + (iteration - 1) + " in " + "hmm" + iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);
                String oldHmmdefs = hmmDir + "hmm" + (iteration - 1) + File.separator + "hmmdefs";
                String newHmmdefs = hmmDir + "hmm" + iteration + File.separator + "hmmdefs";
                FileUtils.copy(oldHmmdefs, newHmmdefs);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 3) {
            // check epsilon_array  
            // the following change_mix_iteration + 2 is used to allow more than one re-estimation after insertion of new mixture
            // Because just after the insertion the delta can be negative 

            if (((iteration != change_mix_iteration + 2)
                    && (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]))
                    || iteration == MAX_MIX_ITERATION) {

                System.out.println("Condition = true: " + "iteration=" + iteration + " change_mix_iteration="
                        + change_mix_iteration + " epsilon_array.get(iteration-2)="
                        + epsilon_array.get(iteration - 2) + " epsilon_PHASE[PHASE_NUMBER]="
                        + epsilon_PHASE[PHASE_NUMBER] + " MAX_MIX_ITERATION" + MAX_MIX_ITERATION);

                change_mix_iteration = iteration;
                MAX_MIX_ITERATION = -1;

                // Creating Increasing mixture config file dynamic iteration
                String hhedconf_mix = getProp(HTDIR) + File.separator + "config" + File.separator + "sil_mix_"
                        + iteration + ".hed";
                File file = new File(hhedconf_mix);
                PrintWriter hhed_conf_pw = new PrintWriter(new FileWriter(file));

                //MU 3 {*.state[2].mix}
                Boolean need_other_updates = false;
                for (int state = 0; state < num_mixtures_for_state.length; state++) {
                    if (current_number_of_mixtures[state] < num_mixtures_for_state[state]) {
                        int wanted_mix = current_number_of_mixtures[state] + 1;
                        int state_to_print = state + 2;
                        hhed_conf_pw.println("MU " + wanted_mix + "{*.state[" + state_to_print + "].mix}");

                        current_number_of_mixtures[state] = wanted_mix;

                        if (current_number_of_mixtures[state] < num_mixtures_for_state[state]) {
                            need_other_updates = true;
                        }
                    }
                }

                if (!need_other_updates) {
                    // copy of logProbFrame_array in current iteration 
                    //logProbFrame_array.add(logProbFrame_array.get(iteration-2));
                    //epsilon_array.add(100000000.0);
                    //now we enter in PHASE 3
                    PHASE_NUMBER = PHASE_NUMBER + 1;
                    System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                    String logfile = "log_hviteMultiplePronunciationAligning_" + iteration + ".txt";
                    String labDir = "'*'";//getProp(HTDIR)+File.separator+"lab";
                    String alignedMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";
                    hviteMultiplePronunciationAligning("hmm" + (iteration - 1), alignedMlf, false, labDir, true,
                            logfile, false);
                    phoneMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";

                    //continue;
                }

                hhed_conf_pw.flush();
                hhed_conf_pw.close();

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_mix + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_mix + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 4) {
            // check epsilon_array
            if (((iteration != change_mix_iteration + 2)
                    && (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]))
                    || iteration == MAX_ITERATIONS) {
                int last = iteration - 1;
                int previus_last = iteration - 2;

                System.out.println(
                        "Average log prob per frame has not beeen increased too much respect the previus iteration:");
                System.out.println("Average log prob per frame at last HREST iteration (" + last + ")-> "
                        + logProbFrame_array.get(iteration - 2));
                System.out.println("Average log prob per frame at previus HREST iteration (" + previus_last
                        + ")-> " + logProbFrame_array.get(iteration - 3));
                System.out.println("Delta -> " + epsilon_array.get(iteration - 2));
                System.out.println("Suggested Action -> stop the iterations.");

                if (logProbFrame_array.get(iteration - 3) > logProbFrame_array.get(iteration - 2)) {
                    BEST_ITERATION = iteration - 2;
                } else {
                    BEST_ITERATION = iteration - 1;
                }
                break;
            }
        }

        //Normal HEREST:
        System.out.println("( cd " + getProp(HTDIR) + "; " + herest + " " + HTK_SO + " -C " + configFile
                + " -I " + phoneMlf + " -t 250.0 150.0 1000.0" + " -S " + trainList + " -H " + hmmDir + "hmm"
                + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm" + (iteration - 1)
                + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration + " " + phoneList
                + " >> log_herestTraining_" + iteration + ".txt" + "; exit )\n");

        pw.println("( cd " + getProp(HTDIR) + "; " + herest + " " + HTK_SO + " -C " + configFile + " -I "
                + phoneMlf + " -t 250.0 150.0 1000.0" + " -S " + trainList + " -H " + hmmDir + "hmm"
                + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm" + (iteration - 1)
                + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration + " " + phoneList
                + " >> log_herestTraining_" + iteration + ".txt" + "; exit )\n");
        pw.flush();
        //shut down
        pw.close();
        process.waitFor();
        // check exit value
        if (process.exitValue() != 0) {
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            throw new MaryConfigurationException(errorReader.readLine());
        }

        // update average_log_prob_per_frame and deltas
        check_average_log_prob_per_frame(iteration);

        System.out.println("Delta average log prob per frame to respect previus iteration-> "
                + epsilon_array.get(iteration - 1));
        System.out.println("Current PHASE: " + PHASE_NUMBER);
        System.out.println("Current state and number of mixtures (for each phoneme): "
                + Arrays.toString(current_number_of_mixtures));

        System.out.println("---------------------------------------");
    }

    System.out.println("***********\n");
    System.out.println("BEST ITERATION: " + BEST_ITERATION);
    System.out.println("COPYNING BEST ITERATION FILES IN hmm-final directory");
    System.out.println("logProbFrame_array:" + logProbFrame_array.toString());

    System.out.println("epsilon_array:" + epsilon_array.toString());

    System.out.println("***********\n");

    String oldMacro = hmmDir + "hmm" + BEST_ITERATION + File.separator + "macros";
    String newMacro = hmmDir + "hmm-final" + File.separator + "macros";
    FileUtils.copy(oldMacro, newMacro);

    String oldHmmdefs = hmmDir + "hmm" + BEST_ITERATION + File.separator + "hmmdefs";
    String newHmmdefs = hmmDir + "hmm-final" + File.separator + "hmmdefs";
    FileUtils.copy(oldHmmdefs, newHmmdefs);

}

From source file:br.com.riselabs.cotonet.builder.commands.ExternalGitCommand.java

/**
 * OBS: this method returns {@code null} when calling '
 * {@code git reset --hard}'./*from   w w w. j  a va2s .c  o  m*/
 * 
 * @return
 * @throws IOException
 */
public List<ConflictChunk<CommandLineBlameResult>> call() throws BlameException {
    Runtime run = Runtime.getRuntime();
    Process pr = null;
    String cmd = null;
    String[] env = {};
    BufferedReader buf;
    List<ConflictChunk<CommandLineBlameResult>> conflicts = null;
    int exitCode;
    try {
        switch (type) {
        case RESET:
            cmd = "git reset --hard";
            pr = run.exec(cmd, env, file);
            break;

        case BLAME:
        default:
            cmd = "git blame -p --line-porcelain";
            env = new String[1];
            // we need this to disable the pager
            env[0] = "GIT_PAGER=cat";
            pr = run.exec(cmd + " " + file, env, file.getParentFile());
            // parse output
            buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            conflicts = new ArrayList<ConflictChunk<CommandLineBlameResult>>();

            final String CONFLICT_START = "<<<<<<<";
            final String CONFLICT_SEP = "=======";
            final String CONFLICT_END = ">>>>>>>";
            boolean addBlame = false;

            ConflictChunk<CommandLineBlameResult> conflict = new ConflictChunk<CommandLineBlameResult>(
                    file.getCanonicalPath());

            CommandLineBlameResult bResult;
            bResult = new CommandLineBlameResult(file.getCanonicalPath());
            Blame<CommandLineBlameResult> cBlame;
            cBlame = new Blame<CommandLineBlameResult>(scenario.getLeft(), bResult);
            List<String> block;
            while ((block = readPorcelainBlock(buf)) != null) {
                String commit = block.get(0).split(" ")[0];
                //   for (String line : block)
                //      System.out.println(line);

                Map<PKeys, String> data = getDataFromPorcelainBlock(block);

                String contentLine = data.get(PKeys.content);

                int n;
                if ((n = contentLine.trim().indexOf(" ")) == -1) {
                    // line without blank space
                    contentLine = contentLine.trim();
                } else {
                    contentLine = contentLine.trim().substring(0, n);
                }

                if (contentLine.equals(CONFLICT_START)) {
                    addBlame = true;
                    continue;
                } else if (contentLine.equals(CONFLICT_SEP)) {
                    addBlame = true;
                    cBlame.setRevision(scenario.getLeft());
                    conflict.setBase(scenario.getBase());
                    conflict.setLeft(cBlame);
                    bResult = new CommandLineBlameResult(file.getCanonicalPath());
                    cBlame = new Blame<CommandLineBlameResult>(scenario.getRight(), bResult);
                    continue;
                } else if (contentLine.equals(CONFLICT_END)) {
                    conflict.setRight(cBlame);
                    conflict.setLine(Integer.valueOf(data.get(PKeys.linenumber)));
                    conflicts.add(conflict);
                    addBlame = false;

                    bResult = new CommandLineBlameResult(file.getCanonicalPath());
                    cBlame = new Blame<CommandLineBlameResult>(scenario.getLeft(), bResult);

                    //@gustavo added this line
                    conflict = new ConflictChunk<CommandLineBlameResult>(file.getCanonicalPath());

                } else if (addBlame) {
                    // we are in one of the conflicting chunks
                    Integer linenumber = Integer.valueOf(data.get(PKeys.linenumber));
                    contentLine = data.get(PKeys.content);
                    String name = data.get(PKeys.authorname);
                    String email = data.get(PKeys.authormail);
                    DeveloperNode dev = new DeveloperNode(name, email);
                    conflict.setLine(linenumber);
                    bResult.addLineAuthor(linenumber, dev);
                    bResult.addLineContent(linenumber, contentLine);
                    bResult.addLineCommit(linenumber, commit);
                    continue;
                }
            }

            buf.close();
            break;
        }

        /*
         * already finished to execute the process. now, we should process
         * the error output.
         */
        buf = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

        String stdErr = IOUtils.toString(pr.getErrorStream(), StandardCharsets.UTF_8).trim();

        IOUtils.closeQuietly(pr.getInputStream());
        IOUtils.closeQuietly(pr.getErrorStream());
        IOUtils.closeQuietly(pr.getOutputStream());

        exitCode = pr.waitFor();

        buf.close();
        if (!stdErr.isEmpty()) {
            Logger.log(String.format("Execution of '%s' returned standard error output:%n%s", cmd, stdErr));
            throw new RuntimeException(
                    String.format("Error on external call with exit code %d", pr.exitValue()));
        }
    } catch (IOException io) {
        try {
            throw new BlameException(file.getCanonicalPath(), "IO Exception", io);
        } catch (IOException e) {
        }
    } catch (InterruptedException ie) {
        // waitFor() exception
        exitCode = 666;
        try {
            throw new BlameException(file.getCanonicalPath(), String.format(
                    "Interrupted while waiting for '%s' to finish. Error code: '%s'", cmd, exitCode), ie);
        } catch (IOException io) {
        }
    } catch (RuntimeException re) {
        try {
            throw new BlameException(file.getCanonicalPath(), "Runtime Exception", re);
        } catch (IOException e) {
        }
    } finally {
        run.freeMemory();
    }
    pr.destroyForcibly();
    return conflicts;
}

From source file:com.cloud.hypervisor.xenserver.resource.CitrixResourceBase.java

public boolean createVmdataFiles(final String vmName, final List<String[]> vmDataList,
        final String configDriveLabel) {

    // add vm iso to the isolibrary
    final String isoPath = "/tmp/" + vmName + "/configDrive/";
    final String configDriveName = "cloudstack/";

    //create folder for the VM
    //Remove the folder before creating it.

    try {//from   w  w  w.  j a  va 2 s  . com
        deleteLocalFolder("/tmp/" + isoPath);
    } catch (final IOException e) {
        s_logger.debug("Failed to delete the exiting config drive for vm " + vmName + " " + e.getMessage());
    } catch (final Exception e) {
        s_logger.debug("Failed to delete the exiting config drive for vm " + vmName + " " + e.getMessage());
    }

    if (vmDataList != null) {
        for (final String[] item : vmDataList) {
            final String dataType = item[0];
            final String fileName = item[1];
            final String content = item[2];

            // create file with content in folder

            if (dataType != null && !dataType.isEmpty()) {
                //create folder
                final String folder = isoPath + configDriveName + dataType;
                if (folder != null && !folder.isEmpty()) {
                    final File dir = new File(folder);
                    final boolean result = true;

                    try {
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                    } catch (final SecurityException ex) {
                        s_logger.debug("Failed to create dir " + ex.getMessage());
                        return false;
                    }

                    if (result && content != null && !content.isEmpty()) {
                        File file = new File(folder + "/" + fileName + ".txt");
                        try (OutputStreamWriter fw = new OutputStreamWriter(
                                new FileOutputStream(file.getAbsoluteFile()), "UTF-8");
                                BufferedWriter bw = new BufferedWriter(fw);) {
                            bw.write(content);
                            s_logger.debug("created file: " + file + " in folder:" + folder);
                        } catch (final IOException ex) {
                            s_logger.debug("Failed to create file " + ex.getMessage());
                            return false;
                        }
                    }
                }
            }
        }
        s_logger.debug("Created the vm data in " + isoPath);
    }

    String s = null;
    try {

        final String cmd = "mkisofs -iso-level 3 -V " + configDriveLabel + " -o " + isoPath + vmName + ".iso "
                + isoPath;
        final Process p = Runtime.getRuntime().exec(cmd);

        final BufferedReader stdInput = new BufferedReader(
                new InputStreamReader(p.getInputStream(), Charset.defaultCharset()));

        final BufferedReader stdError = new BufferedReader(
                new InputStreamReader(p.getErrorStream(), Charset.defaultCharset()));

        // read the output from the command
        while ((s = stdInput.readLine()) != null) {
            s_logger.debug(s);
        }

        // read any errors from the attempted command
        while ((s = stdError.readLine()) != null) {
            s_logger.debug(s);
        }
        s_logger.debug(" Created config drive ISO using the command " + cmd + " in the host " + _host.getIp());
    } catch (final IOException e) {
        s_logger.debug(e.getMessage());
        return false;
    }

    return true;
}

From source file:StreamFlusher.java

public Object visit(ASTdraw_statement node, Object data) {
    // one daughter:  regexp

    // KRB: does this make any sense outside of the GUI???
    // KRB: review this whole method

    node.jjtGetChild(0).jjtAccept(this, data);
    // Should leave an Fst object on the stack (see Fst.java)
    Fst fst = (Fst) (stack.pop());/*from  w w  w . j  a va 2s .co m*/

    // magic numbers for now; limits on the size of a network
    // that will be drawn
    long stateLimit = 400L;
    long arcLimit = 400L;

    long nstates = lib.NumStates(fst);
    long narcs = lib.NumArcs(fst);

    if (nstates > stateLimit) {
        // don't try to draw it
        outputInterpMessage("// Fst contains over " + stateLimit
                + " states, which is generally too much for the dot application to handle.", data);

        return data;
    }

    if (narcs > arcLimit) {
        // don't try to draw it
        outputInterpMessage("// Fst contains over " + arcLimit
                + " arcs, which is generally too much for the dot application to handle.", data);

        return data;
    }

    String userHomeDir = System.getProperty("user.home");
    // to find temp files like ~/.kleene/tmp/last.dot
    // and ~/.kleene/prefs/prefs.xml

    String osName = getOsName();
    // defaults
    String tmpdir = "";
    String prefsPath = "";
    String dotSrcPath = "";
    String slashSep = "/";

    if (osName.equals("windows")) {
        slashSep = "\\";
        // only really needed for command shell (cmd /c), 
        //      else can use "/"
    }

    StringBuilder sbhex = new StringBuilder();
    StringBuilder sb = new StringBuilder();
    getSigmaStrings(fst, sbhex, sb);

    // On Linux and OS X, the basic Kleene directory is ~/.kleene
    // On Windows, this maps to C:\Documents and Settings\
    //      \<username>\.kleene
    tmpdir = userHomeDir + slashSep + ".kleene" + slashSep + "tmp";
    dotSrcPath = tmpdir + slashSep + "last.dot";
    prefsPath = userHomeDir + slashSep + ".kleene" + slashSep + "prefs" + slashSep + "prefs.xml";

    // an FstDotWriter object knows how to write a GraphViz .dot source file (to a specified
    // file; here written to last.lot in the user's tmp/ directory)
    FstDotWriter fstDotWriter = new FstDotWriter(symmap, new File(dotSrcPath), sb.toString(), "UTF-8");

    // call Fst2dot traverses an OpenFst Fst directly and generates 
    //   dot code (by making callbacks to methods in the Java fstDotWriter)
    lib.Fst2dot(fst, fstDotWriter);
    // we should now have tmp/last.dot  (a GraphViz dot source file
    // describing a network diagram)

    // If the osName is "osx" and a native Graphviz.app is installed
    // in /Applications, then things are simple.  Just call Graphviz directly
    // on the .dot source file.  "open -a Graphviz /path/to/last.lot"
    // No need to generate PostScript and then call a viewer to see it.

    File nativeGraphviz = new File("/Applications/Graphviz.app");
    if (osName.equals("osx") && nativeGraphviz.exists()) {
        try {
            Process proc = Runtime.getRuntime().exec("open -a Graphviz " + dotSrcPath);
            try {
                if (proc.waitFor() != 0) {
                    System.err.println("Problem calling native OS X GraphViz: exit value " + proc.exitValue());
                }
            } catch (InterruptedException e) {
                System.err.println(e);
            } finally {
            }
        } catch (Exception e) {
            System.err.println(e);
        }
    } else {
        // Need to do it the hard way.
        //
        // Take the .dot source file and call 'dot' to generate a graphics file, e.g. .ps
        // Then take the graphics file and call a viewer application
        // The location of the 'dot' application, the graphics format, and the view
        // application are specified in the user-specific pref.xml file

        // Access the user-specific prefs.xml file
        // type Document is a Java object representing an XML document (typically
        // read from an XML file into memory)
        Document doc = null;
        try {
            doc = parseXMLPrefs(prefsPath); // parse the user's prefs/prefs.xml
        } catch (Exception e) {
            // KRB:  review this
            System.out.println("Problem reading ~/.kleene/prefs/prefs.xml");
            e.printStackTrace();
        }

        // Navigate to platform-specific and user-specific dot, format, 
        //   viewer elmts in the prefs.xml file

        // get the path to the "dot" application
        String dotpath = getPref(doc, "dot/dotpath");

        // get the file format the dot should produce, e.g. ps or pdf
        String dotflag = getPref(doc, "dot/dotflag");

        // get the path to the viewer application
        String dotview = getPref(doc, "dot/viewer");

        // Trouble with generating/displaying PDF directly; 
        // If you generate .ps and 'open' it, the orientation=landscape
        // and center="true" are reflected correctly in the display (the
        // ps is converted automatically to pdf)
        // But if you generate the PDF file directly and 'open' it, the
        // orientation is wrong and the centering command is ignored.
        // PostScript seems more reliable right now.

        // ****************** Call 'dot' from Java **********************

        // construct the 'dot' command string to be launched by ProcessBuilder

        // Command shell prefix needed for ProcessBuilder is opsys-specific.
        String cmdShell, cmdShellOpts;
        if (osName.equals("windows")) {
            cmdShell = "cmd";
            cmdShellOpts = "/c";
        } else {
            // for Linux and OS X (valued of osName will be "osx")
            cmdShell = "/bin/sh";
            cmdShellOpts = "-c";
        }

        // Use doublequotes to support filenames with embedded spaces.
        // Initial blank prevents undesired doublequote removal by 
        //      Windows cmd.exe (see "cmd /?").
        String cmd = " \"" + dotpath + "\"" + " -T" + dotflag + " \"" + tmpdir + slashSep + "last.dot\"" + " > "
                + "\"" + tmpdir + slashSep + "last." + dotflag + "\"";

        // calling 'dot' from Java, from the .dot source file,
        // it should generate a graphics file, e.g. .ps (PostScript)

        try {
            ProcessBuilder pb = new ProcessBuilder(cmdShell, cmdShellOpts, cmd);
            Process p = pb.start();

            StreamFlusher errorFlusher = new StreamFlusher(p.getErrorStream(), "ERROR");
            StreamFlusher outputFlusher = new StreamFlusher(p.getInputStream(), "OUTPUT");

            errorFlusher.start();
            outputFlusher.start();

            int exitVal = p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // ******************* Now launch the viewer app from Java

        //KRB: putting double quotes around dotview currently 
        //   works for Linux, at least
        // with the current default dotview string:  /usr/bin/kghostview
        // which doesn't contain command-line options

        if (osName.equals("osx")) {
            // KRB: putting double quotes around dotview breaks 
            //   drawing for OS X,
            // where the dotview string is
            // /usr/bin/open -a /Applications/Preview.app/Contents/MacOS/Preview
            // (having three fields and two spaces)
            cmd = dotview + " \"" + tmpdir + slashSep + "last." + dotflag + "\"";
        } else {
            // Phil: fix for Windows (and seems to work for Linux)
            // Use doublequotes to support filenames with embedded spaces.
            // Initial blank prevents undesired doublequote removal by Windows cmd.exe (see "cmd /?").
            cmd = " \"" + dotview + "\" \"" + tmpdir + slashSep + "last." + dotflag + "\"";
        }

        //  launching the viewer on the ps, pdf (or whatever) file generated by 'dot'

        try {
            ProcessBuilder pb = new ProcessBuilder(cmdShell, cmdShellOpts, cmd);
            Process p = pb.start();

            StreamFlusher errorFlusher = new StreamFlusher(p.getErrorStream(), "ERROR");
            StreamFlusher outputFlusher = new StreamFlusher(p.getInputStream(), "OUTPUT");

            errorFlusher.start();
            outputFlusher.start();

            // if active, this stmt causes the viewer window to be 'modal', causing
            // Kleene to suspend operations until the viewer is closed
            //int exitVal = p.waitFor() ;
        } catch (Exception e) {
            e.printStackTrace();
        }

        // need to drain stdout stderr and inputStream in separate threads?
    }

    return data;
}

From source file:com.clark.func.Functions.java

/**
 * Performs the os command.//w w  w.  ja v a2  s .com
 * 
 * @param cmdAttribs
 *            the command line parameters
 * @param max
 *            The maximum limit for the lines returned
 * @param timeout
 *            The timout amount in milliseconds or no timeout if the value
 *            is zero or less
 * @return the parsed data
 * @throws IOException
 *             if an error occurs
 */
static List<String> performCommand(String[] cmdAttribs, int max, long timeout) throws IOException {
    // this method does what it can to avoid the 'Too many open files' error
    // based on trial and error and these links:
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4801027
    // http://forum.java.sun.com/thread.jspa?threadID=533029&messageID=2572018
    // however, its still not perfect as the JDK support is so poor
    // (see commond-exec or ant for a better multi-threaded multi-os
    // solution)

    List<String> lines = new ArrayList<String>(20);
    Process proc = null;
    InputStream in = null;
    OutputStream out = null;
    InputStream err = null;
    BufferedReader inr = null;
    try {

        Thread monitor = ThreadMonitor.start(timeout);

        proc = openProcess(cmdAttribs);
        in = proc.getInputStream();
        out = proc.getOutputStream();
        err = proc.getErrorStream();
        inr = new BufferedReader(new InputStreamReader(in));
        String line = inr.readLine();
        while (line != null && lines.size() < max) {
            line = line.toLowerCase(Locale.ENGLISH).trim();
            lines.add(line);
            line = inr.readLine();
        }

        proc.waitFor();

        ThreadMonitor.stop(monitor);

        if (proc.exitValue() != 0) {
            // os command problem, throw exception
            throw new IOException("Command line returned OS error code '" + proc.exitValue() + "' for command "
                    + Arrays.asList(cmdAttribs));
        }
        if (lines.size() == 0) {
            // unknown problem, throw exception
            throw new IOException(
                    "Command line did not return any info " + "for command " + Arrays.asList(cmdAttribs));
        }
        return lines;

    } catch (InterruptedException ex) {
        throw new IOExceptionWithCause("Command line threw an InterruptedException " + "for command "
                + Arrays.asList(cmdAttribs) + " timeout=" + timeout, ex);
    } finally {
        closeQuietly(in);
        closeQuietly(out);
        closeQuietly(err);
        closeQuietly(inr);
        if (proc != null) {
            proc.destroy();
        }
    }
}