I'm working on a Spring MVC project that uses the Sigar library which relies on a DLL file being loaded by Tomcat. When i first start netbeans and press play (when the server is stopped) it compiles fine and the site interacts with the library perfectly. If i close the webpage and hit play again without first restarting Tomcat, it tries to load the library again and crashes during deployment. It looks like Tomcat tries to load the Sigar DLL a second time and crashes while deploying (since i have a Sigar instance as a global variable within a Spring controller. Is there a way for netbeans to automatically restart Tomcat when pressing play?
EDIT
Ok so I am trying to work around this problem by getting Tomcat to only load the DLL if it's not already loaded. I have searched a bit and found that a class like this one should run before the web app you're compiling runs :
@WebListener
public class MDHISServletContextListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent arg0)
{
loadNativeLibraries();
}
private void loadNativeLibraries()
{
boolean sigarLoaded = false;
try
{
List<String> nativeLibraries = new NativeLibraryBrowser().getLoadedLibraries();
for (String library : nativeLibraries)
{
if (library.contains("sigar"))
{
sigarLoaded = true;
break;
}
}
if (!sigarLoaded)
{
System.loadLibrary("C:\\path\\to\\dll\\folder\\sigar-amd64-winnt.dll");
}
}
catch (Exception ex)
{}
}
}
I have built the NativeLibraryBrowser class like provided in a post i found and it does indeed return a list of String containing the path to the Sigar DLL when i drop it in Tomcat's bin folder for it to be loaded. However, when i put a breakpoint in this class it is never reached and my web app fails to deploy due to the controller depending on it not being able to initialize. What am i doing wrong?
BIG UPDATE
I was able to dynamically load the library by adding the path to where i put the DLL file to the PATH environment variable and doing :
System.loadLibrary("sigar-amd64-winnt");
The problem is that it ONLY works when my Sigar instance is not global to the controller and i need it to be. It looks like tomcat is instantiating the servlets before a class annotated with WebListener runs. Is there any way to get a library loaded into Tomcat before the servlets are loaded aside from just dropping it in Tomcat's bin folder which brings me back to having to constantly restart it before compiling my app?
Thanks!