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,
without using javax.script . A list of these can be found at Wikipedia .
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
line you give is broken into strings by a simple StringTokenizer (see Breaking Strings Into
Words ) and passed on to the operating system's “execute a program” system call. As an ex-
ample, here is a simple program that uses exec() to run kwrite , a windowed text editor pro-
gram. [ 65 ] On Windows, you'd have to change the name to notepad or wordpad , possibly in-
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 ();
}
}
 
Search WWH ::




Custom Search