java.lang.NoClassDefFoundError : au/com/bytecode/opencsv/CSVReader - Upload File Vaadin

标签 java vaadin vaadin7

基于 - https://gist.github.com/canthony/3655917 中提到的示例我创建了一个新的 Vaadin 示例来上传 Excel/CSV 文件。

根据提到的评论,我什至从 http://opencsv.sourceforge.net 下载了 opencsv-3.0并将它们添加到项目中。

以下是我添加它们的方式

右键单击创建的 Vaadin 项目 --> 属性 --> Java 构建路径 --> 添加库(创建新用户库) --> 新建用户库 --> 用户库 --> 新建(在用户库页面中)- > 创建名为 CSV 的新库 --> 包含 OpenCSV3.0-jar

最后这是我的设置的样子:

Libraries set up

不存在错误或警告,但是当我在 tomcat 上发布时,出现以下错误。当我浏览文件并单击上传按钮时出现此错误。有人可以帮忙吗?

SEVERE: 
java.lang.NoClassDefFoundError: au/com/bytecode/opencsv/CSVReader
at com.example.uploadexcel.UploadexcelUI.buildContainerFromCSV(UploadexcelUI.java:101)
at com.example.uploadexcel.UploadexcelUI$2.uploadFinished(UploadexcelUI.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:979)
at com.vaadin.ui.Upload.fireUploadInterrupted(Upload.java:875)
at com.vaadin.ui.Upload$2.streamingFailed(Upload.java:1166)
at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:615)
at com.vaadin.server.communication.FileUploadHandler.handleFileUploadValidationAndData(FileUploadHandler.java:447)
at com.vaadin.server.communication.FileUploadHandler.doHandleSimpleMultipartFileUpload(FileUploadHandler.java:397)
at com.vaadin.server.communication.FileUploadHandler.handleRequest(FileUploadHandler.java:282)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
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:103)
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:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
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)

当我将所需的 jar 添加到 Java 构建路径时,我不明白为什么会出现 java.lang.NoClassDefFoundError。

完整代码:
public class UploadexcelUI extends UI {
protected File tempFile;
protected Table table;
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = UploadexcelUI.class)
public static class Servlet extends VaadinServlet {
}

@SuppressWarnings("deprecation")
@Override
protected void init(VaadinRequest request) {
    Upload upload = new Upload("Upload CSV File", new Upload.Receiver() {
        @Override
        public OutputStream receiveUpload(String filename, String mimeType) {
            try {
                /* Here, we'll stored the uploaded file as a temporary file. No doubt there's
                a way to use a ByteArrayOutputStream, a reader around it, use ProgressListener (and
                a progress bar) and a separate reader thread to populate a container *during*
                the update.

                This is quick and easy example, though.
                 */
                tempFile = File.createTempFile("temp", ".csv");
                return new FileOutputStream(tempFile);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    });
    upload.addListener(new Upload.FinishedListener() {
        @Override
        public void uploadFinished(Upload.FinishedEvent finishedEvent) {
            try {
                /* Let's build a container from the CSV File */
                FileReader reader = new FileReader(tempFile);
                IndexedContainer indexedContainer = buildContainerFromCSV(reader);
                reader.close();
                tempFile.delete();

                /* Finally, let's update the table with the container */
                table.setCaption(finishedEvent.getFilename());
                table.setContainerDataSource(indexedContainer);
                table.setVisible(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    /* Table to show the contents of the file */
    table = new Table();
    table.setVisible(false);

    /* Main layout */
    VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    layout.setSpacing(true);
    layout.addComponent(table);
    layout.addComponent(upload);
    setContent(layout);
}

/**
 * Uses http://opencsv.sourceforge.net/ to read the entire contents of a CSV
 * file, and creates an IndexedContainer from it
 *
 * @param reader
 * @return
 * @throws IOException
 */
@SuppressWarnings("resource")
protected IndexedContainer buildContainerFromCSV(Reader reader) throws IOException {
    IndexedContainer container = new IndexedContainer();
    CSVReader csvReader = new CSVReader(reader);
    String[] columnHeaders = null;
    String[] record;
    while ((record = csvReader.readNext()) != null) {
        if (columnHeaders == null) {
            columnHeaders = record;
            addItemProperties(container, columnHeaders);
        } else {
            addItem(container, columnHeaders, record);
        }
    }
    return container;
}

/**
 * Set's up the item property ids for the container. Each is a String (of course,
 * you can create whatever data type you like, but I guess you need to parse the whole file
 * to work it out)
 *
 * @param container The container to set
 * @param columnHeaders The column headers, i.e. the first row from the CSV file
 */
private static void addItemProperties(IndexedContainer container, String[] columnHeaders) {
    for (String propertyName : columnHeaders) {
        container.addContainerProperty(propertyName, String.class, null);
    }
}

/**
 * Adds an item to the given container, assuming each field maps to it's corresponding property id.
 * Again, note that I am assuming that the field is a string.
 *
 * @param container
 * @param propertyIds
 * @param fields
 */
@SuppressWarnings("unchecked")
private static void addItem(IndexedContainer container, String[] propertyIds, String[] fields) {
    if (propertyIds.length != fields.length) {
        throw new IllegalArgumentException("Hmmm - Different number of columns to fields in the record");
    }
    Object itemId = container.addItem();
    Item item = container.getItem(itemId);
    for (int i = 0; i < fields.length; i++) {
        String propertyId = propertyIds[i];
        String field = fields[i];
        item.getItemProperty(propertyId).setValue(field);
    }
}


}

最佳答案

可能,这是一个依赖问题。您需要添加 commons-lang3 作为依赖。
您可以找到所需的 .jar 文件 here .

查询 this answer .

P.S.- 可能我应该把它放在评论中,但由于我的声誉很低,我不能。

关于java.lang.NoClassDefFoundError : au/com/bytecode/opencsv/CSVReader - Upload File Vaadin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25910553/

相关文章:

java - Object.toString() 如何获得 "memory address"以及如何模仿它

java - 将 zip 文件转换为 war 文件

javascript - 从 vaadin 中的 java 类调用 javascript 函数

java - 网格中的 Vaadin 过滤器

java - 如何在 Vaadin 中实现延迟加载树?

java - 如何计算/近似递归函数的堆栈帧使用的内存?

java - Vaadin 中的跨域验证策略

java - Vaadin上传功能

java - Vaadin:无法调用 com.vaadin.shared.ui.button.ButtonServerRpc 中的方法单击

java - AsyncEventHandler - 实时系统 Java