• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

use c shared object in java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic