Calling C code in Java
Hi,
I've implemented a program that performs some image processing algorithms using C. Now, I want to integrate this into another mammoth project that is implemented in Java. I do not want to implement all of my C functions in JAVA, so what is the best way to handle this problem?
I'd appreciate any pointers, suggestions and help. Thanks
Dilip
Hi,
Thanks for the suggestions. However, I'm experiencing this new problem.
[root@ahi dilip]# java -Djava.library.path=. HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1344)
at java.lang.Runtime.loadLibrary0(Runtime.java:744)
at java.lang.System.loadLibrary(System.java:815)
at HelloWorld.<clinit>(HelloWorld.java:7)
I did the same steps that was given in the tutorial.
1. Created the java class
2. Compiled the java class
3. Created the header file
4. Created the C file
5. Created the shared library using the following command
gcc -fPIC -shared -o HelloWorld HelloWorld.c
I added my current directory where I compiled these files into LD_LIBRARY_PATH too.
I'd really appreciate your reply. Thanks.
Dilip
The problem is only that shared libraries on unix systems are usually named "libxxxx.so" The loadLibrary method takes an abstract "library name" rather than the file name of the library -- it would be the "load" method you give a file name. the system is trying to look for a file with the conventional style name "libHelloWorld.so", fails to find it, and so crashes with UnsatisfiedLinkError.
To fix this, it should be enough to rename the file "HelloWorld" to "libHelloWorld.so". Alternatively you could call load instead of loadLibrary, but I guess it is more portable to use loadLibrary.
> gcc -fPIC -sha..
I take it that you are writing C++? just curious..
uhh.. right, otherwise you'd be calling g++ and not gcc. I just wondered about -fPIC, somehow I associated it with C++ though I guess it doesn't matter which language you use..