Unfortunately, I am not much of an expert on native methods - in fact, I have never used them. One thing I know is that you need to manually load the necessary library using the "System.loadLibrary" method.
You should do this in a static block to ensure that the library is loaded immediately after the class is loaded and will be available when you need it. For example:
class CryptTest {
...
...
static {
System.loadLibrary("MyLibrary");
}
...
...
}
Replace "MyLibrary" with the appropriate library name, not sure what this is in your case (probably the standard C library).
An alternative, which I was referring to in my first post, would be to use Unix commands corresponding to the C functions (if they exist). You can use "Runtime.getRuntime().exec()" to execute a System command, which spawns a new process. You then need to wait until it has finished and look at the result.
Hope this helps.
-Mirko
Originally posted by Ma Di:
Thanks Mirko. I understand what you mean. It's exactly I want to
say but I meet hard problems when I implenment the solution. I'm
not very familiar with Java. For example,I need to call a C Library function crypt() in Java. The test code is as following:
import java.io.*;
class CryptTest {
private native String crypt(String pass, String salt);
public static void main(String[] args) {
CryptTest test = new CryptTest();
String pass = "melody";
String salt = "QE";
String entpass = test.crypt(pass,salt);
System.out.println("entpass is : "+entpass);
}
}
The compile is successful. When I run it, there's a "java.lang.UnsatisfiedLinkError". It means it can't find the crypt() C function. How can I do now?
Thanks for your answers.