即使在 class 路径中包含 jar 文件后,也无法在运行时找到 class
Cannot find class on runtime even after including the jar files in the classpath
我正在尝试使用 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
我在编译时没有收到任何错误。但是当我尝试 运行 时,我得到 class 未找到 HSSFWorkbook 的异常。我做错了什么。
PS - 所有 jar 文件和我的 java 代码都在同一个文件夹中。
试试这个:
java -cp "poi-3.12-20150511.jar;poi-ooxml-3.12-20150511.jar;poi-ooxml-schemas-3.12-20150511.jar;." Main
我正在尝试使用 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
我在编译时没有收到任何错误。但是当我尝试 运行 时,我得到 class 未找到 HSSFWorkbook 的异常。我做错了什么。
PS - 所有 jar 文件和我的 java 代码都在同一个文件夹中。
试试这个:
java -cp "poi-3.12-20150511.jar;poi-ooxml-3.12-20150511.jar;poi-ooxml-schemas-3.12-20150511.jar;." Main