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 Windows. It's a library of mostly mathematical functions, so as far as I can tell it doesn't use anything that's platform-dependent, it's just very basic C code. Also, it's not that big, less than 5,000 lines.
我找到了我想在我的Java应用程序中使用的这个开源库。该库是用C语言编写的,是在Unix / Linux下开发的,我的应用程序将在Windows上运行。它是一个主要是数学函数的库,所以据我所知,它不使用任何与平台相关的东西,它只是非常基本的C代码。而且,它不是那么大,不到5000行。
What's the easiest way to use the library in my application? I know there's JNI, but that involves finding a compiler to compile the library under Windows, getting up-to-date with the JNI framework, writing the code, etc. Doable, but not that easy. Is there an easier way? Considering the small size of the library, I'm tempted to just translate it to Java. Are there any tools that can help with that?
在我的应用程序中使用库的最简单方法是什么?我知道有JNI,但这需要找到一个编译器来在Windows下编译库,获得JNI框架的最新版本,编写代码等等。但是可行,但不是那么容易。有更容易的方法吗?考虑到库的小尺寸,我很想把它翻译成Java。是否有任何工具可以帮助解决这个问题?
EDIT
I ended up translating the part of the library that I needed to Java. It's about 10% of the library so far, though it'll probably increase with time. C and Java are pretty similar, so it only took a few hours. The main difficulty is fixing the bugs that get introduced by mistakes in the translation.
我最终将我需要的库部分翻译成Java。到目前为止,它大约是图书馆的10%,尽管它可能会随着时间的推移而增加。 C和Java非常相似,所以只花了几个小时。主要的困难是修复因翻译错误而引入的错误。
Thank you everyone for your help. The proposed solutions all seemed interesting and I'll look into them when I need to link to larger libraries. For a small piece of C code, manual translation was the simplest solution.
感谢大家的帮助。所提出的解决方案似乎都很有趣,当我需要链接到更大的库时,我会研究它们。对于一小段C代码,手动翻译是最简单的解决方案。
7 个解决方案
#1
5
Your best bet is probably to grab a good c book (K&R: The C Progranmming language) a cup of tea and start translating! I would be skeptical about trusting a translation program, more often then not the best translator is yourself! If you do this one, then its done and you don't need to keep re-doing it. There might be some complications if the library is open source, you'll need to check the licence carefully about this. Another point to consider is that there is always going to be some element of risk and potential error in the translation, therefore it might be necessary to consider writing some tests to ensure that the translation is correct.
你最好的选择可能是拿一本好书(K&R:The C Progranmming语言)喝一杯茶然后开始翻译!我会对信任翻译程序持怀疑态度,更常见的不是最好的翻译是你自己!如果你这样做,那么它完成了,你不需要继续重做。如果库是开源的,可能会有一些复杂的问题,您需要仔细检查许可证。需要考虑的另一点是,翻译中总会存在一些风险因素和潜在错误,因此可能需要考虑编写一些测试以确保翻译是正确的。
Are there no JAVA equivelent Math functions?
是否没有JAVA等效数学函数?
As you yourself comment the JNI way is possible, as for a c compiler you could probably use 'Bloodshead Dev-c++' might work, but it is a lot of effort for ~5000 lines.
正如你自己评论JNI方式是可能的,对于c编译器你可能使用'Bloodshead Dev-c ++'可能会起作用,但对于~5000行来说需要付出很多努力。
#2
11
On the Java GNU Scientific Library project I used Swig to generate the JNI wrapper classes around the C libraries. Great tool, and can also generate wrapper code in several languages including Python. Highly recommended.
在Java GNU Scientific Library项目中,我使用Swig生成围绕C库的JNI包装类。伟大的工具,也可以生成包括Python在内的多种语言的包装代码。强烈推荐。
#3
4
I'd compile it and use JNA.
我编译它并使用JNA。
JNA (Java Native Access) is basically does in runtime what JNI at compile time and doesnt need any non-java code (not much java either).
JNA(Java Native Access)基本上是在运行时运行什么JNI在编译时并且不需要任何非Java代码(也不是很多java)。
I don't know about its performance or usability in your case but I'd give it a try.
我不知道你的性能或可用性,但我会试一试。
#4
1
Are you sure you want to use the C library, even if it is that small?
你确定要使用C库,即使它很小吗?
Once 64 bit gets a little more common, you'll need to start building/deploying both 32 bit and 64 bit versions of the library as well. And depending on what the C code is like, you may or may not need to update the code to make it build as 64 bit.
一旦64位变得更加普遍,您还需要开始构建/部署32位和64位版本的库。根据C代码的含义,您可能需要或不需要更新代码以使其构建为64位。
If the C library is simple, it may be easier to just port the C library to pure java and not have to deal with building/deploying a JNI library, the C library and the java code.
如果C库很简单,那么将C库移植到纯java可能更容易,而不必处理构建/部署JNI库,C库和Java代码。
#5
0
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。它是Windows,MacOS X和Linux的C编译器,可以将C代码编译为Java Byte Code(在Java虚拟机上运行的代码类型)。
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.
但是,它是商业的,每个许可证的费用为199美元。我怀疑这会为你带来回报;)我不知道有任何类似的免费编译器。
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?
OTOH,Java和C非常相似。您可以将C代码重构为Java(结构可以用具有公共实例变量的对象替换),并且指针操作通常可以转换为其他内容(例如,数组操作)。虽然我猜你不想通过5000行代码,不是吗?
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.
使用JNI会使代码平台依赖,但是如果您说它是独立于平台的C,则没有理由认为您的Java代码应该依赖于平台。 OTOH,取决于这些计算的成本,使用JNI实际上可能会为您带来性能提升,因为在原始数字运算吞吐量方面,C仍然可以在速度上超过Java。然而,JNI调用非常昂贵,因此如果计算只是一个非常简单,快速的计算,那么JNI调用本身可能需要比执行的计算一样长(甚至更长),在这种情况下使用JNI将不会给你什么,但会减慢关闭你的应用程序并导致内存开销。
#6
0
Indeed, JNA looks impressive, it requires less effort than directly using JNI. But in any case you'd lose the platform independence, and since you're probably only using a small part of it, you might consider translating what you actually need.
事实上,JNA看起来令人印象深刻,它比直接使用JNI需要更少的努力。但无论如何你都会失去平*立性,因为你可能只使用了它的一小部分,你可能会考虑翻译你真正需要的东西。
#7
-2
Have you tried using:
你尝试过使用过:
System.loadLibrary("mylibrary.dll");
Not sure if this will work with a pure C library but it's probably worth a shot. :)
不确定这是否适用于纯C库,但它可能值得一试。 :)
#1
5
Your best bet is probably to grab a good c book (K&R: The C Progranmming language) a cup of tea and start translating! I would be skeptical about trusting a translation program, more often then not the best translator is yourself! If you do this one, then its done and you don't need to keep re-doing it. There might be some complications if the library is open source, you'll need to check the licence carefully about this. Another point to consider is that there is always going to be some element of risk and potential error in the translation, therefore it might be necessary to consider writing some tests to ensure that the translation is correct.
你最好的选择可能是拿一本好书(K&R:The C Progranmming语言)喝一杯茶然后开始翻译!我会对信任翻译程序持怀疑态度,更常见的不是最好的翻译是你自己!如果你这样做,那么它完成了,你不需要继续重做。如果库是开源的,可能会有一些复杂的问题,您需要仔细检查许可证。需要考虑的另一点是,翻译中总会存在一些风险因素和潜在错误,因此可能需要考虑编写一些测试以确保翻译是正确的。
Are there no JAVA equivelent Math functions?
是否没有JAVA等效数学函数?
As you yourself comment the JNI way is possible, as for a c compiler you could probably use 'Bloodshead Dev-c++' might work, but it is a lot of effort for ~5000 lines.
正如你自己评论JNI方式是可能的,对于c编译器你可能使用'Bloodshead Dev-c ++'可能会起作用,但对于~5000行来说需要付出很多努力。
#2
11
On the Java GNU Scientific Library project I used Swig to generate the JNI wrapper classes around the C libraries. Great tool, and can also generate wrapper code in several languages including Python. Highly recommended.
在Java GNU Scientific Library项目中,我使用Swig生成围绕C库的JNI包装类。伟大的工具,也可以生成包括Python在内的多种语言的包装代码。强烈推荐。
#3
4
I'd compile it and use JNA.
我编译它并使用JNA。
JNA (Java Native Access) is basically does in runtime what JNI at compile time and doesnt need any non-java code (not much java either).
JNA(Java Native Access)基本上是在运行时运行什么JNI在编译时并且不需要任何非Java代码(也不是很多java)。
I don't know about its performance or usability in your case but I'd give it a try.
我不知道你的性能或可用性,但我会试一试。
#4
1
Are you sure you want to use the C library, even if it is that small?
你确定要使用C库,即使它很小吗?
Once 64 bit gets a little more common, you'll need to start building/deploying both 32 bit and 64 bit versions of the library as well. And depending on what the C code is like, you may or may not need to update the code to make it build as 64 bit.
一旦64位变得更加普遍,您还需要开始构建/部署32位和64位版本的库。根据C代码的含义,您可能需要或不需要更新代码以使其构建为64位。
If the C library is simple, it may be easier to just port the C library to pure java and not have to deal with building/deploying a JNI library, the C library and the java code.
如果C库很简单,那么将C库移植到纯java可能更容易,而不必处理构建/部署JNI库,C库和Java代码。
#5
0
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。它是Windows,MacOS X和Linux的C编译器,可以将C代码编译为Java Byte Code(在Java虚拟机上运行的代码类型)。
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.
但是,它是商业的,每个许可证的费用为199美元。我怀疑这会为你带来回报;)我不知道有任何类似的免费编译器。
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?
OTOH,Java和C非常相似。您可以将C代码重构为Java(结构可以用具有公共实例变量的对象替换),并且指针操作通常可以转换为其他内容(例如,数组操作)。虽然我猜你不想通过5000行代码,不是吗?
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.
使用JNI会使代码平台依赖,但是如果您说它是独立于平台的C,则没有理由认为您的Java代码应该依赖于平台。 OTOH,取决于这些计算的成本,使用JNI实际上可能会为您带来性能提升,因为在原始数字运算吞吐量方面,C仍然可以在速度上超过Java。然而,JNI调用非常昂贵,因此如果计算只是一个非常简单,快速的计算,那么JNI调用本身可能需要比执行的计算一样长(甚至更长),在这种情况下使用JNI将不会给你什么,但会减慢关闭你的应用程序并导致内存开销。
#6
0
Indeed, JNA looks impressive, it requires less effort than directly using JNI. But in any case you'd lose the platform independence, and since you're probably only using a small part of it, you might consider translating what you actually need.
事实上,JNA看起来令人印象深刻,它比直接使用JNI需要更少的努力。但无论如何你都会失去平*立性,因为你可能只使用了它的一小部分,你可能会考虑翻译你真正需要的东西。
#7
-2
Have you tried using:
你尝试过使用过:
System.loadLibrary("mylibrary.dll");
Not sure if this will work with a pure C library but it's probably worth a shot. :)
不确定这是否适用于纯C库,但它可能值得一试。 :)