简体   繁体   English

Java类位于同一包(不同的目录)中,但它们不能彼此访问

[英]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: 我尝试过的

  • Rebuild project 重建项目
  • Invalidate cache/Restart 使缓存无效/重启
  • Reimport project 重新导入项目
  • Delete .iml files and .idea folder and import everything again 删除.iml文件和.idea文件夹,然后再次导入所有内容

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.

相关问题 为什么同一包中的类无法自动相互看见? - Why can't classes in the same package see each other automagically? 如何使两个不同的程序包访问彼此的类而又不允许任​​何其他第三个程序包在Java中访问它? - How to make two different packages access each other's classes without allowing any other third package to access it in java? 为什么同一个 package 中的班级没有相互协作? - Why classes in the same package aren't collaborating with each other? 有什么方法可以在Java中定义一个私有类,只有同一包中的其他类才能访问它? - Is there any way to define a private class in Java which only other classes in the same package can access it? 同一个包中的类不能相互通信 - Classes in the same package not communicating with each other 同一包,不同目录 - same package, different directories 如何在同一个包中访问Java类 - How to access Java classes in same package 一个Java类在同一包中找不到另一个 - A java class can't find an other in the same package 为什么Java中的“protected”修饰符允许访问同一个包中的其他类? - Why does the “protected” modifier in Java allow access to other classes in same package? 无法访问默认包中的类 - Can't access Classes in default package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM