问题
I am implementing a web application for face recognition in java using opencv. While I am running the code for face recognition I am getting errors like
java.lang.UnsatisfiedLinkError: Native Library F:\opencv\build\java\x86\opencv_java2411.dll already loaded in another classloader
i had done several methods avaliable over internet such as checking whether the class is loaded or not making the system.loadlibrary as static block adding path to environment variable and so on but the error persist in the system after all this process.
does anyone has the solution for this problem i am able to run with this dll in desktop application but while running in web application the error comes
i am running this project using glassfish server on netbeans ide
public class FaceRecognition {
public static boolean loaded = false;
public void loadLib(){
System.out.println("loading library");
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}catch(Exception e){}
loaded = true;
}
public FaceRecognition() {
}
public void saveFaceRecognizedImage(File file){
try {
MatOfByte mem = new MatOfByte();
CascadeClassifier faceDetector = new CascadeClassifier(facerecog.FaceRecognition.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));
MatOfRect faceDetections = new MatOfRect();
BufferedImage bi = ImageIO.read(file);
Mat frame = bufferedImageToMat(bi);
Rect rectcrop = null;
faceDetector.detectMultiScale(frame, faceDetections);
for (Rect rect : faceDetections.toArray()) {
System.out.println("ttt");
Core.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255,0));
rectcrop = new Rect(rect.x,rect.y,120,120);
}
try {
Mat imgrr = new Mat(frame,rectcrop);
Highgui.imwrite(file.getAbsolutePath(), imgrr);
// BufferedImage image = ImageIO.read(file);
// BufferedImage scaled = getScaledInstance(
// image, 120,120, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
// writeJPG(scaled, new FileOutputStream(file.getAbsolutePath()), 0.85f);
}catch(Exception e){}
// Highgui.imencode(".bmp", frame, mem);
// Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
// BufferedImage buff = (BufferedImage) im;
// ImageIO.write(buff,"jpg",file);
} catch (IOException ex) {
Logger.getLogger(FaceRecognition.class.getName()).log(Level.SEVERE, null, ex);
}
}
回答1:
Per the JNI specification:
Each class loader manages its own set of native libraries. The same JNI native library cannot be loaded into more than one class loader. Doing so causes UnsatisfiedLinkError to be thrown. For example, System.loadLibrary throws an UnsatisfiedLinkError when used to load a native library into two class loaders.
In an application server, each instance of an application uses a new class loader, so you will see this error if multiple applications try to load the same native library or if a single application is restarted without restarting the entire application server. Some options:
- Load the native library from a server-wide class loader that can be shared by all applications in the server and will not be recreated when an application is restarted. Most application servers support this, but the configuration is product-specific.
- If only a single application is accessing the native library but it is being restarted, then restart the entire application server JVM instead.
- If multiple applications are accessing the native library, then copy the native library to different locations to trick the JVM into loading it twice.
来源:https://stackoverflow.com/questions/34544261/unsatisfiedlink-error-opencv-java2411-dll-already-loaded-in-another-classloade