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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:24:13  来源:igfitidea点击:

cannot access the classes in the package

javapackagepackages

提问by PRP

I have the following directory structure :

我有以下目录结构:

MessManagementis the parent directory.

MessManagement是父目录。

Under that i have 3 directories : student, messconvener,messmanager

在此之下,我有 3 个目录 : student, messconvener,messmanager

The student directory contains the Student.javaand Student.classfiles. The messconvenercontains the MessConvener.javathis requires the Student classas MessConveneris extendedfrom Studentitself.

student 目录包含Student.javaStudent.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

发生这种情况有两个可能的原因

  1. 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.

  2. Are your classes public? If you forget the public modifier, classes will have package visibility and cannot be accessed from other packages.

  3. Oh well, nobody expects the spanish inquisition ... check your spelling carefully, including capitals.

  1. 您的目录的根目录是否包含在类路径中?启动java程序时需要指定。请参阅有关如何为 unix执行此操作此变体的文档

  2. 你的课是公开的吗?如果您忘记了 public 修饰符,类将具有包可见性并且不能从其他包访问。

  3. 哦,没有人期望西班牙宗教裁判所...仔细检查您的拼写,包括大写字母。

回答by smac89

The error you're getting makes sense. The Studentclass is located in MessManagementpackage not MessManagement.studentpackage.

你得到的错误是有道理的。本Student类位于MessManagement包不MessManagement.student包。

So either remove your import of Studentin MessManagementor change the package name in Studentto MessManagement.student.

因此,要么删除Studentin的导入,要么将MessManagement包名称更改StudentMessManagement.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 Leonid Ivankin

I deleted the outfolder and the problem was solved

我删除了out文件夹,问题解决了

回答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+Enterto 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 cleanhelped me out.

在 maven 项目中mvn clean帮助了我。