Skip to content

Instantly share code, notes, and snippets.

@qlong8807
Last active April 23, 2018 03:11
Show Gist options
  • Save qlong8807/43ad6f334bedcdf08c123e89adc5540c to your computer and use it in GitHub Desktop.
Save qlong8807/43ad6f334bedcdf08c123e89adc5540c to your computer and use it in GitHub Desktop.
web项目启动前Listener,把sigar加入java.library.path
1.在web.xml中添加监听
<listener>
<listener-class>com......listener.InitLibraryPath</listener-class>
</listener>
2.读取src/main/resources/sigar目录下的所有内容
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.google.common.io.Resources;
public class InitLibraryPath implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
try {
String file = Resources.getResource("sigar").getFile();
String path = System.getProperty("java.library.path");
if (isOSWin()) {
path += ";" + file;
} else {
path += ":" + file;
}
System.setProperty("java.library.path", path);
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean isOSWin() {// OS 版本判断
String OS = System.getProperty("os.name").toLowerCase();
if (OS.indexOf("win") >= 0) {
return true;
} else
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment