尽管我添加了正确的 Apache POI .jar 文件,Eclipse 仍抛出 ClassNotFoundException?

发布于 2024-08-12 04:42:03 字数 758 浏览 2 评论 0原文

我正在使用 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.jarpoi-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶舞 2024-08-19 04:42:03

您正在使用 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.

極樂鬼 2024-08-19 04:42:03

解决方案非常简单:

  1. 下载此链接中的所有 jar 文件 http://archive.apache.org/dist/poi/release/bin/poi-bin-3.9-20121203.zip
  2. 解压文件并将所有 jar 文件复制到“\WebContent\ WEB-INF\lib\" 在您的项目中。请注意:您必须将其直接复制并粘贴到 ECLIPSE 中的项目中,不要通过 Windows 资源管理器复制粘贴它或将其作为外部 JAR 放置在 java 构建路径中,因为它永远不会工作。

就这样,你完成了..:D
如果这能解决您的问题,请告诉我..

the solution for this is very simple :

  1. download all the jar file in this link http://archive.apache.org/dist/poi/release/bin/poi-bin-3.9-20121203.zip
  2. extract the file and copy all the jar file to "\WebContent\WEB-INF\lib\" in your project. PLEASE NOTE : YOU MUST COPY AND PASTE IT RIGHT INTO YOUR PROJECT IN ECLIPSE, don't copy paste it through windows explorer or place it as external JAR in java build path, because it will never works.

that's it, you're done.. :D
Please let me know if this solve your problem..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文