0

I am using Apache POI for reading excel file (in xls and xlsx format). It runs fine in IDE (I am using IntelliJ), but when I make the project to JAR, it can't read the excel file. I tried by giving full path of excel file, but not working. File name spelling is right, file is not protected or read only format. Everything is good until I run JAR.

java.lang.reflect.InvocationTargetException
    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.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    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 sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
    at TakeInput.Take(TakeInput.java:25)
    at MainGUI.main(MainGUI.java:53)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more
Exception running application MainGUI

Here is my TakeInput Class:

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class TakeInput {

public void Take() throws Exception{
    System.out.println("Inside TakeInput class..");

    File f = new File("courses.xls");
    FileInputStream fs = new FileInputStream(f);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);


    int CSECourseCounter=0;

    for(int i=0;i<=sheet.getLastRowNum();i++){
        String tempCode = sheet.getRow(i).getCell(0).getStringCellValue();
        StringTokenizer tok = new StringTokenizer(tempCode);

        String codeTok=tok.nextToken();

        if(codeTok.equals("CSE") || codeTok.equals("CSI")) {
            CSECourseCounter++;
        }

    }

    CourseInfo[] courses = new CourseInfo[CSECourseCounter];
    for(int i=0;i<CSECourseCounter;i++)
        courses[i]=new CourseInfo();

    System.out.println(sheet.getLastRowNum());


    for(int i=0,j=0;i<=sheet.getLastRowNum();i++){
        String tempCode = sheet.getRow(i).getCell(0).getStringCellValue();
        StringTokenizer tok = new StringTokenizer(tempCode);

        String codeTok=tok.nextToken();

        if(codeTok.equals("CSE") || codeTok.equals("CSI")) {
            courses[j].code = sheet.getRow(i).getCell(0).getStringCellValue();
            courses[j].title = sheet.getRow(i).getCell(1).getStringCellValue();
            courses[j].section = sheet.getRow(i).getCell(2).getStringCellValue();
            courses[j].day = sheet.getRow(i).getCell(4).getStringCellValue();
            courses[j].time = sheet.getRow(i).getCell(5).getStringCellValue();
            //courses[i].room = sheet.getRow(i).getCell(6).getNumericCellValue();
            //System.out.println(courses[j].code);
            j++;
            //CSECourseCounter++;
        }
    }

    Arrays.sort(courses, new Comparator<CourseInfo>() {
        @Override
        public int compare(CourseInfo o1, CourseInfo o2) {
            return o1.title.compareTo(o2.title);
        }
    });

    System.out.println("CSE courses = "+CSECourseCounter);

    for(int i=0;i<CSECourseCounter;i++){
        courses[i].setCampus();
        courses[i].setLabCLassAndTimeSlot();
        courses[i].setDay();
    }



    //***************************** CourseInfo to a linked list ******************

    for(int i=0;i<courses.length;i++){

        CourseToLinkedList temp = AddSearchCourse.searchCourse(courses[i].code);

        if(temp==null) {
            AddSearchCourse.addCourse(courses[i].title,courses[i].code,courses[i].islabClass);
            CourseToLinkedList temp1 = AddSearchCourse.searchCourse(courses[i].code);
            temp1.addSections(courses[i].code,courses[i].title,courses[i].section,courses[i].day,courses[i].time,
                    courses[i].timeStart,courses[i].timeEnd,courses[i].room,
                    courses[i].faculty,courses[i].credit,courses[i].assigned,
                    courses[i].campus,courses[i].dept,courses[i].islabClass,
                    courses[i].timeSlot,courses[i].labTimeSlot,courses[i].day1,courses[i].day2
            );
        }
        else{
            CourseToLinkedList temp1 = AddSearchCourse.searchCourse(courses[i].code);
            temp1.addSections(courses[i].code,courses[i].title,courses[i].section,courses[i].day,courses[i].time,
                    courses[i].timeStart,courses[i].timeEnd,courses[i].room,
                    courses[i].faculty,courses[i].credit,courses[i].assigned,
                    courses[i].campus,courses[i].dept,courses[i].islabClass,
                    courses[i].timeSlot,courses[i].labTimeSlot,courses[i].day1,courses[i].day2
            );
        }
    }

    System.out.println("outside try catch..");
}
}

I put a copy of this excel file which is in the project folder to that folder where JAR file situated. No problem with location.

  • Possible duplicate of [How do I resolve this Java Class not found exception?](http://stackoverflow.com/questions/17408769/how-do-i-resolve-this-java-class-not-found-exception) – fabian Jul 18 '16 at 08:34

1 Answers1

0

As I can see the logs, this issue may happen because of dependency jars for your code. try to add the jar(may be 'jakarta-poi-version.jar') having class 'HSSFWorkbook' into your class path and then try to run it again.

  • I added all external JAR of Apache POI 3.14 Should I have to add jakarta-poi-version.jar again? – Mohammad Maruf Imtiaze Jul 18 '16 at 04:59
  • No just try to add that jar into your java class path variable first and see weather it is working r not since according to the error it seems like your code can not find the desired class. – Ashish Sharma Jul 18 '16 at 05:35