cannot access the classes in the package - java

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

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.

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

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.

I deleted the out folder and the problem was solved

For me it happened after updating java to jdk_16.
it was solved after I changed the "misc.xml" file inside ".idea" folder.
changed languageLevel="JDK_15" to languageLevel="JDK_16".

In Intellij, try
Go to File
Invalidate Caches/Restart
You can choose only Invalidate and restart
See this answer

Well I guess I am literally 8 years late, but today I had this problem and I managed to solve it just re-writing the state of the class. Seems that if you comitted your project, pulling it back could make you have that problem, but writing again the "public" state of your class worked for me. I Hope it works for you guys. chrss.

Related

Packages in java not compile

Good morning everyone!
I did a project x but all my files were out of order so I decided to group them by folders.
foldera
---ClassA.java
---ClassB.java
folderb
---Class1.java
Main.java
The problem arises when I try to compile, since in the main it appears that the classes I made are not found
I thought this could be solved by putting in the classes
package src.foldera.ClassA;
And in the others the same
package src.foldera.ClassB;
And
package src.folderb.Class1;
So in all classes
And in the main put
import src.foldera.*;
import src.folderb.*;
But I keep getting the same error even though I put the packages
It should be noted that I did not create the folders in the code editor, rather I did it in the same Windows 10 File system
What is this about? Thanks!
Assuming a standard setup where the src folder is the root of your source hierarchy, the statements should be:
package foldera;
package folderb;
The imports should be similarly shortened to:
import foldera.*;
import folderb.*;
You have to define the package in which the class is in.
As an example in classA:
package src.foldera;
And in class1:
package src.folderb;
import src.foldera.ClassA;

Can't import Java class?

The screenshot is the error I am receiving. I am trying to use the class ConnectDB contained within the package CIS4362Connect1, in the class AdminManager in the package myconnectoracle. Why can't I import this class?
As comments above indicate, it is possible that your ConnectDB is not public, in order to use that close outside of the its package, it must be declared public
Please refer to: Access control documentation
EDITED: (Solution)
The problem is that packages are case sensitive, you have a different case in both packages declaration.
inside the ConnectDB class you maybe have cis in lower case.
As far as I can tell, the class you are looking for is not defined in the current project. Try referencing the project with the package you are looking for in the libraries, or just move the package/class.
I just got a very similar issue with one of my class being unexplicably unimportable. I tried building the project anyways, and it did build successfuly, despite errors showing everywhere.
Moving the package to another location within my ide, and then moving it back to where it was supposed to be located somehow solved the issue for me.

Jars with default package

I found this question -> Import custom libraries in Java
And #Andy Thomas-Cramer said that the classes in "stdlib.jar" from "An introduction to programming in Java" have no packages, so they are in the default package.
Isn't this a bad practice? If you have something with no package the IDEs' auto-completion is quite slower. And also this means that we could not use any of the classes, in that jar, from classes with packages different then the default?
Can someone please tell me how we could deal with this?
EDIT:
I have 2 jars and I put them in Referenced libraries, they both have a bunch of classes in default package. When I create class in different package then the default - lets say org.myquestion I can't access the classes from the jars anymore.
This is something that really bugs me... First I can't create my own package and use anything from the jars. Second my IDE's (I use eclipse) auto-complete goes terrible - I guess it searches to meany classes at once... What I want to do is to put somehow the jars in some namespace... and to be able to access them like org.someones.libs.SomeClass
It certainly is bad practice to use the default package. A package groups classes and provides them with access protection (protected, package private) and functions as a unique namespace.
You can always use classes from every package, them being default or not, you can always mix.
Download the jar source code, And built it to jar by yourself and added the package name whatever your like.That's will solve your problem.
Importing classes inside JAR files that are in the default package
I ran into the exactly same problem as you did. The problem is the jar file "stdln.jar" has no named package, say, only with default package.
You cannot import a class from a default package, basically, since the import operation needs the package name:
import packagename.*;
So there are only two way to fix this problem:
the easier one: Do not create a package in your src folder and use default package two! Every class in stdln.jar would be imported to your src automatically.
Like this:
enter image description here
try to create your own jar file with a named package and copy all the class file into your newly-created jar file.
Since the stdln.jar is only used for education, so which you are gonna choose does not really matter. In real development, we never use default named package since it's not really a good practice, always leading to some confusing stuff.
Hope this would help you!

Beginner Importing in Dr. Java

Trying to import a class I made in Dr. Java. I made a simple class called QuestionObject which has String questionString and an array for answers and then an int which corresponds to the correct answer and corresponding getters, setters, and constructors. Compiles fine. In my other class file, the one with my main method and such called Game, when I try import QuestionObject; I get a 'cannot resolve' error in the compiler. I saved the class in the same folder as Game.java. Doing everything in OS X not using command line.
Help please! There's probably a simple answer, I just can't find it!
Don't import the class if both classes are in the same directory and in the default package!
Try removing the import statement.
Try setting the package of both classes. And make sure that both classes are in the class path. I am not familiar with Dr Java but with the command line compiler javac *.java in the directory with the classes should pickup both classes.

The package collides with a type in java code

I have a question about eclipse version 3.6.1.
I had run my code well before, but now I open the eclipse and my code appear red line at the package.
The error message is The package com.test collides with a type.
How should I solve this problem?
My code is as the following:
package com.test;
public class test extends Activity{
.....
.....
}
Thank you for your help~:)
I've changed the class name and the file name to Test, but it always wrong at the package com.test;, I have no idea about that problem.
At this point, Eclipse could just be confused.
Do a "project > clean" on all projects in your workspace. If that fails try the various other tricks that are typically used to de-confuse Eclipse.
Even after i corrected the naming conflicts and changed the package declaration to match its location, Eclipse showed this error message.
I deleted my project from the Eclipse package explorer(not from the computer) and then imported the same project after restarting the Eclipse. Problem solved!
The easiest solution is to follow the Java naming convention whereby class names start with an uppercase letter; so Test rather than test.

Categories