简体   繁体   English

在Eclipse中导入库

[英]Importing Library in Eclipse

I have an Eclipse project where I imported the library "OpenCSV" as an external JAR. 我有一个Eclipse项目,我将库“OpenCSV”作为外部JAR导入。 The library is now shown in my project under "referenced libraries". 该库现在显示在我的项目“参考库”下。

However, when I call: 但是,当我打电话时:

CSVReader reader = new CSVReader(new FileReader(csvPath));

Eclipse throws an error saying that the constructor CSVReader is not defined. Eclipse抛出一个错误,指出没有定义构造函数CSVReader。 csvPath is of type String. csvPath的类型为String。

Any thoughts? 有什么想法吗?

EDIT: Screenshot 编辑:截图

我的工作区

I may be mistaken (I don't use this library and don't know history of its package names) but it looks like autoEvoSuite is your own package. 我可能弄错了(我不使用这个库,也不知道它的包名的历史)但看起来autoEvoSuite是你自己的包。

If that is the case then you have class name conflict (actually there is no conflict, you are just using wrong class) since your class is also named CSVReader so inside method readCVS you are not calling constructor of au.com.bytecode.opencsv.CSVReader , but constructor of your own class autoEvoSuite.CSVReader , and since your class doesn't have 如果是这种情况, 那么你有 类名冲突 (实际上没有冲突,你只是使用了错误的类)因为你的类也被命名为CSVReader所以在方法readCVS你不是在调用au.com.bytecode.opencsv.CSVReader构造au.com.bytecode.opencsv.CSVReader ,但是你自己的类autoEvoSuite.CSVReader构造autoEvoSuite.CSVReader ,因为你的类没有

public CSVReader(FileReade reader){...}

constructor, compiler informs you about this problem. 构造函数,编译器通知您这个问题。

To solve this problem consider renaming your class, or be explicit and say which class exactly you want to use by writing its full-package-name like 要解决这个问题,请考虑重命名您的类,或者通过编写其完整的包名来明确说明您要使用哪个类

au.com.bytecode.opencsv.CSVReader reader = new au.com.bytecode.opencsv.CSVReader(new FileReader(csvPath));

you have to try and catch FileNotFoundException and see the code below. 你必须尝试捕获FileNotFoundException并查看下面的代码。

public FileReader(String fileName) throws FileNotFoundException {
    super(new FileInputStream(fileName));
}

you can modify it like following 你可以像下面这样修改它

   try {
        CSVReader reader = new CSVReader(new FileReader(csvPath));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

You are calling a constructor for your own class CSVReader that doesn't exist! 您正在调用不存在的类CSVReader的构造函数! Either rename your class and create a new instance of au.com.bytecode.opencsv , or delete the respective line, make sure you import au.com.bytecode.opencsv.CSVReader , and re-create the line. 重命名您的类并创建au.com.bytecode.opencsv的新实例,或删除相应的行,确保导入au.com.bytecode.opencsv.CSVReader ,然后重新创建该行。

Alternatively - but I'm unsure whether this is what you'd want - you can make your CSVReader (optimally under another name), extend au.com.bytecode.opencsv.CSVReader and override the respective constructor. 或者 - 但我不确定这是否是你想要的 - 你可以制作你的CSVReader (最好用另一个名字),扩展au.com.bytecode.opencsv.CSVReader并覆盖相应的构造函数。

I guess your problem was caused when you used auto-complete for CSVReader and didn't pick the class from the right package? 我猜您的问题是由于您使用CSVReader自动完成并且没有从正确的包中选择类时引起的?

You need to add the relevant JAR in the project's Build Path in order to have it built. 您需要在项目的构建路径中添加相关的JAR才能构建它。

Your Java Project (Right Click)--> Properties --> Java Build Path --> Libraries --> Add JARs/Add External JARs 您的Java项目(右键单击) - >属性 - > Java构建路径 - >库 - >添加JAR /添加外部JAR

在此输入图像描述

我在这里列出了我的完整解决方案 ,尝试使用CSVParser ,这可以从Apache Commons获得并且更容易使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM