Load C++ Library Only Once
-
Hello there. I have managed to load and use c++ dll using JNI, in my java servlet (System.load). It works perfectly on first request I submit. But on subsequent requests, I get this weir d exception.
java.lang.UnsatisfiedLinkError: Native Library WEB-INF/lib/XXXXXX.dll already loaded in another classloader at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1525)
Here is my loading codepublic void init(ServletConfig config) throws ServletException
{
super.init(config);
ServletContext context = getServletContext();
String sMathDllpath = context.getInitParameter("MathDLLPath");
System.load(sMathDllpath);}
I studied this tomcat wiki link and implemented their solution, among others, but could not succeed. How do I implement it successfully? Thnks for anything you share.
-
Hello there. I have managed to load and use c++ dll using JNI, in my java servlet (System.load). It works perfectly on first request I submit. But on subsequent requests, I get this weir d exception.
java.lang.UnsatisfiedLinkError: Native Library WEB-INF/lib/XXXXXX.dll already loaded in another classloader at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1525)
Here is my loading codepublic void init(ServletConfig config) throws ServletException
{
super.init(config);
ServletContext context = getServletContext();
String sMathDllpath = context.getInitParameter("MathDLLPath");
System.load(sMathDllpath);}
I studied this tomcat wiki link and implemented their solution, among others, but could not succeed. How do I implement it successfully? Thnks for anything you share.
-
You should declare it as a static action at class level like:
class ... {
static
{
System.loadLibrary("MathDLLPath");
}
// remainder of your class
}That will ensure it is loaded when the class is first instantiated, but will only be loaded once.
Richard MacCutchan wrote:
You should declare it as a static action at class level like:
I tried this just now. This is what I have
public class MainServlet extends HttpServlet
{
static{
System.loadLibrary("MathDll");
}......... // rest of the class below this
}And this is the exception I am getting
java.lang.UnsatisfiedLinkError: Native Library C:\Program Files\Java\jdk1.7.0_71\bin\MathDll.dll already loaded in another classloader
What am I doing wrong now? -
Richard MacCutchan wrote:
You should declare it as a static action at class level like:
I tried this just now. This is what I have
public class MainServlet extends HttpServlet
{
static{
System.loadLibrary("MathDll");
}......... // rest of the class below this
}And this is the exception I am getting
java.lang.UnsatisfiedLinkError: Native Library C:\Program Files\Java\jdk1.7.0_71\bin\MathDll.dll already loaded in another classloader
What am I doing wrong now?