尽管我添加了正确的 Apache POI .jar 文件,Eclipse 仍抛出 ClassNotFoundException?
我正在使用 servlet 将 .xls
(Excel 电子表格)文件上传到服务器。我将 Apache FileUpload API 用于业务逻辑的上传部分,为了确保这部分正常工作,我已成功尝试将 .txt 文件上传到 Servlet,然后从 servlet 端使用 Apache FileUpload API给我一个 InputStream
,我可以将 .txt
的所有内容打印到控制台上。
不起作用的部分是,当我尝试使用该 InputStream
并将其转发到一个方法时,我会扫描用户上传的 .xls
文件并打印它在控制台上。抛出的异常是:
java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.WorkbookFactory
尽管我已将 poi-3.5-FINAL-20090928.jar
和 poi-ooxml-3.5-FINAL-20090928.jar
添加到Java 项目构建路径。应该提到的是,后一个文件包含 WorkbookFactory
类。
然而,当我尝试扫描硬盘驱动器本地存在的 .xls 文件时,我从中派生了 InputStream
并将其转发以打印 .xls
的所有单元格>,它在控制台上完美地打印出每行和每列的单元格。有什么解决办法吗?
I'm using a servlet to upload .xls
(Excel Spreadsheets) files to the server. I'm using the Apache FileUpload API for the upload portion of the business logic, to ensure weather this part is working, I have successfully attempted uploading a .txt file to the Servlet after which -- the Apache FileUpload API, from the servlet side gives me an InputStream
from which I print out all the contents of the .txt
on to the console.
The part which isn't working is, when, I attempt to use that InputStream
and forward it to a method in which, I scan the user uploaded .xls
file and print it on the console. The Exception being thrown is:
java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.WorkbookFactory
This happens despite me having added both, poi-3.5-FINAL-20090928.jar
and poi-ooxml-3.5-FINAL-20090928.jar
to the Java Project Build Path. It should be mentioned that the latter file contains the WorkbookFactory
class.
Yet, when I tried scanning a .xls file which was locally present on the hard drive, from which I derived the InputStream
and forward that to print all the cells of the .xls
, it prints out the cells on each row and columns flawlessly on the console. Any solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用 Apache Commons FileUpload,这意味着您正在使用 Web 应用程序。您是否像通常对任何第 3 方库所做的那样将库放置在 web 应用程序的
/WEB-INF/lib
文件夹中?您不需要对 Java 项目构建路径执行任何操作。 Eclipse 将自动获取构建路径中/WEB-INF/lib
中的任何库,并且 Web 应用程序部署后,默认情况下将获取类路径中的这些库。如果这没有帮助,那么请确保类路径中的其他位置没有旧版本的 POI,这可能会在类加载中发生冲突。除了每个应用程序服务器的
/lib
下的 webapp 的/WEB-INF/lib
之外(也许还包括其他内容,具体取决于 make/version/configuration,请参阅文档以了解详细信息) ) 和 JRE 的/lib
。如果其中有较旧的 POI 版本,您需要先将其删除。实际上,这些文件夹不应包含任何第三方 Web 应用程序特定的库,它只会使类路径变得混乱。You're using Apache Commons FileUpload which thus implies that you're using a webapplication. Did you place the libraries in the webapp's
/WEB-INF/lib
folder as you usually should do for any 3rd party libraries? You don't need to do anything with Java Project Build Path. Eclipse will automagically take any libraries in/WEB-INF/lib
in the buildpath and the webapplication, once deployed, will by default take those libraries in the classpath.If that doesn't help, then ensure that you don't have somewhere else an older version of POI in the classpath which may coillide in the classloading. This includes aside from the webapp's
/WEB-INF/lib
under each the appserver's/lib
(and maybe others, depending on make/version/configuration, consult the docs for details) and the JRE's/lib
. If there are older POI versions in there, you need to remove them first. Actually, those folders should not contain any 3rd party webapp-specific libraries, it would only clutter the classpath.解决方案非常简单:
就这样,你完成了..:D
如果这能解决您的问题,请告诉我..
the solution for this is very simple :
that's it, you're done.. :D
Please let me know if this solve your problem..