[英]Java classes are in the same package (different directories) but they can't access each other
I have two files: 我有两个文件:
MyProject/src/main/java/foo_package/bar_package/MainClass.java MyProject/src/gen/java/foo_package/bar_package/OtherClass.java MyProject / src / main / java / foo_package / bar_package / MainClass.java MyProject / src / gen / java / foo_package / bar_package / OtherClass.java
In both of those classes the very first line is: 在这两个类中,第一行是:
package foo_package.bar_package;
If I call: 如果我打电话给:
OtherClass foo = new OtherClass();
It cannot resolve symbol OtherClass. 它无法解析符号OtherClass。 Why is that?
这是为什么?
What I've tried: 我尝试过的
It looks like there is a problem with the path of the last class: 上一堂课的路径似乎有问题:
MyProject/src/ main /java/foo_package/bar_package/MainClass.java MyProject/src/ gen /java/foo_package/bar_package/OtherClass.java
MyProject / src / main /java/foo_package/bar_package/MainClass.java MyProject / src / gen /java/foo_package/bar_package/OtherClass.java
If your classes have the same package (package starts after ../java/
) but they are not part of the java build path, then the IDE won't recognise them as valid. 如果您的类具有相同的程序包(程序包在
../java/
之后../java/
),但它们不是Java构建路径的一部分,则IDE不会将其识别为有效。
Try moving your OtherClass.java
to the package where the MainClass.java
is. 试试你的移动
OtherClass.java
到的包MainClass.java
是。 Doing this should eventually solve your problem. 这样做最终应该可以解决您的问题。
PS: be aware about the source folders of your project (most of the time main
is the source folder by default and it's enough but there may be other source folders, generally added manually). PS:请注意项目的源文件夹(大多数情况下,默认情况下,
main
是源文件夹,足够了,但是可能还有其他源文件夹,通常是手动添加的)。
Make sure that your IDEA source folder is java
, not src
(for both java
folders inside /gen/
and inside /main
). 确保您的IDEA源文件夹是
java
,而不是src
(对于/gen/
内部和/main
内部的两个java
文件夹)。 The sources root is marked as a blue directory in "Project" window ( Alt + 1
). 在“项目”窗口(
Alt + 1
)中,源根目录被标记为蓝色目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.