Java Reference
In-Depth Information
▪
Renjin (pronounced “R Engine”)
,
a fairly complete open source clone of the R statistics
package with the ability to scale to the cloud.
These are JVM-centric and some can be called directly from Java to script or vice versa,
Running an External Program from Java
Problem
You want to run an external program from within a Java program.
Solution
Use one of the
exec()
methods in the
java.lang.Runtime
class. Or set up a
Pro-
cessBuilder
and call its
start()
method.
Discussion
The
exec()
method in the
Runtime
class lets you run an external program. The command
ample, here is a simple program that uses
exec()
to run
kwrite
, a windowed text editor pro-
cluding the full pathname; for example,
c:/windows/notepad.exe
(you can also use back-
slashes, but be careful to double them because the backslash is special in Java strings):
public
public class
class
ExecDemoSimple
ExecDemoSimple
{
public
public static
static
void
void
main
(
String av
[])
throws
throws
Exception
{
// Run the "notepad" program or a similar editor
Process p
=
Runtime
.
getRuntime
().
exec
(
"kwrite"
);
p
.
waitFor
();
}
}