Alex’s Weblog

My personal blog

Calling an external executable program in a JAVA program

Today I was struggling to call an executable program inside a Java Program. I was used to do it using the C/C++. Searching on the internet I found two classes

Runtime and Process Class

A basic example

Process processo = Runtime.getRuntime().exec("java");
InputStream ips = processo.getInputStream();

Accordingly to Java documentation:

“Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.”

This class provides some information about the environment that the class is running over and also some features. The one that I was looking for is provide by the method exec that return to us a object from class Process citing the same source:

“…can be used to control the process and obtain information about it.”

It’s possible also to send arguments to the program called calling the method with the signature

exec(String[] cmdarray).

I hope this can be useful for someone. See ya!

Novembro 11, 2008 Publicado por alexpinheiro | Programming | | Sem comentários ainda