简体   繁体   English

Eclipse错误:“导入……无法解决”

[英]Eclipse error: “The import … cannot be resolved”

I know I am asking the very popular question. 我知道我在问这个非常受欢迎的问题。 But I can not find the solution to the problem. 但是我找不到解决问题的办法。 I have a sandbox to which I added a code of the unit test MulticurveBuildingDiscountingDiscountAUDTest.java file and commented it. 我有一个沙箱,向其中添加了单元测试MulticurveBuildingDiscountingDiscountAUDTest.java文件的代码并对其进行了注释。

Then I added the main method and I could successfully run the program (print something in a console). 然后,我添加了main方法,就可以成功运行该程序(在控制台中打印内容)。

Finally, I uncommented the code of the MulticurveBuildingDiscountingDiscountAUDTest.java file and I saw the following error: 最后,我取消注释了MulticurveBuildingDiscountingDiscountAUDTest.java文件的代码,并且看到以下错误:

The import com.opengamma.analytics.financial.instrument.index.GeneratorSwapFixedONMaster cannot be resolved.

And further in the code: 并在代码中进一步:

GeneratorSwapFixedONMaster cannot be resolved

I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path. 我知道此导入位于og-analytics src/test/java位置,我相信在构建路径中未列出任何位置。 I believe the problem is with a build path options and specially with classes like GeneratorSwapFixedONMaster which were created specially for tests. 我相信问题出在构建路径选项上,特别是对于专门为测试创建的类GeneratorGeneratorSwapFixedONMaster。 I have been playing around with cleaning, rebuilding projects, reinstalling and as a result updating the JRE. 我一直在进行清理,重建项目,重新安装以及更新JRE的工作。 I have visited these Import *** cannot be resolved [duplicate] and these Eclipse error: “The import XXX cannot be resolved” questions. 我已经访问了以下[ 导入***无法解决](重复)和以下Eclipse错误:“导入XXX无法解决”问题。 Do you know what shall I do to cure the following error? 您知道如何解决以下错误吗?

I have many problems with other imports from the original MulticurveBuildingDiscountingDiscountAUDTest.java file as well. 我也从原始MulticurveBuildingDiscountingDiscountAUDTest.java文件的其他导入中遇到很多问题。

Update : #1 is a location of my file. 更新 :#1是我文件的位置。 #2 is the location of classes this project uses. #2是该项目使用的类的位置。 The MulticurveBuildingDiscountingDiscountAUDTest.java file is taken from the src/test/java MulticurveBuildingDiscountingDiscountAUDTest.java文件来自src/test/java

屏幕截图

Update 2 : one may see that in Libraries I have included all the dependencies I might need (at least I do not know what else to add). 更新2 :可能会看到,在库中,我包含了我可能需要的所有依赖项(至少我不知道还要添加什么)。 The Maven Dependencies contains the hole og-analytics package: Maven依赖项包含Hole og-analytics软件包:

屏幕截图2

I know that this import is located in the og-analytics src/test/java location, which I believe is not listed anywhere in the build path. 我知道此导入位于og-analytics src / test / java位置,我相信在构建路径中未列出任何位置。

Perfectly explains your problem. 完美地解释您的问题。 In order to import any class, you must either have the source in your build path, or some directory that contains a compiled version of that class. 为了导入任何类,您必须在构建路径中包含源,或者在某个目录中包含该类的编译版本。 It is that simply. 就是这样。

The answer is: get clear on your project setup. 答案是:弄清项目设置。 If you intend to use classes from somewhere, you have to make them available somehow. 如果打算从某个地方使用类,则必须以某种方式使它们可用。 Otherwise it will not work. 否则它将无法正常工作。 In your case: if your tests want to make use a certain thing - then you should probably add that "thing" to your test project (you should avoid putting test-only stuff to your "product" projects). 以您的情况为例:如果您的测试想使用某个东西-那么您可能应该将该“东西”添加到测试项目中(应避免将仅测试的东西放到“产品”项目中)。

That is all there is to this. 这就是全部。

You included the source ( src ) folder og-analytics/src/main/java which contains the *.java files instead of the classes ( bin or classes ) folder with the *.class files (in your case, probably og-analytics/target/classes ). 你包含在源( src )文件夹 og-analytics/src/main/java其中包含了*.java文件,而不是类( binclasses )文件夹中*.class文件(在你的情况,可能是og-analytics/target/classes )。

But instead using Add Class Folder... you should add the project og-analytics in the tab Projects . 而是使用添加类文件夹...你应该添加项目og-analytics在该选项卡项目 Or even better, in the Maven pom.xml file add the dependency to the project og-analytics like you did for og-util . 甚至更好的是,像在og-util所做的那样,在Maven pom.xml文件中将依赖项添加到项目og-analytics

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

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