Reputation: 35
I have a simple program that uses apache.poi external libraries in order to manipulate excel files. I used eclipse in a windows environment and now I have to compile and run my program on linux with terminal. I searched how to include those jars that I need but I ended up with the following error:
Error: Could not find or load main class xlsToCsv.
The steps that I followed are this:
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
My current directory is /home/demo/Desktop/xls_to_csv where is the xlsToCsv.java file. The jar files are in /home/demo/Desktop/xls_to_csv/jars.
Anyone can tell me and explain the right syntax? Is possible to call a folder with all jar files instead of call them individualy?
Thanks in advance.
EDIT, MY CODE:
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();
}
}
}
Upvotes: 1
Views: 2247
Reputation: 16628
If you want to add directory while contains all the required jars for running/ compiling your Java file you can use below command:
In Windows:
java -classpath ".;c:\lib*" MainClass
In UNIX/ Linux
java -classpath ".:/lib/*" MainClass
Note: In windows ;
(semicolon) is the separator, while in UNIX/ Linux :
(colon) is the separator for multiple jar for directory
.
(dot) represents current directory
Upvotes: 2