[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: execute an executable file from java

On Mon, 2 Jul 2001, Lan Vo wrote:

> Would someone please teach me how to call an exe file from java?  Is
> it possible to make a java program works with an exe in an endless
> loop, like inter-process communication?

It's pretty easy.. Just use Runtime.getRuntime().exec();

That'll return a Process object, for which you can get an InputStream
or OutputStream. (and Note that the exe's output will be an
InputStream as far as your program is concerned). 

Here's a simple example... The exec'ed command just prints hello.


- Michal
www.manifestation.com  www.sabren.net  www.linkwatcher.com  www.zike.net

import java.io.*;

public class Exec {
    public static void main(String[] args) { 
	Runtime run = Runtime.getRuntime();
	try {
	    Process python = run.exec("python -c\"print'hello'\"");
	    BufferedReader in = new BufferedReadeer
		( new InputStreamReader(python.getInputStream()) );
	    String line;
	    while ((line = in.readLine()) != null) {
	} catch (Exception e) {