如何解决“主线程” java.lang.NoClassDefFoundError中的异常:org / apache / poi / ss / usermodel / Row'

时间:2018-12-18 05:30:20

标签: java cmd jvm apache-poi

我现在面临的问题是我想导入一个excel文件并将其传递给这样的另一个类:

package cal;

import java.util.Scanner;

public class CalendarMain {
public static void main(String[] args) {

    Scanner link = new Scanner(System.in);
    System.out.println("Please insert the raw file link: ");
    String excellink = link.nextLine();
    link.close();

    String[] excelLink = new String[] { excellink };
    ShiftSetting.main(excelLink);
   }
}

编译并通过命令提示符运行它没有问题,但是当将原始文件链接粘贴到命令提示符并单击Enter时,会出现此错误:

C:\Users\User\eclipse-workspace\Calendar_v1\src>java cal.CalendarMain cal.ShiftSetting cal.PHNames
    Please insert the raw file link:
    C:\Users\User\Desktop\WORK\2019raw.xlsx
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
            at cal.CalendarMain.main(CalendarMain.java:16)
    Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Row
            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)
            ... 1 more

仅供参考,当我在Eclipse上运行它时,永远不会出现此问题。但是当我尝试在命令提示符下运行时,它给了我这个错误。第16行实际上就是这一行:ShiftSetting.main(excelLink);

我还提供了ShiftSetting.java文件

package cal;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ShiftSetting {
    public static void main(String[] excelLink) {
        ArrayList<String> shiftname = new ArrayList<String>();
        ArrayList<String> shiftcode = new ArrayList<String>();
        try {
            FileInputStream file = new FileInputStream(new File("C:\\Users\\User\\Desktop\\WORK\\shiftnameall.xlsx"));
            XSSFWorkbook wb = new XSSFWorkbook(file);
            XSSFSheet sheet = wb.getSheetAt(0);
            DataFormatter formatter = new DataFormatter();
            String cellValue = null;
            for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {
                Row r = sheet.getRow(rowNum);
                String val = null;
                for (int m = 0; m < r.getLastCellNum(); m = m + 2) {
                    Cell cell = r.getCell(m);
                    val = formatter.formatCellValue(cell);
                    shiftname.add(val);
                }
            }
            for (int rowNum = 0; rowNum < sheet.getLastRowNum() + 1; rowNum++) {
                Row r = sheet.getRow(rowNum);

                String val = null;
                for (int m = 1; m < r.getLastCellNum(); m++) {
                    Cell cell = r.getCell(m);
                    val = formatter.formatCellValue(cell);
                    shiftcode.add(val);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /* PHDatesArray.main(excelLink, shiftname, shiftcode); */
    }
}

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。实际上,这与APACHE POI的版本或系统变量中的PATH无关。只是我在命令提示符下的路径写错了。

我已将所有.jar文件放在同一文件夹中的一个文件夹中。

因此,我运行以下命令进行编译:

javac -cp jars/*;. *.java

然后运行它,我这样写

java -cp calendar/jars/*; calendar.nameofclass1, calendar.nameofclass2