Problem : Native Library already loaded in another classloader
843829Jun 7 2005 — edited Oct 26 2007I created a shared library HaspKeyAPI.dll by using JNI and add path to Java class library. I need to call native functions in my servletAction class to check some information in a key. Sometimes not always, I met �java.lang.UnsatisfiedLinkError: Native Library C:\eclipse\workspace\HaspKey\SharedLib\HaspKeyAPI.dll already loaded in another classloader� problem. I have to restart tomcat server then to restart my web app.
Does anyone know how to fix it?
Thank you in advance!
////////////////////////////////////my native function class, a singleton class////////////////////////////////
package haspkey.api;
import java.util.Collection;
import java.util.Vector;
import haspkey.HardwareSecurityKey;
Public class HaspKeyAPI {
Public static final int KEYEXIST = 1;
//A Singleton Class
private static HaspKeyAPI haspKeyAPI;
private HaspKeyAPI()
{
}
public static HaspKeyAPI getSingletonObject()
{
if ( haspKeyAPI== null)
haspKeyAPI = new HaspKeyAPI();
return haspKeyAPI;
}
public Object clone()
throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
/**********use native functions to call hasp C API***********/
public native int keyPresent(int password1, int password2);
public native int getId(int password1, int password2);
public native void readBlock(int startMemoryLocation, int size,
byte[] block, int password1, int password2);
public native void writeBlock(int startMemoryLocation,int size, byte[] block, int password1,
int password2);
public native boolean decodeData(int size, byte[] block, int password1,
int password2);
public native boolean encodeData(int size, byte[] block, int password1,
int password2);
static {
System.loadLibrary("HaspKeyAPI");
}
private int getPrivMachineSpeed(boolean decrypt, int pass1, int pass2, String MLOCRTypeCode){
byte[] block = new byte[8];
int privMachineSpeed = 0;
boolean isNumeric = true;;
readKeyMemory(40,8,block, decrypt, pass1, pass2);
if(block.length!=8){
return -1;
}else{
String s = new String(block);
for(int n=0; n<3; n++){
if(s.charAt(n)<48 || s.charAt(n)>57){
isNumeric = false;
}
}
if(isNumeric){
privMachineSpeed = Integer.parseInt(s.substring(0,3));
}
}
return privMachineSpeed;
}
public HardwareSecurityKey getHardwareSecurityKey(boolean decrypt, int pass1, int pass2, String AuthorizationCodes,String MLOCRTypeCode){
HardwareSecurityKey key = new HardwareSecurityKey();
if(keyPresent(pass1,pass2)==KEYEXIST){
key.setPrivKeyID(getId(pass1, pass2));
key.setPrivMachineSpeed(getPrivMachineSpeed(true, pass1,pass2, MLOCRTypeCode));
}else{
return null;
}
}
/////////////////////////////// a class include some static final information to call the native function///////////////
import com.bowebellhowell.haspkey.api.HaspKeyAPI;
import com.bowebellhowell.winsort.vo.haspkey.HardwareSecurityKey;
public class HaspKey{
public static final String MLOCRTypeCode ="mmmmmmmmmmmmmmm";
public static final String AuthorizationCodes = "bbbbbbbbbbbbbbbb";
public static final int p1 = 0;
public static final int p2 = 0;
public static HardwareSecurityKey getHardwareSecurityKey(){
HaspKeyAPI key = HaspKeyAPI.getSingletonObject();
return key.getHardwareSecurityKey(true,p1,p2,AuthorizationCodes,MLOCRTypeCode);
}
}
/////////////////////////////////// a servlet to call native function///////////////////////////////////
public class LogonAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
int n = HaspKey.isKeyPresent();
if(n!=1){
if(n==2){
message = "KeyDriverNotMatch";
}else if(n==3){
message = "KeyOldModel";
}else if(n==4){
message = "KeyPasswordNotMatch";
}else if(n==-1){
message = "keyNotExist";
}
request.getSession().setAttribute("message",message);
}
}
java.lang.UnsatisfiedLinkError: Native Library C:\eclipse\workspace\HaspKey\SharedLib\HaspKeyAPI.dll already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1551)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1511)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:834)
at haspkey.api.HaspKeyAPI.<clinit>(HaspKeyAPI.java:76)
at util.HaspKey.isKeyPresent(HaspKey.java:35)
at LogonAction (LogonAction .java:65)