简体   繁体   English

将类从WAR移到JAR时发生NoClassDefFoundError

[英]NoClassDefFoundError when moving classes from WAR to JAR

I am trying to deploy a dynamic web project in a way that allows me to dynamically add Quartz jobs to the classpath. 我正在尝试以允许我将Quartz作业动态添加到类路径的方式部署动态Web项目。 Here is my thinking. 这是我的想法。

'If I read configuration from an XML file containing a fully qualified classpath, then use Class.forName() in the class to create an instance of said class from the config XML, then I should be able to access dynamically added classes placed in Tomcats TOMCAT_HOME/lib directory'. “如果我从包含完全限定的类路径的XML文件中读取配置,然后在该类中使用Class.forName()从config XML中创建该类的实例,那么我应该能够访问动态添加到Tomcat中的类TOMCAT_HOME / lib目录”。

Before this update, the application worked fine, but I had these newly externalized classes contained in the war. 在此更新之前,该应用程序运行良好,但战争中包含了这些新近外部化的类。 However, I can no longer do this because I: 但是,我不能再这样做,因为我:

  1. Don't want to redeploy the war every time a new job is required. 不想在每次需要新工作时都重新部署战争。
  2. Cannot take the server down to add new jobs as there are jobs that need to run continuously. 无法关闭服务器以添加新作业,因为有些作业需要连续运行。

However, I am getting a NoClassDefFoundError when I run the class.forName() method. 但是,当我运行class.forName()方法时,出现了NoClassDefFoundError。 I have already verified in catalina.properties that the lib directory in Tomcat is in the common.loader property. 我已经在catalina.properties中验证了Tomcat中的lib目录在common.loader属性中。

My question is, how can I get my WAR classes to recognize the classes in a jar in the Tomcat common library. 我的问题是,如何获取WAR类以识别Tomcat公共库中jar中的类。 Any ideas? 有任何想法吗? Thanks. 谢谢。

addition: 加成:

@BalusC: I have actually already developed a web based admin screen which allows the user to edit the XML config file to add new jobs. @BalusC:我实际上已经开发了一个基于Web的管理屏幕,该屏幕允许用户编辑XML配置文件以添加新作业。 However, to add not just another instance of a job, but an entirely new job, there needs to be code definition of this new job. 但是,要添加的不仅是一个作业的另一个实例,而是一个全新的作业,需要对该新作业进行代码定义。 I want that to be placed into a jar file to be dropped into the tomcat lib directory to be picked up by class.forName(). 我希望将其放置在jar文件中,然后放到tomcat lib目录中,以供class.forName()拾取。

Hopefully, Tomcat does not reload automatically your context or loads the jars in your classloader automatically. 希望Tomcat不会自动重新加载上下文,也不会自动将jar加载到类加载器中。 It could result in uncontrollable behaviors. 这可能会导致无法控制的行为。

You will never be able to access those new classes in the WebAppClassLoader (the one managed per tomcat context) without loading the explicitly the jar through an URLClassLoader . 如果没有通过URLClassLoader显式加载jar,您将永远无法访问WebAppClassLoader中的那些新类(每个tomcat上下文管理一个新类)。 I'll advice to use an absolute path to the jar. 我建议使用罐子的绝对路径。 For some dark reasons I had issues with relative path. 由于某些黑暗的原因,我在相对路径方面遇到了问题。

If you want to know the tomcat installation path and for instance the lib directory, you could use the catalina.home and catalina.base environment variables. 如果您想知道tomcat的安装路径,例如lib目录,则可以使用catalina.homecatalina.base环境变量。

HIH HIH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM