Invoking C
- 25% developed as of Dec 19, 2012 Networking
- 25% developed as of Dec 19, 2012 Database programming
- 25% developed as of Jan 30, 2016 Regular Expressions
- 25% developed as of Dec 19, 2012 Libraries, extensions and frameworks
- 25% developed as of Dec 19, 2012 3D programming
- 75% developed as of Fev 27, 2013 Java Native Interface
- 75% developed as of Nov 20, 2013 Invoking C
- 50% developed as of Nov 20, 2013 Byte Code
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> intmain(){ 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:
$ cc test.c -o test
Then invoke the C program using this Java code:
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(Stringcmd)throwsIOException{ // --- set up list to capture command output lines --- ArrayListlist=newArrayList(); // --- start command running Processproc=Runtime.getRuntime().exec(cmd); // --- get command's output stream and // put a buffered reader input stream on it --- InputStreamistr=proc.getInputStream(); BufferedReaderbr=newBufferedReader(newInputStreamReader(istr)); // --- read output lines from command Stringstr; while((str=br.readLine())!=null){ list.add(str); } // wait for command to terminate try{ proc.waitFor(); } catch(InterruptedExceptione){ 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]); } publicstaticvoidmain(Stringargs[])throwsIOException{ try{ // run a command Stringoutlist[]=runCommand("test"); // display its output for(inti=0;i<outlist.length;i++) System.out.println(outlist[i]); } catch(IOExceptione){ System.err.println(e); } } }
The demo calls a method runCommand
to actually run the program.
Stringoutlist[]=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.
InputStreamistr=proc.getInputStream(); BufferedReaderbr=newBufferedReader(newInputStreamReader(istr)); Stringstr; while((str=br.readLine())!=null){ list.add(str); }
Migrating C to Java
[edit | edit source ]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.
To do:
Add some examples.