简体   繁体   English

导入无法在Java中解决

[英]Import cannot be resolved in java

在此处输入图片说明 I am getting: 我正进入(状态:

import com.test.foo.A cannot be resolved 

IDE I am using is NWDS. 我正在使用的IDE是NWDS。

I have a class A which is declared as public, I have a class B which is declared as public final. 我有一个宣布为公开的A类,我有一个宣布为公开决赛的B类。 Both are declared in different packages. 两者都在不同的包中声明。

Class A in com.test.foo com.test.foo中的A类

Class B in com.test.foo1 com.test.foo1中的B类

I am trying to import a public method from class A to class B but I am getting the above mentioned error in IDE. 我正在尝试将公共方法从类A导入到类B,但在IDE中出现了上述错误。

Both are in different projects. 两者都在不同的项目中。

Code snippet as below :- 程式码片段如下:

package com.test.foo 

public class A {

    public static void method1(){
    ....some code ....
    }

}

-----

package com.test.foo1

import com.test.foo.A // i'm getting error here as import cannot be resolved

public final class B {

    private method2(){

    ...... some code....
     A.method1(); // import cannot be resolved error
    }
}

Can any help help on this? 有什么可以帮助的吗?

Thanks in advance for you help :) 在此先感谢您的帮助:)

In your image you have the class ClassA not A . 在您的图像中,类为ClassA而不是A。

So the error is: 所以错误是:

 "import com.test.foo.A cannot be resolved"

You should import the class ClassA . 您应该导入ClassA类。

package com.test.foo1

import com.test.foo.ClassA;

public final class B {

  private method2(){

    //...... some code....
    ClassA.method1(); 
  }
}

This is like trying to copy text by clicking "copy" button on a computer, and clicking "paste" button on another computer. 这就像尝试通过单击计算机上的“复制”按钮,然后单击另一台计算机上的“粘贴”按钮来复制文本。 Quite amusing. 很好玩。 :-) :-)

  • Well, a general Java way is you build the project that contains A as a JAR, and use it as a library in another project that contains B. 好吧,一种通用的Java方法是构建包含A作为JAR的项目,并将其用作包含B的另一个项目中的库。

  • Or if you organize your project using Maven, you can import another project as dependency. 或者,如果您使用Maven组织项目,则可以导入另一个项目作为依赖项。 Something like: 就像是:

?

<project>
   ...
   <dependencies>
      <dependency>
         <groupId>yourgroup</groupId>
         <artifactId>myejbproject</artifactId>
         <version>2.0</version>
         <scope>system</scope>
         <systemPath>path/to/myejbproject.jar</systemPath>
      </dependency>
   </dependencies>
   ...
</project>

解决给定问题,请点击以下链接stackoverflow.com/a/48381463/5093657

即使您导入了正确的类并且仍然出现此错误,也请使用此技巧。

Remove the import statement and re-import it.

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

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