Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?


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.

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.

like image 447
Marlo Guthrie Avatar asked Sep 17 '08 13:09

Marlo Guthrie


People also ask

Can I write C code in java?

It's not possible to run C source code at all unless you can find a C interpreter.

Does java compile in C?

very first Java compiler developed by Sun Microsystems was written in C, but now the class libraries are always written in Java (since they are intended to be run using the Java VM itself).

What is source code in java?

Source code is the set of instructions and statements written by a programmer using a computer programming language. This code is later translated into machine language by a compiler. The translated code is referred to as object code.


4 Answers

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.

like image 185
lindelof Avatar answered Oct 15 '22 09:10

lindelof


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.

Are there no JAVA equivelent Math functions?

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.

like image 37
TK. Avatar answered Oct 15 '22 09:10

TK.


I'd compile it and use 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).

I don't know about its performance or usability in your case but I'd give it a try.

like image 31
jb. Avatar answered Oct 15 '22 11:10

jb.


Are you sure you want to use the C library, even if it is that small?

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.

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.

like image 24
John Gardner Avatar answered Oct 15 '22 09:10

John Gardner