Can't import Java class? - java

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.

Related

Making a package default like java.lang in java

what I mean by making a package default is including the java package into the class mainstream just like java.lang
suppose I Have a package utility.* I want this package to be imported by default in any class I create.
Hence, whenever I'd create a class, I wouldn't have to write the import statement at all, as we don't have to in the case of java.lang. Is there a way to achieve this?
Only java.lang is auto-imported this way and there's no way to add other packages to this list (outside of building your own version of Java, which is definitely a bad idea).
However, most IDEs allow you to define "favorite" packages that will be searched first for suggestions when writing a class name that hasn't been imported there and adding your package to that list has a very similar effect, since the IDE will just add the import statement for you.

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

Java/eclipse: Why can't I run a prog with package by Run Configuration? Pic inside

Someone help me please? I don't know why can't I run a simple program by the run configuration. If I don't set a package in the beginning it works.
I attach a picture of my screen
The class bb is in the package aa2. Its name is thus aa2.bb, not bb.
If not specified the package name externally then a given class is said to be included in default package i.e this class exists directly under src folder.
If you remove the package statement in the class then you have to move this class to one folder level up, in order to run this class.
But it is discouraged and normally it is expected that each class should declare its own package statement.

confused with package and import statements including default package

I am confused with the terminology and its use in the eclipse IDE.
It is my understanding that you can use the import keyword to reference specific classes/java files within a group of class/java files called packages. You can also reference an entire group of classes/java files by using a wildcard modifier end the end of the import statement. So why do this when you can just make the package statement at the top of the java src file instead of having to do an import?
for instance:
folder structure: myapp>graphics>.class1, .class2
instead of doing this: import myapp.graphics.*
we can do this right?: package myapp
so whey even have the import keyword? can't you just use package myapp and reference the class with say class1 example = class1();
Second, I tried to remove the package com.example.myapp and just import the classes with import myapp.graphics.* but it it gave me an error stating that "" does not match the expected package com.example.myapp. one of the fixes for it was to move MainActivity.java to a default package. I chose that option and it moved it to a default package and then I was able to use the import myapp.graphcis.*; statement while leaving out the package myapp.graphics statement without any errors.
I am confused. Also, what is a deauflt package? I read somewhere that it is bad practice to use a default package. why?
thanks.
Packages - A location where class is located. It is used to build the fully qualified class name.
Import - Used for incorporating pre-existing classes, by using this you can avail the functionality from them.
Default package - Location directly under src folder, for example: src\NoPackage.java
For example, if NoPackage.java contains import com.assertcheck.AssertTesting; then AssertTesting is references under NoPackage class.
However in AssetTesting class you can't import/use NoPackage.
According to oracle:
A package is a namespace that organizes a set of related classes and
interfaces. Conceptually you can think of packages as being similar to
different folders on your computer. You might keep HTML pages in one
folder, images in another, and scripts or applications in yet another.
Because software written in the Java programming language can be
composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.
So, you can think of packages as organizers, which have nothing to do with imports.
The default package is no package at all. Eclipse warns you about this when you leave the field blank. Using it is bad practice because
Using the default package may create namespace collisions. Imagine you're creating a library which contains a MyClass class. Someone uses
your library in his project and also has a MyClass class in his
default package. What should the compiler do? Package in Java is
actually a namespace which fully identifies your project. So it's
important to not use the default package in the real world projects.
Why shouldn't we use the (default)src package?

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!

Categories