What's the easiest way to use C source code in a Java application?

前端 未结 7 1977
野的像风
野的像风 2021-01-12 00:21

I found this open-source library that I want to use in my Java application. The library is written in C and was developed under Unix/Linux, and my application will run on Wi

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 00:44

    Well, there is AMPC. It is a C compiler for Windows, MacOS X and Linux, that can compile C code into Java Byte Code (the kind of code, that runs on a Java virtual machine).

    AMPC

    However, it is commercial and costs $199 per license. I doubt that pays off for you ;) I don't know of any free compiler like that.

    OTOH, Java and C are pretty similar. You could probably refactor the C Code to Java (structs can be replaced with objects with public instance variables) and pointer operations can usually be translated to something else (array operations for example). Though I guess you don't want to go through 5,000 lines of code, do you?

    Using JNI makes the code platform dependent, however if you say it is platform independent C, there is no reason why your Java code should be platform dependent. OTOH, depending on how costly these calculations are, using JNI might actually buy you a performance gain, as when it comes to raw number crunching throughput, C can still beat Java in speed. However JNI calls are very costly, so if the calculation is just a very simple, quick calculation, the JNI call itself might take equally long (or even longer) than the calculation performed, in which case using JNI will buy you nothing, but slowing down your app and causing memory overhead.

提交回复
热议问题