Is it possible to run C source code from Java?

Now I have some C source code, I would like to use it in my java application. I need to execute the C source code and return the result to my Java application. Instead of rewriting all of the C source code in java, how can I reuse the C source code in my Java application?

+6
source share
4 answers

Check out the Java Native Interface .

Java Native Interface (JNI) is a programming environment that allows Java code running on the Java Virtual Machine (JVM) to be called and called by native applications (programs specific to the hardware and operating system) and libraries written in other languages, such as C, C ++ and assembly.

+9
source

The following methods exist.

  • JNI (see @AurelioDeRosa +1 answer)
  • JNA
  • If your C program can run as a command line utility, why not just execute it with Runtime.getRuntime (). exec () or ProcessBuilder?
+7
source

Two options:

  • Make library accessible via JNI or JNA
  • Make an executable file and call it using ProcessBuilder or Runtime.exec and, if necessary, write the output file
+4
source

Yes, you can take a look at it

Call C code from Java

+2
source

Source: https://habr.com/ru/post/899902/


All Articles