1

Though I import all packages it can't work. I am new in java.

Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/commons/collections4/ListValuedMap at demo.ReadAndPrintXMLFile.main(ReadAndPrintXMLFile.java:101) Caused by:java.lang.ClassNotFoundException: org.apache.commons.collections4.

package demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ReadAndPrintXMLFile {
    private static XSSFWorkbook workbook;

    public static void main(String argv[]) {

        ArrayList<String> firstNames = new ArrayList<String>();
        ArrayList<String> lastNames = new ArrayList<String>();
        ArrayList<String> ages = new ArrayList<String>();

        try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("D:/amit/book.xml"));

            // normalize text representation
            doc.getDocumentElement().normalize();
            System.out.println("Root element of the doc is :\" "+ doc.getDocumentElement().getNodeName() + "\"");
            NodeList listOfPersons = doc.getElementsByTagName("person");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of people : " + totalPersons);
            for (int s = 0; s < listOfPersons.getLength(); s++) 
            {
                Node firstPersonNode = listOfPersons.item(s);
                if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) 
                {
                    Element firstPersonElement = (Element) firstPersonNode;
                    NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                    Element firstNameElement = (Element) firstNameList.item(0);
                    NodeList textFNList = firstNameElement.getChildNodes();
                    System.out.println("First Name : "+ ((Node) textFNList.item(0)).getNodeValue().trim());
                    firstNames.add(((Node) textFNList.item(0)).getNodeValue().trim());
                    NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                    Element lastNameElement = (Element) lastNameList.item(0);
                    NodeList textLNList = lastNameElement.getChildNodes();
                    System.out.println("Last Name : "+ ((Node) textLNList.item(0)).getNodeValue().trim());
                    lastNames.add(((Node) textLNList.item(0)).getNodeValue().trim());
                    NodeList ageList = firstPersonElement.getElementsByTagName("age");
                    Element ageElement = (Element) ageList.item(0);
                    NodeList textAgeList = ageElement.getChildNodes();
                    System.out.println("Age : "+ ((Node) textAgeList.item(0)).getNodeValue().trim());
                    ages.add(((Node) textAgeList.item(0)).getNodeValue().trim());
                }// end of if clause

            }// end of for loop with s var
            for(String firstName:firstNames)
            {
                System.out.println("firstName : "+firstName);
            }
            for(String lastName:lastNames)
            {
                System.out.println("lastName : "+lastName);
            }
            for(String age:ages)
            {
                System.out.println("age : "+age);
            }

        } 
        catch (SAXParseException err) 
        {
            System.out.println("** Parsing error" + ", line "+ err.getLineNumber() + ", uri " + err.getSystemId());
            System.out.println(" " + err.getMessage());
        } 
        catch (SAXException e) 
        {
            Exception x = e.getException();
            ((x == null) ? e : x).printStackTrace();
        } 
        catch (Throwable t) 
        {
            t.printStackTrace();
        }

        workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Sample sheet");

        Map<String, Object[]> data = new HashMap<String, Object[]>();
        for(int i=0;i<firstNames.size();i++)
        {
            data.put(i+"",new Object[]{firstNames.get(i),lastNames.get(i),ages.get(i)});
        }
        Set<String> keyset = data.keySet();
        int rownum = 0;
        for (String key : keyset) {
            Row row = sheet.createRow(rownum++);
            Object[] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof Date)
                    cell.setCellValue((Date) obj);
                else if (obj instanceof Boolean)
                    cell.setCellValue((Boolean) obj);
                else if (obj instanceof String)
                    cell.setCellValue((String) obj);
                else if (obj instanceof Double)
                    cell.setCellValue((Double) obj);
            }
        }

        try {
            FileOutputStream out = new FileOutputStream(new File("D:/amit/book1.xlsx"));
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }// end of main
}
lorond
  • 3,856
  • 2
  • 37
  • 52

1 Answers1

0

You are missing a dependency to org.apache.commons.collections4. This might be because of your other dependencies (which should have the collections4 dependency in theirs, but apparently there is still some sort of error).

You can try to add the JAR to the path, or if you use any build tools (Maven, Gradle, Ivy, etc.) you can add it to the dependencies list. Try and add this dependency: https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1

Turbut Alin
  • 2,568
  • 1
  • 21
  • 30
  • I added that jar already and build path but it still showing the same error.It just showing me plane output in eclipse but it is not creating the excel file which I want. –  Dec 08 '16 at 07:28
  • Have a look here and try to interpret in what you have: http://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java. Basically, you are getting a `NoClassDefFoundError` which is different than `ClassNotFoundError`, but it signifies that `ListValuedMap` is not present at runtime, but is at compile time. – Turbut Alin Dec 08 '16 at 07:30
  • Root element of the doc is :" book" Total no of people : 3 First Name : hussain Last Name : hussain Age : 21 First Name : akhtar Last Name : akhtar Age : 22 First Name : wahid Last Name : wahid Age : 23 firstName : hussain firstName : akhtar firstName : wahid lastName : hussain lastName : akhtar lastName : wahid age : 21 age : 22 age : 23 and remaining output is I am mentioning in following comment –  Dec 08 '16 at 07:35
  • Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap at demo.ReadAndPrintXMLFile.main(ReadAndPrintXMLFile.java:101) Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) –  Dec 08 '16 at 08:27