我有一个简单的程序,它使用apache.poi外部库来操作excel文件。我在windows环境中使用eclipse,现在我必须在linux上用终端编译和运行我的程序。我搜索了如何包含我需要的罐子,但最终出现了以下错误:
错误:无法找到或加载主类xlstocsv。
我遵循的步骤如下:
javac-cp./jars/poi-3.13-20150929.jar:./jars/poi-ooxml-schemas-3.13-20150929.jar:./jars/poi-ooxml-3.13-20150929.jar:./jars/xmlbeans-2.6.0.jar xlstocsv.java
java-cp./jars/poi-3.13-20150929.jar:./jars/poi-ooxml-schemas-3.13-20150929.jar:./jars/poi-ooxml-3.13-20150929.jar:./jars/xmlbeans-2.6.0.jar xlstocsv
我当前的目录是/home/demo/desktop/xls_to_csv,其中是xlstocsv.java文件。jar文件位于/home/demo/desktop/xls_to_csv/jars中。
有人能告诉我并解释正确的语法吗?可以调用包含所有jar文件的文件夹而不是单独调用它们吗?
提前谢谢。
编辑,我的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class xlsToCsv {
static void convert(File input, File output) throws Exception {
StringBuffer data = new StringBuffer();
FileOutputStream fos = new FileOutputStream(output);
HSSFWorkbook file = new HSSFWorkbook(new FileInputStream(input));
DataFormatter objDefaultFormat = new DataFormatter();
FormulaEvaluator objFormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) file);
Sheet sheet;
Row row;
Cell cell;
String sheetName, cellValue;
int cellType, rowIndex = 0, columnIndex = 0;
boolean blankRow = true, blankArea = true;
Iterator<Sheet> sheetIt;
Iterator<Row> rowIt;
Iterator<Cell> cellIt;
sheetIt = file.iterator();
while(sheetIt.hasNext()) {
sheet = sheetIt.next();
sheetName = sheet.getSheetName();
rowIt = sheet.iterator();
while(rowIt.hasNext()) {
row = rowIt.next();
cellIt = row.iterator();
while (cellIt.hasNext()) {
cell = cellIt.next();
cellType = cell.getCellType();
if(!isBlankCell(cellType) || !blankArea) {
if(rowIndex == 0 && columnIndex == 0)
data.append(sheetName + " - Header" + ";");
else if(rowIndex > 0 && columnIndex == 0)
data.append(sheetName + ";");
objFormulaEvaluator.evaluateInCell(cell);
cellValue = objDefaultFormat.formatCellValue(cell,objFormulaEvaluator);
if(cellValue.isEmpty())
data.append(";");
else
data.append(cellValue + ";");
columnIndex++;
blankRow = false;
blankArea = false;
}
}
if(!blankRow) {
data.append('\n');
rowIndex++;
}
blankRow = true;
columnIndex = 0;
}
//new sheet => reset control fields
rowIndex = 0;
columnIndex = 0;
blankRow = true;
blankArea = true;
}
fos.write(data.toString().getBytes());
fos.close();
}
private static boolean isBlankCell(int cellType) {
return cellType == Cell.CELL_TYPE_BLANK
|| cellType == Cell.CELL_TYPE_ERROR
|| cellType == Cell.CELL_TYPE_FORMULA;
}
public static void main(String[] args) {
if(args.length < 2 || args.length > 2) {
System.err.println("Insert input and output path");
System.exit(0);
}
File input = new File(args[0]);
File output = new File(args[1]);
try {
convert(input, output);
System.out.println("File " + output.getName() + "created sucessfully");
} catch(Exception e) {
e.printStackTrace();
}
}
}
最佳答案
如果要在包含运行/编译Java文件所需的所有JAR的同时添加目录,可以使用以下命令:
在Windows中:
java-classpath“;c:\ lib*”主类
在Unix/Linux中
java-classpath“./lib/*”主类
注意:在windows中,分号是分隔符,而在unix/linux中,分号是多个jar目录的分隔符。;
(点)表示当前目录
关于java - 编译并运行带有多个外部jar文件的Java程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35314602/