Example usage for java.lang Process waitFor

List of usage examples for java.lang Process waitFor

Introduction

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

Prototype

public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException 

Source Link

Document

Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated, or the specified waiting time elapses.

Usage

From source file:com.diversityarrays.kdxplore.trialdesign.RscriptFinderPanel.java

private void doCheckScriptPath() {

    String scriptPath = scriptPathField.getText().trim();

    BackgroundTask<Either<String, String>, Void> task = new BackgroundTask<Either<String, String>, Void>(
            "Checking...", true) {
        @Override//from   www.  jav a 2  s.c  o m
        public Either<String, String> generateResult(Closure<Void> arg0) throws Exception {

            ProcessBuilder findRScript = new ProcessBuilder(scriptPath, "--version");

            Process p = findRScript.start();

            while (!p.waitFor(1000, TimeUnit.MILLISECONDS)) {
                if (backgroundRunner.isCancelRequested()) {
                    p.destroy();
                    throw new CancellationException();
                }
            }

            if (0 == p.exitValue()) {
                String output = Algorithms.readContent(null, p.getInputStream());
                versionNumber = Algorithms.readContent(null, p.getErrorStream());
                return Either.right(output);
            }

            errorOutput = Algorithms.readContent("Error Output:", p.getErrorStream());
            if (errorOutput.isEmpty()) {
                errorOutput = "No error output available";
                return Either.left(errorOutput);
            }
            return Either.left(errorOutput);
        }

        @Override
        public void onException(Throwable t) {
            onScriptPathChecked.accept(Either.left(t));
        }

        @Override
        public void onCancel(CancellationException ce) {
            onScriptPathChecked.accept(Either.left(ce));
        }

        @Override
        public void onTaskComplete(Either<String, String> either) {
            if (either.isLeft()) {
                MsgBox.error(RscriptFinderPanel.this, either.left(), "Error Output");
            } else {
                TrialDesignPreferences.getInstance().setRscriptPath(scriptPath);
                onScriptPathChecked.accept(Either.right(scriptPath));
                checkOutput = either.right();
            }
        }
    };

    backgroundRunner.runBackgroundTask(task);
}

From source file:org.fcrepo.importexport.integration.ExecutableJarIT.java

@Test
public void testJarSanity() throws IOException, InterruptedException {
    // Run the executable jar with no arguments
    final Process process = startJarProcess();

    // Verify it ran
    assertTrue("Process did not exit before timeout!", process.waitFor(TIMEOUT_SECONDS, TimeUnit.SECONDS));
    assertEquals("Did not exit with success status!", 0, process.exitValue());
}