aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes calling C from java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "calling C from java" Watch "calling C from java" New topic
Author

calling C from java

chandana nannapaneni
Ranch Hand

Joined: Dec 18, 2008
Posts: 39
Hi,

Firstly, I am sorry if this topic is in a wrong forum(I didnot find an appropiate forum to post this).

My aim is to call a C function from java. I got "java.lang.UnsatisfiedLinkError: Can't load library" error. Let me explain what I have done.

I wrote a java program as,

public class ReadFile {
native void loadFile(String name);
static {
System.load("C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\ nativelib.obj");
}
public static void main(String args[]) {
ReadFile mappedFile=new ReadFile();
mappedFile.loadFile("ReadFile.java");
}
}

Then, I generated ReadFile.class and ReadFile.h files
After that I wrote a C program as,

#include <jni.h>
JNIEXPORT void JNICALL Java_ReadFile_loadFile
(JNIEnv * env, jobject jobj, jstring name) {
printf("yahoo, i am into the C program");
return 0;
}

Now at the VB command prompt, I generate the library files
When I run the ReadFile program, I get
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: C
:\Program Files\Microsoft Visual Studio 9.0\VC\ nativelib.obj
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at ReadFile.<clinit>(ReadFile.java:8)
Could not find the main class: ReadFile. Program will exit.

Can anyone help me with this.


Chandana.
chandana nannapaneni
Ranch Hand

Joined: Dec 18, 2008
Posts: 39
I figured out my mistake.
I should load .dll file instead of the .obj file.
I am sorry for this topic.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calling C from java