簡體   English   中英

僅在TestCases中獲取NoClassDefFoundError

[英]Getting NoClassDefFoundError only with TestCases

我有一類創建了excel表格,為此我在類路徑中添加了poi-3.2.jar。 我正在使用Eclipse3.7。
我通過記錄與WindowTester Pro一起使用的工作台操作來生成測試用例。 我編輯了生成的測試用例,並嘗試在其中創建excel表以編寫測試結果。

如果我從普通的Java程序中使用相同的API(在該插件本身內),則它可以創建Excel文件而不會出錯。 但是,當我嘗試從測試用例創建時,出現以下錯誤:

java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook

我已經將所需的jar文件添加到lib文件夾中,並添加到了構建路徑中。 為什么它可以從普通的Java程序運行,而不能從WindowTester生成測試用例呢?

這是我的代碼:

import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.swt.UITestCaseSWT;
import com.windowtester.runtime.swt.locator.eclipse.WorkbenchLocator;

public class Configuration extends UITestCaseSWT {

/*
 * @see junit.framework.TestCase#setUp()
 */
protected void setUp() throws Exception {
    super.setUp();
    IUIContext ui = getUI();
    try {
        CreateExcelFile file = new CreateExcelFile();
        file.CreateExcel();
    } catch (Throwable e) {
        System.out.println("***** Exception catched.. *****");
        // fail("Configuration failed");
        e.printStackTrace();
    }
    ui.ensureThat(new WorkbenchLocator().hasFocus());
}

/**
 * Main test method.
 */
public void testConfiguration() throws Exception {

    //Test code
    }

@Override
protected void tearDown() throws Exception {
    super.tearDown();
    // ....
}

}


public class CreateExcelFile {
HSSFRow row;
HSSFRichTextString string;

public void CreateExcel() {
    try {
        String filename = "E:/hello.xls";
        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet("new sheet");

        HSSFRow rowhead = sheet.createRow((short) 0);
        string = new HSSFRichTextString("TC_Name");
        rowhead.createCell((int) 0).setCellValue(string);
        string = new HSSFRichTextString("Result");
        rowhead.createCell((int) 1).setCellValue(string);

        for (int i = 0; i <= 10; i++) {
            row = sheet.createRow((short) i);
            string = new HSSFRichTextString("TC_Name" + i);
            row.createCell((int) 0).setCellValue(string);
            if (i % 2 == 0) {
                row.createCell((int) 1).setCellValue(true);
            } else {
                row.createCell((int) 1).setCellValue(false);
            }
        }
        FileOutputStream fileOut = new FileOutputStream(filename);
        hwb.write(fileOut);

        fileOut.close();
        System.out.println("Your excel file has been generated!");
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

}

誰能幫我。

我有問題原因。 這是因為運行時的類路徑。我找到的解決方案是在清單編輯器中打開MANIFEST.MF。然后在運行時選項卡的Classpath部分中添加了必需的JAR。
現在,它的工作沒有錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM