File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes use c shared object in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "use c shared object in java" Watch "use c shared object in java" New topic
Author

use c shared object in java

balaji phaneedra kuamr kumar
Greenhorn

Joined: Mar 10, 2009
Posts: 3
i have shared object created using gcc (c compiler) not contain any java related things. now i need to call apis present inside the shared object (i dont have c code which used for shared object) using java.
please give small example for that.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

balaji phaneedra kuamr kumar wrote:i have shared object created using gcc (c compiler) not contain any java related things. now i need to call apis present inside the shared object (i dont have c code which used for shared object) using java.
please give small example for that.



This is not exactly something that can be learned with a small example. Basically, you need to write a C wrapper (which conforms to the Java calling standard) that will be called by Java and in turn, can route the request to your C shared object.

The steps are....

1. With Java, create the methods that you want (declared as native methods).
2. Compile the class.
3. Use javah to generate the include file for the C wrapper.
4. With C, create the methods that will take the request from Java and call your share lib. (using the include file)
5. Compile and generate the wrapper shared lib.
6. Modify the Java class to load the wrapper shared lib.

The Sun tutorial is a good place to start (or you can just google for JNI) ....

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
balaji phaneedra kuamr kumar
Greenhorn

Joined: Mar 10, 2009
Posts: 3
thanks for quick reply but my situation is
for example:
some coder done this like steps
gcc -c -fPIC calc_mean.c -o calc_mean.o
gcc -shared -Wl,-soname,libmean.so.1 -o libmean.so calc_mean.o

provided only libmean.so not code and i know it contain api as float mean_value(int a,int b);

so have written code like
#include "JNIDemo.h"
JNIEXPORT void JNICALL Java_JNIDemo_display(JNIEnv *env, jobject obj)
{
int a,b;
float result;
printf("enter two values\n");
scanf("%d %d",&a,&b);
result=mean_value(a,b);
printf("result is %f",result);
}

JavaDemo.c
public class JNIDemo
{
public native void display();
static
{
System.loadLibrary("mean");
System.loadLibrary("nativecode");
}
public static void main(String args[])
{
try
{
JNIDemo jdo=new JNIDemo();
jdo.display();
}
catch(Exception e)
{
System.out.println("Alert Alert Alert:"+e.getMessage());

}
}
}
}

now i have done
1)cc -o libnativecode.so -shared -I/JDK/include -I/JDK/include/linux NativeCode.c
2)export LD_LIBRARY_PATH=`pwd`
3)java JNIDemo

then it giving error "java.lang.UnsatisfiedLinkError"

please help me i just want to use that .so file apis in java
please
i was getting result for .so files which is compiled using java library's but my case is different
iam very new to java (frankly very beginner )
please help me out


it is very urgent, tell me is it possible to use c library (which is compiled with out java libraries) in java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use c shared object in java
 
Similar Threads
JNI
Java Shared Memory
do we have any methods to used c dll in java except jni
Why this synchronization doesn't work
java onn windows vista