错误:包org.apache.commons.io.filefilter不存在

时间:2014-05-19 05:39:46

标签: java apache

我有一个Java文件“RemoteXMLRead.java”,其中包含以下行:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileFilter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RemoteXMLRead {

public static void main(String argv[]) {
    Connection conn = null;

    try {

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url3 = "jdbc:sqlserver://127.0.0.1:1433;databaseName=TestPerfomenceTrack";
        conn = DriverManager.getConnection(url3, "reliantUser", "F4C78266-021e");

        Statement stmt = conn.createStatement();
        String sql = "";

        List<File> files = listf("C:\\jenkins\\jobs\\AdvBackOffice\\builds");

        File dir = new File(files.get(258), sql);
        FileFilter fileFilter2 = new WildcardFileFilter("*.xml");
        File[] files2 = dir.listFiles(fileFilter2);

        File fXmlFile = files2[1];
        System.out.println(fXmlFile);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);


        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName("case");

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                sql="EXEC [spXMLDataForTestCasesTrack] " +
                        "'"+eElement.getElementsByTagName("duration").item(0).getTextContent()+"'," +
                        "'"+eElement.getElementsByTagName("className").item(0).getTextContent()+"'," +
                        "'"+eElement.getElementsByTagName("testName").item(0).getTextContent()+"'," +
                        "'"+eElement.getElementsByTagName("skipped").item(0).getTextContent()+"'," +
                        ""+eElement.getElementsByTagName("failedSince").item(0).getTextContent()+"";
                stmt.executeUpdate(sql);



                /*--EXEC [spXMLDataForTestCasesTrack] '123.00','test class name','test test name','test skipped',3
                  --SELECT * FROM [dbo].[ExecutionTimeTrack]*/

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static List<File> listf(String directoryName) {
    File directory = new File(directoryName);

    List<File> resultList = new ArrayList<File>();

    // get all the files from a directory
    File[] fList = directory.listFiles();
    resultList.addAll(Arrays.asList(fList));
    for (File file : fList) {
        if (file.isFile()) {
            System.out.println(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            resultList.addAll(listf(file.getAbsolutePath()));
        }
    }
    //System.out.println(fList);
    return resultList;
}

}

这在Eclipse中运行良好。但是,使用

运行相同的程序

javac RemoteXMLRead.java会导致以下错误:

RemoteXMLRead.java:4: error: package org.apache.commons.io.filefilter does not exist
import org.apache.commons.io.filefilter.WildcardFileFilter;
                                   ^
RemoteXMLRead.java:35: error: cannot find symbol
                    FileFilter fileFilter2 = new WildcardFileFilter("*.xml");
                                                 ^
symbol:   class WildcardFileFilter
location: class RemoteXMLRead
2 errors

为什么错误出现在javac而不是Eclipse?

1 个答案:

答案 0 :(得分:0)

这是因为您使用eclipse创建此项目并尝试使用CMD运行。 当您使用eclipse时,它会自动为您添加的库生成路径和其他相关内容。因此,当您尝试使用CMD运行它时,它无法正确加载库。因此,您必须手动创建项目以使用CMD运行。