Java 无法访问包中的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26857487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot access the classes in the package
提问by PRP
I have the following directory structure :
我有以下目录结构:
MessManagement
is the parent directory.
MessManagement
是父目录。
Under that i have 3 directories :
student
, messconvener
,messmanager
在此之下,我有 3 个目录 :
student
, messconvener
,messmanager
The student directory contains the Student.java
and Student.class
files. The messconvener
contains the MessConvener.java
this requires the Student class
as MessConvener
is extendedfrom Student
itself.
student 目录包含Student.java
和Student.class
文件。该messconvener
包含MessConvener.java
这需要Student class
作为MessConvener
扩展从Student
本身。
How should i do the packaging of the classes....??
我应该如何打包课程......?
What i have tried so far.
到目前为止我尝试过的。
Code :
代码 :
This is Student.java
这是 Student.java
package MessManagement;
import java.sql.*;
public class Student
{
}
This is MessConvener.java
这是 MessConvener.java
package MessManagement;
import MessManagement.student.Student;
public class MessConvener extends Student
{
}
But this does not seem to work.Error Meesage :
但这似乎不起作用。错误消息:
MessConvener.java:2: error: package MessManagement.student does not exist
import MessManagement.student.Student;
^
MessConvener.java:3: error: cannot find symbol
public class MessConvener extends Student
^
symbol: class Student
2 errors
MessConvener.java:2: error: package MessManagement.student does not exist
import MessManagement.student.Student;
^
MessConvener.java:3: error: cannot find symbol
public class MessConvener extends Student
^
symbol: class Student
2 errors
采纳答案by Jens Schauder
There are two possible reasons why this happens
发生这种情况有两个可能的原因
Is the root of your directories included in the classpath? You need to specify when starting a java program. See the documentation on how to do thator this variant for unix.
Are your classes public? If you forget the public modifier, classes will have package visibility and cannot be accessed from other packages.
Oh well, nobody expects the spanish inquisition ... check your spelling carefully, including capitals.
回答by smac89
The error you're getting makes sense. The Student
class is located in MessManagement
package not MessManagement.student
package.
你得到的错误是有道理的。本Student
类位于MessManagement
包不MessManagement.student
包。
So either remove your import of Student
in MessManagement
or change the package name in Student
to MessManagement.student
.
因此,要么删除Student
in的导入,要么将MessManagement
包名称更改Student
为MessManagement.student
.
For me, I got this error for a different reason and this was in a maven project. It began to occur after I made major changes to the classes and copied in a lot of new classes. There were no conflicting package names as in your case.
对我来说,我因为不同的原因得到这个错误,这是在一个 Maven 项目中。在我对课程进行重大更改并复制了许多新课程后,它开始发生。与您的情况一样,没有冲突的包名称。
The solution was to do mvn clean
.
解决办法是做mvn clean
。
After this, I didn't get that error anymore.
在此之后,我不再收到该错误。
回答by Sam Orozco
I recently had this issue and it was because I had a class file in the same directory as the java file that I was compiling. You need to delete all .class files in that directory.
我最近遇到了这个问题,这是因为我在编译的 java 文件所在的目录中有一个类文件。您需要删除该目录中的所有 .class 文件。
回答by Melissa Loos
This can occur when copying file from one project over to the other or when you move the files via refactor.
将文件从一个项目复制到另一个项目或通过重构移动文件时,可能会发生这种情况。
What helped for me in my Intellij IDEA/Gradle project was to remove the package line and use Shift+Enter
to replace it with the seemingly very same. If you got a lot of classes, you need to find the "root" of the dependencies and start there first.
Very odd solution, as a diff to the plaintext of these files would show nothing, but solves the problem.
Another solution can be to use Refactor-Rename on the package, give it a buffername and then name it back again like you want it to be called.
I haven't looked deeper into why this occurs.
在我的 Intellij IDEA/Gradle 项目中对我有帮助的是删除包线并用Shift+Enter
看似非常相同的来替换它。如果您有很多类,则需要找到依赖项的“根”并首先从那里开始。非常奇怪的解决方案,因为与这些文件的纯文本的差异不会显示任何内容,但可以解决问题。另一种解决方案是在包上使用 Refactor-Rename,给它一个缓冲区名称,然后像你希望它被调用一样重新命名它。我还没有深入研究为什么会发生这种情况。
In maven project mvn clean
helped me out.
在 maven 项目中mvn clean
帮助了我。