File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use c shared object in java

 
balaji phaneedra kuamr kumar
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
balaji phaneedra kuamr kumar
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic