为什么运行时无法找到类? [英] Why can't the runtime find class?

查看:130
本文介绍了为什么运行时无法找到类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个eclipse项目,项目A和B.项目A是在eclipse内在Tomcat上运行的JavaEE Web应用程序。项目B有一些独立的应用程序代码。我将项目B添加到项目A的构建路径和Tomcat的类路径(作为用户条目)中作为项目依赖关系。



项目A和B都有独立的apache commons-lang-2.6.jar副本,其中包含StringUtils类。当我运行Tomcat服务器时,一切都会启动。当发生请求时,它由项目A中的Servlet(其导入并使用StringUtils)处理。然后调用项目B中的代码,该代码也使用StringUtils。但是,项目B中的代码出现以下错误:

  SEVERE:Servlet的Servlet.service()servlet [*** Servlet ]在路径[/ ***]的上下文中抛出异常
[Servlet执行抛出一个异常]与根本原因
java.lang.ClassNotFoundException:org.apache.commons.lang.StringUtils

显然,已加载了StringUtils类以供Servlet使用。

1)为什么不能运行时环境没有找到/使用已经加载的StringUtils来执行Project B代码?
2)如何解决这个问题?

解决方案

编译时, Java Build Path 控制类路径,但是需要使用动态Web项目部署程序集属性页来配置部署。毕竟,您可能已经添加了一个API jar来编译您最终部署服务器运行时的一部分可用的项目,或者在您的情况下,使用另一个必须在Web应用程序中打包为jar的项目WEB-INF / lib文件夹在运行时使用。



部署装配新的和值得注意的提及


I have two eclipse projects, project A and B. Project A is a JavaEE web application run on Tomcat within eclipse. Project B has some standalone application code. I have added Project B as a project dependency to Project A's Build Path and to Tomcat's classpath (as a User Entry).

Both Project A and B have separate copies of apache commons-lang-2.6.jar, which contains the StringUtils class. When I run the Tomcat server, everything starts up fine. When a request occurs, it is handled by a Servlet in Project A (Which imports and uses StringUtils). It then calls code in Project B, which also uses StringUtils. However, the code in Project B gives the following error:

SEVERE: Servlet.service() for servlet [***Servlet] in context with path [/***] threw exception 
[Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils

Obviously the StringUtils class was loaded for the Servlet to use.
1) Why can't the runtime environment not find/use the StringUtils already loaded to execute Project B code? 2) How do I fix this so dependencies are handled as they should?

解决方案

The Java Build Path controls the classpath when compiling, but you need to also configure the deployment using the Deployment Assembly property page of your Dynamic Web Project. After all, you might have added an API jar to compile your project that's available as part of the server runtime when you finally deploy it, or as in your case, made use of another project that must be packaged as a jar in your web app's WEB-INF/lib folder to used at runtime.

Deployment Assembly New and Noteworthy mention

这篇关于为什么运行时无法找到类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆