You can use Runtime.exec() method to invoke a program from within a running Java application. Runtime.exec() also allows you to perform operations related to the program, such as control the program's standard input and output, wait until it completes execution, and get its exit status.

Here's a simple C application that illustrates these features. This C program will be called from Java:

#include <stdio.h>int main(){printf("testing\n");return0;}

This application writes a string "testing" to standard output, and then terminates with an exit status of 0. To execute this simple program within a Java application, compile the C application:

Compilation

$ cc test.c -o test

Then invoke the C program using this Java code:

Code listing 10.2: Invoking C programs.

importjava.io.InputStream;

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

importjava.io.IOException;

importjava.io.InterruptedException;

importjava.io.Process;

importjava.io.Runtime;

importjava.util.ArrayList;

publicclass ExecDemo {

publicstaticString[] runCommand(String cmd)throwsIOException{

// --- set up list to capture command output lines ---

ArrayList list = newArrayList();

// --- start command running

Process proc = Runtime.getRuntime().exec(cmd);

// --- get command's output stream and

// put a buffered reader input stream on it ---

InputStream istr = proc.getInputStream();

BufferedReader br = newBufferedReader(newInputStreamReader(istr));

// --- read output lines from command

String str;

while((str = br.readLine())!= null){

list.add(str);

}

// wait for command to terminate

try{

proc.waitFor();

}

catch(InterruptedException e){

System.err.println("process was interrupted");

}

// check its exit value

if(proc.exitValue()!= 0){

System.err.println("exit value was non-zero");

}

// close stream

br.close();

// return list of strings to caller

return(String[])list.toArray(newString[0]);

}

publicstaticvoid main(String args[])throwsIOException{

try{

// run a command

String outlist[] = runCommand("test");

// display its output

for(int i = 0; i < outlist.length; i++)

System.out.println(outlist[i]);

}

catch(IOException e){

System.err.println(e);

}

}

}

The demo calls a method runCommand to actually run the program.

Code section 10.1: Running a command.

String outlist[] = runCommand("test");

This method hooks an input stream to the program's output stream, so that it can read the program's output, and save it into a list of strings.

Tools exist to aid the migration of existing projects from C to Java. In general, automated translator tools fall into one of two distinct kinds:

One kind converts C code to Java byte code. It is basically a compiler that creates byte code. It has the same steps as any other C compiler. See also C to Java JVM compilers.

The other kind translates C code to Java source code. This type is more complicated and uses various syntax rules to create readable Java source code. This option is best for those who want to move their C code to Java and stay in Java.