calling native c code from java using JNI
hi guys im fed up with jni couldnt even execute a simple helloworld program.. here is what i did as per the instruction given in a JNI book..
STEP 1:
Creating HelloWorld.java
Code Java:
class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld.dll");
}
}
compile HelloWorld.java to get HelloWorld.class
STEP 2
Creating a jni style header
command: javah -jni HelloWorld will generate a header file HelloWorld.h
STEP 3:
Creating dll and lib files
Code C:
#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}
int main(int argc, char **argv)
{
// dummy
return 0;
}
compile with file along with the header in Visual Studio in a dll project will generate dll files
STEP 4
now i ve created a java class file header file and the library HelloWorld.dll
when i execute the java HelloWorld.class it throws the the error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloWorld.dll in
java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at HelloWorld.<clinit>(HelloWorld.java:9)
Could not find the main class: HelloWorld. Program will exit.
guys please help me i dont know how to fix and i followed lot of other tutorials and i get the same error
Re: calling native c code from java using JNI
Java's smart enough to figure out the extension of HelloWorld for you, and actually requires that you don't give it. The reason you don't want to put the extension on there is because different OS's use different extensions (for example, windows use .dll, linux use .so, etc.).
Code Java:
static {
System.loadLibrary("HelloWorld");
}
Re: calling native c code from java using JNI
Just on the off chance someone has this same issue under linux I would like to point out that your library needs to be lib[whatever].so and when you do the system.loadlibrary() you put in [whatever] instead of the loadlibrary function excluding the lib and .so. I ran into this issue not long ago and it drove me nuts until I figured it out.
Re: calling native c code from java using JNI
Nex Gen Media Server is best known as a multi-purpose media streaming server to deliver live and stored video to a variety of devices. The same media server can be embedded into a mobile application to facilitate real time video communication...
For More Info :- Java Outsourcing, Java Development, Hire Java Developers - Aegis
Thanks.