java - “发生Java异常” Apache POI错误

标签 java apache crash

我用Java编写了一个程序,该程序从本地文件中提取一个excel表格,然后将给定的值上传到程序中进行一些计算。当我在eclipse中进行编译时,所有操作均按预期进行,但是当我创建可运行的jar文件时,出现弹出错误“发生Java异常”。我遍历了代码,如果我删除了从电子表格加载值的方法,则可运行的jar可以正常工作。但是,如果我放回代码以加载值,则会中断。在这段代码中发生了什么,导致整个程序中断?

    private static void loadValues() throws IOException{

    //FileInputStream file = new FileInputStream(new File("src/PricingData.xls"));
    FileInputStream file = new FileInputStream(new File("F:/Sales/Stitt/PricingData.xls"));


    HSSFWorkbook wb = new HSSFWorkbook(file);
    HSSFSheet sheet = wb.getSheetAt(0);

    Cell cell = sheet.getRow(1).getCell(1);
    MLPPSF = cell.getNumericCellValue();
    cell = sheet.getRow(2).getCell(1);
    COPPSF = cell.getNumericCellValue();

    cell = sheet.getRow(3).getCell(1);
    GPPSF = cell.getNumericCellValue();
    cell = sheet.getRow(4).getCell(1);
    UPFINISHED = cell.getNumericCellValue();
    cell = sheet.getRow(5).getCell(1);
    LOWFINISHED = cell.getNumericCellValue();
    cell = sheet.getRow(6).getCell(1);
    DPPSF = cell.getNumericCellValue();

    cell = sheet.getRow(8).getCell(1);
    onePanelWaterHeater = cell.getNumericCellValue();
    cell = sheet.getRow(9).getCell(1);
    twoPanelWaterHeater = cell.getNumericCellValue();
    cell = sheet.getRow(10).getCell(1);
    saferoomCost = cell.getNumericCellValue();
    cell = sheet.getRow(11).getCell(1);
    solarPVCost = cell.getNumericCellValue();
    cell = sheet.getRow(12).getCell(1);
    metlundCost = cell.getNumericCellValue();
    cell = sheet.getRow(13).getCell(1);
    ervCost = cell.getNumericCellValue();
    cell = sheet.getRow(14).getCell(1);
    gasFireplaceCost = cell.getNumericCellValue();
    cell = sheet.getRow(15).getCell(1);
    woodFireplaceCost = cell.getNumericCellValue();

    cell = sheet.getRow(18).getCell(1);
    allCabVanPercent = cell.getNumericCellValue();
    cell = sheet.getRow(19).getCell(1);
    allAppliancePercent = cell.getNumericCellValue();
    cell = sheet.getRow(20).getCell(1);
    allLightFixPercent = cell.getNumericCellValue();
    cell = sheet.getRow(21).getCell(1);
    allPlumbFixPercent = cell.getNumericCellValue();
    cell = sheet.getRow(22).getCell(1);
    allFloorCoveringPercent = cell.getNumericCellValue();

    cell = sheet.getRow(25).getCell(1);
    DepositPercent = cell.getNumericCellValue();
    cell = sheet.getRow(26).getCell(1);
    Draw1Percent = cell.getNumericCellValue();
    cell = sheet.getRow(27).getCell(1);
    Draw2Percent = cell.getNumericCellValue();
    cell = sheet.getRow(28).getCell(1);
    Draw3Percent = cell.getNumericCellValue();
    cell = sheet.getRow(29).getCell(1);
    Draw4Percent = cell.getNumericCellValue();
    cell = sheet.getRow(30).getCell(1);
    Draw5Percent = cell.getNumericCellValue();
    cell = sheet.getRow(31).getCell(1);
    Draw6Percent = cell.getNumericCellValue();
    cell = sheet.getRow(32).getCell(1);
    Draw7Percent = cell.getNumericCellValue();
    cell = sheet.getRow(33).getCell(1);
    Draw8Percent = cell.getNumericCellValue();
    cell = sheet.getRow(34).getCell(1);
    Draw9Percent = cell.getNumericCellValue();

}

主要是:
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    initialize();
    try {
        loadValues();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    showGUI();
}

编辑:

当我通过命令行运行它时,这是错误。
C:\Documents and Settings\Conference Room\SESI>java -jar SESI.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/use
rmodel/Cell
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Cell
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    ... 6 more

C:\Documents and Settings\Conference Room\SESI>

最佳答案

要找出问题所在,您需要实际查看异常。只需双击jar文件即可使用“javaw.exe”启动该文件,该文件没有控制台,因此仅报告存在异常-无需堆栈跟踪。

而是,启动命令提示符并“cd”到您的jar文件所在的位置。然后,假设jar名称为“MyJarFile.jar”,请运行以下命令:

java -jar MyJarFile.jar

当使用“java.exe”运行时,您的printStackTrace()实际上将具有一个控制台,在该控制台中可以打印堆栈跟踪,这将使您指向有问题的代码行并指出问题的类型。

UPDATE(在发布的异常之后):

嗯,由于您需要第三方依赖性,因此您需要切换到类似的方式,显式运行主类并指定包含所有必需类的类路径:
java -cp path\to\ApachePoiJarName.jar;path\to\MyAppJarFile.jar my.main.class.package.MyMainClass

...将“path \ to”,“ApachePoiJarName”,“MyAppJarFile”,“my.main.class.package”和“MyMainClass”替换为适当的名称。

如果POI jar依次需要其自己的依赖性,则也将这些文件的路径也添加到类路径(由分号分隔的jar路径列表,它们之间没有空格)。

关于java - “发生Java异常” Apache POI错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18084963/

相关文章:

java - 无法从上下文访问getContentResolver()?

java - processMessage 运行两次

java - 使用 Java 中设置的路径变量执行外部程序?

c++ - 在 Release模式下在 Xcode 中编译 .cpp 期间 clang 崩溃,但在调试中正常

php - 如何在www目录中创建符号链接(symbolic link)?

php - 如何删除安装在 Apache 中的较旧的 PHP 倍数?

Android AppcompatActivity 与 fragment 从后台调用后崩溃(需要等待几分钟)

java - 如何在 Web 服务上下文中存储信息?

java - 我怎样才能让这个开关只选择 3 个选项之一?

java - tomcat 显示 org.apache.catalina.LifecycleException