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

[379 byte] By [dilip.sundarraj] at [2007-9-30 11:06:30]
# 1
do a google search for "java jni tutorial"Follow sun's JNI tutorial and once you get the helloWorld app running in c, main program java your well on your way.
wew64 at 2007-7-3 22:45:09 > top of Java-index,Administration Tools,Sun Connection...
# 2
Sun has a whole book online which you'll hopefully find helpful: http://java.sun.com/docs/books/jni/
jsalonen at 2007-7-3 22:45:09 > top of Java-index,Administration Tools,Sun Connection...
# 3

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

dilip.sundarraj at 2007-7-3 22:45:09 > top of Java-index,Administration Tools,Sun Connection...
# 4

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..

jsalonen at 2007-7-3 22:45:09 > top of Java-index,Administration Tools,Sun Connection...
# 5
Thanks! that worked..BTW, I'm using C
dilip.sundarraj at 2007-7-3 22:45:09 > top of Java-index,Administration Tools,Sun Connection...
# 6
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..
jsalonen at 2007-7-3 22:45:09 > top of Java-index,Administration Tools,Sun Connection...