Git Product home page Git Product logo

java-start-process's Introduction

java-start-process

Important This project has moved to javadelight/delight-java-start-process Important

Wrapper to make work with Runtime.getRuntime() easier and less error-prone.

Why

Starting another program from within a Java application is easy to do. Just provide the path to the application to the method Runtime.exec() and the JVM will launch the specified application. However, as with so many supposedly simple tasks in software development, the devil lies in the detail. It is all too easy to introduce bugs around a call to Runtime.exec(), especially when one wants to pass parameters or read the output of the launched application.

This library is designed to make the task of starting applications from Java programs easier.

Usage

  1. Link the project as Maven dependency or as JAR to your project (or just download and copy&paste the packages).
  2. Start external process in your Java application.

Starting a process synchronously

Spawn.runCommand(new String[] { "/bin/bash", "-c",
                "vmstat -s -S M | egrep -ie 'memory|swap' | grep 'free memory'" },
                        null);

Starting a process asynchronously

final String cmd = "C:\\Windows\\System32\\NETSTAT.exe -ab";
final File folder = new File("C:\\Windows\\System32\\");

Spawn.startProcess(cmd, folder, new ProcessListener() {

	@Override
	public void onOutputLine(final String line) {
		System.out.println(line);
	}

	@Override
	public void onOutputClosed() {
		System.out.println("process completed");
	}

	@Override
	public void onErrorLine(final String line) {

	}

	@Override
	public void onError(final Throwable t) {
		throw new RuntimeException(t);
	}
});

Thread.sleep(600);

Maven Dependency

Artifact

<groupId>de.mxro.process</groupId>
<artifactId>java-start-process</artifactId>
<version>[0.0.1,)</version>

Repository

<repositories>
    <repository>
        <id>onedb Releases</id>
        <url>http://dl.dropbox.com/u/957046/onedb/mvn-releases</url>
    </repository>
</repositories>

Resources

When Runtime.exec() won't

Executing a Command Line Executable From Java

Five Common java.lang.Process Pitfalls

Apache Commons Exec

SO: Java Process with Input/Output Stream

SO: Starting a process in Java?

java-start-process's People

Contributors

mxro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.