Again let me dive straight in to catch this culprit. So! how did it happen? You have a method call which uses or wants to use
JNA to perform some native system calls. You then fired your call only to be greeted with:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'c':
The specified module could not be found.
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:145)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:188)
at com.sun.jna.Library$Handler.<init>(Library.java:123)
at com.sun.jna.Native.loadLibrary(Native.java:255)
at com.sun.jna.Native.loadLibrary(Native.java:241)
WTH is this you asked yourself?! well I have to be honest, this is just the beginning. Let me demostrate this with a simple
JNA call to illustrate where this exception came from. Let us assume below is your native functions you want to invoke. Again I will make an assumption that, your native code was compiled on windows enviroment using perhaps
Cygwin gcc command.
//File Name: native.c
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#ifdef _WIN32
# include <io.h>
typedef signed int md_t;
int fchmod(const char * path, md_t mode)
{
return (_chmod(path, (mode==777 ? _S_IWRITE : _S_IREAD )));
}
#else
int fchmod(const char * path, md_t mode)
{
return(chmod(path, mode));
}
#endif
int main(int argv, char ** argvs)
{
return 0;
}
Here again let us assume this is your Java application from which you want to call the native functions:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class demoJNA
{
public interface Natives extends Library
{
public int fchmod(String path, int mode);
}
public static void main(String[] args)
{
Natives posix = (Natives) Native.loadLibrary("c", Natives.class);
posix.fchmod("c:\\somefile.txt", 777);
}
}
Out of patience you compiled your java application, executed it and hence the exception. Ok stop here! lets revisit your application, first notice the call;
Native.loadLibrary("c", Natives.class);
is
"c" the library output name given when you compiled your native code? Let say I compile the native code above like this;
gcc -o native.dll native.c
the call then should look like this;
Natives posix = (Natives) Native.loadLibrary("native", Natives.class);
One more thing, Is the ouput library file located at your classpath? if not then please do move it there, because it from there JNA will be looking when the call
Native.loadLibrary is fired.
No comments:
Post a Comment