即使在类路径中包含jar文件后,也无法在运行时找到类

时间:2015-06-30 08:19:22

标签: java excel jar apache-poi hssf

我正在尝试使用 apache poi api 。我已经下载了jar库。这是我写的代码。该文件的名称是 Main.java

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        File inputFile = new File(".\test.xlsx");
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
        HSSFSheet sheet = workbook.getSheetAt(0);
        Cell cell;
        Row row;
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()){
            row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()){
                cell = cellIterator.next();
                System.out.println(cell.getStringCellValue());
            }
        }
    }
}

这就是我运行编码文件的代码。

javac -cp "./poi-3.12-20150511.jar;./poi-ooxml-3.12-20150511.jar;./poi-ooxml-schemas-3.12-20150511.jar" Main.java

编译时我没有收到任何错误。但是当我尝试运行它时,我得到了 HSSFWorkbook 的类找不到异常。我做错了什么。

PS - 所有jar文件和我的java代码都在同一个文件夹中。

1 个答案:

答案 0 :(得分:3)

试试这个:

java -cp "poi-3.12-20150511.jar;poi-ooxml-3.12-20150511.jar;poi-ooxml-schemas-3.12-20150511.jar;." Main