简体   繁体   English

是否可以从Java运行C源代码?

[英]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. 现在我有一些C源代码,我想在我的java应用程序中使用它。 I need to execute the C source code, and get back the result to my java application. 我需要执行C源代码,并将结果返回到我的java应用程序。 Instead of re-write all the C source code to java, how can I reuse the C's source code in my java application? 而不是将所有C源代码重写到java,我如何在我的Java应用程序中重用C的源代码?

Take a look at Java Native Interface . 看看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. Java Native Interface(JNI)是一个编程框架,它使在Java虚拟机(JVM)中运行的Java代码能够调用并由本机应用程序(特定于硬件和操作系统平台的程序)和用其他语言编写的库调用例如C,C ++和汇编。

There are the following ways. 有以下几种方式。

  1. JNI (see answer of @AurelioDeRosa +1) JNI(见@AurelioDeRosa +1的回答)
  2. JNA JNA
  3. If your C program can run as command line utility why not just to execute it using Runtime.getRuntime().exec() or ProcessBuilder? 如果您的C程序可以作为命令行实用程序运行,为什么不只是使用Runtime.getRuntime()。exec()或ProcessBuilder执行它?

Two options: 两种选择:

  1. Make a library accessible via JNI or JNA 通过JNI或JNA访问库
  2. Make an executable, and call it with ProcessBuilder or Runtime.exec and capture the output streem if needed 创建一个可执行文件,并使用ProcessBuilder或Runtime.exec调用它,并在需要时捕获输出streem

Yes its possible Take a look at 是的可能看一看

Calling C Code from Java 从Java调用C代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM