主方法不会在Eclipse中结束

时间:2012-01-07 16:02:52

标签: java eclipse

我正在使用java(1.6)和WebDriver(2.16)在Eclipse(Indigo)中开发测试。

我有一个相当简单的测试运行良好,但实际上并没有'结束'。我的意思是,在执行'main'方法中的所有代码行之后,Eclipse控制台仍然指示代码正在运行(控制台中的红色'终止'按钮已启用)。

这只是在我用一些新代码扩展测试脚本后,昨天才开始发生的。想到这个问题,我评论了所有新代码。不幸的是,问题仍然存在。

如何解决此问题?

感谢您的意见。

修改

JStack线程转储的相关部分下面。有关完整转储

,请参阅here
  

2012-01-07 10:56:40全线程转储Java HotSpot(TM)64位服务器VM   (20.4-b02混合模式):

     

“Thread-7”守护程序prio = 6 tid = 0x0000000008c08800 nid = 0x12f4 runnable   [0x000000000 921f000] java.lang.Thread.State:RUNNABLE           at java.io.FileInputStream.readBytes(Native Method)           在java.io.FileInputStream.read(未知来源)           在org.apache.commons.exec.StreamPumper.run(StreamPumper.java:105)           在java.lang.Thread.run(未知来源)

     

“Thread-6”守护程序prio = 6 tid = 0x0000000008d7b800 nid = 0xb98 runnable   [0x0000000009 11f000] java.lang.Thread.State:RUNNABLE           at java.io.FileInputStream.readBytes(Native Method)           在java.io.FileInputStream.read(未知来源)           在java.io.BufferedInputStream.fill(未知来源)           在java.io.BufferedInputStream.read1(未知来源)           在java.io.BufferedInputStream.read(未知来源)            - 已锁定< 0x00000007d5a00888> (一个java.io.BufferedInputStream)           at java.io.FilterInputStream.read(Unknown Source)           在org.apache.commons.exec.StreamPumper.run(StreamPumper.java:105)           在java.lang.Thread.run(未知来源)

     

“Thread-5”prio = 6 tid = 0x000000000678b000 nid = 0x10e4 runnable   [0x000000000901f000] java.lang.Thread.State:RUNNABLE           at java.lang.ProcessImpl.waitFor(Native Method)           在org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecut   or.java:347)           在org.apache.commons.exec.DefaultExecutor.access $ 200(DefaultExecutor.ja   VA:46)           在org.apache.commons.exec.DefaultExecutor $ 1.run(DefaultExecutor.java:18   8)“main”prio = 6 tid = 0x000000000062e000 nid = 0x450 runnable   [0x000000000261f000] java.lang.Thread.State:RUNNABLE           在java.lang.Thread.exit(未知来源)

修改

这是我的Excel代码(使用java IO和Apache的POI类)

public HashMap<String, String> getTestData()
{
    InputStream myxls = null;

    try {
        // Create a connection to the Excel file
        myxls = new FileInputStream(fileName);
        System.out.println("Excel Input was opened");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
    }

    // Define a workbook object
    HSSFWorkbook wb = null;

    try {
        // Instantiate the workbook object
        wb = new HSSFWorkbook(myxls);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Create a worksheet object
    HSSFSheet sheet = wb.getSheet(sheetName);
    // Read the first row (it always contains the headers (variable names)
    HSSFRow headerRow = sheet.getRow(0);
    // Read the row requested
    HSSFRow row = sheet.getRow(dataRow);  

    //System.out.println("Creating a new HashMap from row " + dataRow + " in sheet " + sheetName);

    // Define a collection of value/value pairs
    HashMap<String,String> testData = new HashMap<String,String>();

    // Get count of columns != empty
    int columnCount = row.getPhysicalNumberOfCells();
    //System.out.println(Integer.toString(columnCount));

    String textHeader;
    String textData;

    // Loop through the columns
    for(int colcount=0; colcount < columnCount; colcount++)
    {
        // Read the column header and the cell value
        HSSFCell cell = row.getCell(colcount);
        HSSFCell headerCell = headerRow.getCell(colcount);

        switch (headerCell.getCellType ())
        {
            case HSSFCell.CELL_TYPE_NUMERIC :
            {
                // cell type numeric.
                textHeader = Double.toString(headerCell.getNumericCellValue());
                //System.out.println(textHeader);
                break;
            }
            case HSSFCell.CELL_TYPE_STRING :
            {
                // cell type string.
                HSSFRichTextString richTextString = headerCell.getRichStringCellValue();
                textHeader = richTextString.getString();
                break;
            }
            default :
            {
                // types other than String and Numeric.
                textHeader = "Type not supported";
                break;
            }

        }

        switch (cell.getCellType ())
        {
            case HSSFCell.CELL_TYPE_NUMERIC :
            {
                // cell type numeric.
                textData = Double.toString(cell.getNumericCellValue());
                break;
            }
            case HSSFCell.CELL_TYPE_STRING :
            {
                // cell type string.
                HSSFRichTextString richTextString = cell.getRichStringCellValue ();
                textData = richTextString.getString();
                break;
            }
            case HSSFCell.CELL_TYPE_BLANK :
            {
                // cell type string.
                textData = "";
                break;
            }
            default :
            {
                // types other than String and Numeric.
                textData = "Type not supported";
                break;
            }

        }


        // Add the value of each cell to the HashMap collection
        testData.put(textHeader,textData);
        //System.out.println(textHeader + " / " + textData);

    }

    try{
        // End the file object

        myxls.close();
        System.out.println("Excel Input was closed");
    }
    catch (IOException e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Send the HashMap back to the calling code
    return testData;

}

2 个答案:

答案 0 :(得分:2)

为了对此进行故障排除,您可以进行线程转储,例如使用jstack命令(此处有一个完整的SO question)。这允许您查看哪些线程仍在运行以及它们正在执行的代码部分。

修改

根据JStack输出,我会说你在单独的Thread上读取了一些文件,read方法仍在阻塞,等待javadoc of FileInputStream#read中记录的输入。

答案 1 :(得分:0)

在调试我的代码后,我相信我找到了罪魁祸首:

Selenium WebDriver类附带了几个“驱动程序”,每个支持的浏览器类型(IE,Firefox,Chrome)都有一个。当我创建一个Firefox对象时,它会尝试通过log4j启用日志记录(我没有配置,因为我没有使用它)。我相信log4j会导致挂起的线程。