DNB5brims
DNB5brims

Reputation: 30558

Is it possible to run C source code from Java?

Now I have some C source codes, I would like to use it in my java application. I need to execute the C source code, and get back the result to my java application. Instead of re-write all the C source code to java, how can I reuse the C's source code in my java application?

Upvotes: 11

Views: 28179

Answers (4)

AlexR
AlexR

Reputation: 115328

There are the following ways.

  1. JNI (see answer of @AurelioDeRosa +1)
  2. JNA
  3. If your C program can run as command line utility why not just to execute it using Runtime.getRuntime().exec() or ProcessBuilder?

Upvotes: 9

Maurice Perry
Maurice Perry

Reputation: 32831

Two options:

  1. Make a library accessible via JNI or JNA
  2. Make an executable, and call it with ProcessBuilder or Runtime.exec and capture the output streem if needed

Upvotes: 6

bilash.saha
bilash.saha

Reputation: 7296

Yes its possible Take a look at

Calling C Code from Java

Upvotes: 3

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22142

Take a look at Java Native Interface.

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

Upvotes: 14

Related Questions