1

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();
        }
    }
}

1 Answers1

2

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

Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • I'm having a problem. If I don't change directory to the folder where my .java file is, what is suppose to be the command? I'm asking because I compiled the file as well but when I try to run it give me the following error: > Error: Could not find or load main class .home.demo.Desktop.xls_to_csv.xlsToCsv. What I did was: > javac -classpath ".:/home/demo/Desktop/xls_to_csv/jars/*" /home/demo/Desktop/xls_to_csv/xlsToCsv.java. > java -classpath ".:/home/demo/Desktop/xls_to_csv/jars/*" /home/demo/Desktop/xls_to_csv/xlsToCsv. – Alexandre Silva Feb 10 '16 at 14:50
  • your problem is not clear, I assume you have defined some package structure in your .java file, if so you will have to use `-p` command with that package structure. If not it is required to go to that location to compile your .java file otherwise how javac or java command with know where your .java files are? – Vishrant Feb 10 '16 at 19:29
  • I wrote the problem down. It's here. http://stackoverflow.com/questions/35337793/compile-and-run-java-program-in-linux-with-path-to-the-java-file-and-path-to-ex – Alexandre Silva Feb 11 '16 at 12:08