java - 使用多个外部 jar 文件编译和运行 java 程序

标签 java linux apache-poi javac javacompiler

我有一个简单的程序,它使用 apache.poi 外部库来操作 excel 文件。我在 windows 环境中使用了 eclipse,现在我必须使用终端在 linux 上编译和运行我的程序。我搜索了如何包含我需要的那些 jar ,但最终出现以下错误:

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

.(点)代表当前目录

关于java - 使用多个外部 jar 文件编译和运行 java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35314602/

相关文章:

node.js - 我如何在 Node JS 中通过 ssh 执行 sudo 命令?

java.lang.ClassNotFoundException : org. apache.xmlbeans.XmlOptions

java - 为什么 .doc 文件可以通过 Lucene 建立索引,而 .docx 文件无法建立索引?

java - 使用额外的细节增强对象

java - 如何使用 JSTL <c :set> 存储字符

linux - 如何使用 dnsmasq 来阻止站点并显示被阻止的页面?

excel - 为什么逻辑 IFS() 函数总是以小写形式呈现?

java - 注释或关键字告诉对象的垃圾收集器首先删除

java - Java 中的 W3C dom api

linux - 运行 EasyApache 时出错