java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26024193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions
提问by bhrt
I am trying to use POI in a servlet to process an uploaded file as Excel file:
我正在尝试在 servlet 中使用 POI 将上传的文件处理为 Excel 文件:
public static String readExcel(InputStream inp) {
// InputStream inp = null;
StringBuilder excelDataBuilder = new StringBuilder();
try {
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Header header = sheet.getHeader();
int rowsCount = sheet.getLastRowNum();
for (int rowCounter = 0; rowCounter <= rowsCount; rowCounter++) {
Row row = sheet.getRow(rowCounter);
int colCounts = row.getLastCellNum();
for (int colCounter = 0; colCounter < colCounts; colCounter++) {
Cell cell = row.getCell(colCounter);
excelDataBuilder.append(cell.getStringCellValue());
if (colCounter < colCounts)
excelDataBuilder.append(",");
}
if (rowCounter <= rowsCount) {
excelDataBuilder.append("\n");
}
}
return excelDataBuilder.toString();
} catch (Exception ex) {
LOG.error("Exception", ex);
} finally {
try {
inp.close();
} catch (IOException ex) {
LOG.error("IOException", ex);
}
}
return excelDataBuilder.toString();
}
However, it threw an exception at the following line:
但是,它在以下行抛出异常:
Workbook wb = WorkbookFactory.create(inp);
Here's the Stack Trace:
这是堆栈跟踪:
SEVERE: Servlet.service() for servlet [com.bhrt93.excel.servlet.UploadProcessExcel] in context with path [/ExcelServletProcessCommons] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.poi.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:59)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:78)
at com.bhrt93.excel.service.ExcelReaderService.readExcel(ExcelReaderService.java:22)
at com.bhrt93.excel.servlet.UploadProcessExcel.doPost(UploadProcessExcel.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
How is this caused and how can I solve it?
这是怎么引起的,我该如何解决?
采纳答案by Gagravarr
As detailed in the Apache POI page on Components and their Dependencies...
If you want to use Common SS code, such as WorkbookFactory, you need both the poi
and poi-ooxml
POI jars on your classpath. In addition, you also need their dependencies too. One of the listed dependenciesis xmlbeans
, which incidentally is where your missing class comes from
如果要使用通用 SS 代码,例如 WorkbookFactory,则类路径中需要poi
和poi-ooxml
POI jar。此外,您还需要它们的依赖项。其中的上市依赖性是xmlbeans
,这亦是你缺少类来自
So, you just need to review the Components and Dependencies page, then make sure you include the appropriate jars from the binary package of Apache POI on your classpath. (Or use Maven and let it worry about that for you)
因此,您只需要查看组件和依赖项页面,然后确保在类路径中包含来自 Apache POI 二进制包的适当 jar。(或者使用 Maven 让它为你担心)
回答by Jakob Alexander Eichler
As found here, you have to add (at least that was it in my case): xmlbeans-2.6.0.jar
由于发现在这里,你必须增加(至少是它在我的情况):XMLBeans的-2.6.0.jar
回答by Manimaran Samuthirapandi
System couldn't find the XML beans dependency on your classpath so you should add the XML beans dependency to your class path.
系统在您的类路径上找不到 XML bean 依赖项,因此您应该将 XML bean 依赖项添加到您的类路径中。
The XML beans library looks like xmlbeans-x.x.x.jar
XML bean 库看起来像 xmlbeans-x.x.x.jar
https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans
https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans