Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot access the classes in the package

I have the following directory structure :

MessManagement is the parent directory.

Under that i have 3 directories : 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 extended from Student itself.

How should i do the packaging of the classes....??

What i have tried so far.

Code :

This is Student.java

package MessManagement;
import java.sql.*;

public class Student
{

}

This is 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

like image 928
Rohith R Avatar asked Nov 11 '14 04:11

Rohith R


People also ask

Can a class be accessed outside of a package?

No one can access classes outside of the package, except for your own classes, which are implemented in the package. 6. What does ‘public’ access type mean for a class implemented in a package? The class in the package can be publicly available for use in other external libraries.

How do you find the class of a package?

The word ‘class’ is preceded by the ‘public’ access modifier. The class is accessed using the package name and the name of this class, separated by the symbol ‘ . ‘; the ‘package’ access level.

How to fix cannot find classes in same package issue?

The issue can be summarized as attaching the jar file source fin 'B' module to a dependency causes the "cannot find classes in same package issue" in the module 'A' whose source is referenced. 2. Open module B dependencies dialog, attach sources to module A source dir Select Build -> Rebuild Project, it works for me!

Why can't I create a classa1 class object in the module?

In the module, a ClassB2 class object is correctly created, because the ClassB1 and ClassB2 classes are in the same PackageB package, which is accessed by default. An attempt to create a ClassA1 class object is failed, because the ClassA1 class is declared in another package and there is no public access modifier for it.


2 Answers

The error you're getting makes sense. The Student class is located in MessManagement package not MessManagement.student package.

So either remove your import of Student in MessManagement or change the package name in Student to 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.

The solution was to do mvn clean.

After this, I didn't get that error anymore.

like image 111
smac89 Avatar answered Oct 14 '22 22:10

smac89


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 that or 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.

like image 21
Jens Schauder Avatar answered Oct 14 '22 22:10

Jens Schauder