使用多个外部 jar 文件编译和 运行 java 程序
Compile and run java program with mutiple external jar files
我有一个使用 apache.poi 外部库来操作 excel 文件的简单程序。我在 windows 环境中使用了 eclipse,现在我必须使用终端在 linux 上编译和 运行 我的程序。我搜索了如何包含我需要的那些罐子,但最终出现以下错误:
Error: Could not find or load main class 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*" MainClass
在 UNIX 中/Linux
java -classpath ".:/lib/*" MainClass
注意:在windows中;
(分号)是分隔符,而在UNIX/Linux中:
(冒号) 是目录
的多个 jar 的分隔符
.
(点)表示当前目录
我有一个使用 apache.poi 外部库来操作 excel 文件的简单程序。我在 windows 环境中使用了 eclipse,现在我必须使用终端在 linux 上编译和 运行 我的程序。我搜索了如何包含我需要的那些罐子,但最终出现以下错误:
Error: Could not find or load main class 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*" MainClass
在 UNIX 中/Linux
java -classpath ".:/lib/*" MainClass
注意:在windows中;
(分号)是分隔符,而在UNIX/Linux中:
(冒号) 是目录
.
(点)表示当前目录