Java 使用在ServeltContextListener中外部定义的类

Java 使用在ServeltContextListener中外部定义的类,java,tomcat,servlets,noclassdeffounderror,Java,Tomcat,Servlets,Noclassdeffounderror,我正在实现ServletContextListener: public class Listener implements ServletContextListener { public void contextInitialized(ServletContextEvent arg0) { ServletContext context=arg0.getServletContext(); try { XMLInputSource

我正在实现ServletContextListener:

public class Listener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent arg0) {

        ServletContext context=arg0.getServletContext();

        try {
            XMLInputSource in = new XMLInputSource("/home/ahmed/Desktop/AE.xml");
        } catch (IOException e) {

        }
    }
}
其中,
XMLInputSource
外部jar中定义;它包含在项目中(在Eclipse上)


但是,尝试使用此外部类,会导致异常
: java.lang.NoClassDefFoundError


有什么帮助吗?Tomcat是否需要以特殊方式注册使用过的外部库???

Webapp库必须存储在部署的WEB应用程序的/WEB-INF/lib目录中。在WTP eclipse项目中,您只需将jar放在WebContent/WEB-INF/lib文件夹中,这个jar就会自动删除

  • 在项目的构建类路径中
  • 通过Eclipse在/WEB-INF/lib下部署
因此,将其从构建路径中删除,并将其放到WebContent/WEB-INF/lib中

请不要像在代码片段中那样吞咽异常!如果不能抛出IOException,至少抛出一个封装原始IOException的运行时异常