- Demonstration of
Package Does Not Exist
in Java - Resolve
Package Does Not Exist Error
in Java
Today, we will reproduce the package does not exist
error in Java to understand the error and its reasons. Further, we will also learn about its solution with the help of code examples.
Demonstration of Package Does Not Exist
in Java
Example Code (Students.java
file):
package name;
public class Students implements Comparable<Students>{
private String studentFirstName;
private String studentLastName;
public Students(String studentFirstName, String studentLastName){
this.studentFirstName = studentFirstName;
this.studentLastName = studentLastName;
}
public String getStudentFirstName() {
return studentFirstName;
}
public void setStudentFirstName(String studentFirstName) {
this.studentFirstName = studentFirstName;
}
public String getStudentLastName() {
return studentLastName;
}
public void setStudentLastName(String studentLastName) {
this.studentLastName = studentLastName;
}
/**
*
* @param other
* @return
*/
@Override
public int compareTo(Students other) {
int compareResults = this.studentLastName
.compareTo(other.studentLastName);
if(compareResults == 0){
if(this.studentFirstName.chars().count() ==
other.studentFirstName.chars().count()) {
compareResults = 0;
return compareResults;
}
else if(this.studentFirstName.chars().count() >
other.studentFirstName.chars().count()){
compareResults = 1;
return compareResults;
}
else{
compareResults = -1;
return compareResults;
}
}
else{
return compareResults;
}
}
}
Example Code (StudentMain.java
file):
import names.Students;
public class StudentMain {
public static void main(String[] args) {
Students student1 = new Students ("Ali", "Ashfaq");
Students student2 = new Students ("Ali", "Ashfaq");
System.out.println("Comparison 1: " + student1.compareTo(student2));
Students student3 = new Students ("Ali", "Ashfaq");
Students student4 = new Students ("Zoya", "Ashfaq");
System.out.println("Comparison 2: " + student3.compareTo(student4));
Students student5 = new Students ("Mehr-un-nissa", "Ashfaq");
Students student6 = new Students ("Hina", "Ashfaq");
System.out.println("Comparison 3: " + student5.compareTo(student6));
}
}
We have a directory Desktop/java/stuff/com/name
where all our .java
files are located except those containing the main()
method. For the above code example, we have Students.java
in the Desktop/java/stuff/com/name
directory, while StudentMain.java
with the main()
method is in the Desktop/java/stuff/com
directory.
It is also important to note that we have set Desktop/java/stuff
to our CLASSPATH
.
Let’s understand the code to figure out the error and its causes.
The above code compares the last names of the Students
and stores the result in the compareResults
variable. This result is further used to compare their first names. How?
If the last name matches, the result would be true
means 0
. So, it jumps to the if
condition and assesses if the number of characters in their first names matches.
The result of comparing first names is based on the following conditions:
- If
this.count
is equal to theother.count
, the result will be0
. - If
this.count
is greater than theother.count
, the result will be1
. - If
this.count
is less than theother.count
, the result will be-1
.
But, when we try to compile the program, it gives us the following error.
C:UsersMEHVISH ASHIQDesktopjavastuffcom>javac StudentMain.java
StudentMain.java:1: error: package names do not exist
import names.Students;
What does it mean, and why are we facing this problem? This error means the package we are trying to import does not exist.
There can be different reasons for that, and all of them are listed below:
-
We have imported the incorrect package, or we may have some typos while importing the package.
-
Recheck if all of the files are in the correct sub-directories.
-
If we have set our
CLASSPATH
toDesktop/java/stuff
, then the files defined withpackage name;
must reside in theDesktop/java/stuff/com/name
directory. You may check this to learn how to setCLASSPATH
. -
Make sure all the Java source files are in the correct sub-directory. We must also ensure that the Java source files are compiled in the
Desktop/java/stuff/com/name
directory.Why? The
.class
files must be on theCLASSPATH
. Remember, the.java
files are not required to be on theCLASSPATH
butSOURCEPATH
, while.class
files are generated when we compile the file using thejavac
command. -
We also get this error if we don’t use the built-in package properly. See the following code:
Example Code:
public class Test{
public static void main(String[] args){
/*
Solution: This line must be as follows:
System.out.println("Hi!");
*/
system.out.println("Hi!");
}
}
Error Description:
Test.java:3: error: package system does not exist
system.out.println("Hi!");
^
1 error
Resolve Package Does Not Exist Error
in Java
Example Code (Students.java
file):
package name;
public class Students implements Comparable<Students>{
private String studentFirstName;
private String studentLastName;
public Students(String studentFirstName, String studentLastName){
this.studentFirstName = studentFirstName;
this.studentLastName = studentLastName;
}
public String getStudentFirstName() {
return studentFirstName;
}
public void setStudentFirstName(String studentFirstName) {
this.studentFirstName = studentFirstName;
}
public String getStudentLastName() {
return studentLastName;
}
public void setStudentLastName(String studentLastName) {
this.studentLastName = studentLastName;
}
/**
*
* @param other
* @return
*/
@Override
public int compareTo(Students other) {
int compareResults = this.studentLastName
.compareTo(other.studentLastName);
if(compareResults == 0){
if(this.studentFirstName.chars().count() ==
other.studentFirstName.chars().count()) {
compareResults = 0;
return compareResults;
}
else if(this.studentFirstName.chars().count() >
other.studentFirstName.chars().count()){
compareResults = 1;
return compareResults;
}
else{
compareResults = -1;
return compareResults;
}
}
else{
return compareResults;
}
}
}
Example Code (StudentMain.java
file):
import name.Students;
public class StudentMain {
public static void main(String[] args) {
Students student1 = new Students ("Ali", "Ashfaq");
Students student2 = new Students ("Ali", "Ashfaq");
System.out.println("Comparison 1: " + student1.compareTo(student2));
Students student3 = new Students ("Ali", "Ashfaq");
Students student4 = new Students ("Zoya", "Ashfaq");
System.out.println("Comparison 2: " + student3.compareTo(student4));
Students student5 = new Students ("Mehr-un-nissa", "Ashfaq");
Students student6 = new Students ("Hina", "Ashfaq");
System.out.println("Comparison 3: " + student5.compareTo(student6));
}
}
We got the package does not exist
error because of importing the wrong package in the StudentMain.java
file. We were importing as import names.Students;
, while it must be import name.Students;
.
You may see all the commands below, including how we set the CLASSPATH
.
Output:
C:UsersMEHVISH ASHIQ>cd Desktop/java/stuff
C:UsersMEHVISH ASHIQDesktopjavastuff>set classpath=.;
C:UsersMEHVISH ASHIQDesktopjavastuff>cd com/name
C:UsersMEHVISH ASHIQDesktopjavastuffcomname>javac Students.java
C:UsersMEHVISH ASHIQDesktopjavastuffcomname>cd..
C:UsersMEHVISH ASHIQDesktopjavastuffcom>javac StudentMain.java
C:UsersMEHVISH ASHIQDesktopjavastuffcom>java StudentMain
Comparison 1: 0
Comparison 2: -1
Comparison 3: 1
0 / 0 / 0 Регистрация: 06.12.2014 Сообщений: 7 |
|
1 |
|
06.12.2014, 12:21. Показов 21837. Ответов 6
Скачал Java. Изменил всё как над в path, Написал первый хелло ворлд, сохранил в формате .java, но всё равно ошибка, не понимаю откуда она.
__________________
0 |
115 / 100 / 52 Регистрация: 14.09.2011 Сообщений: 694 Записей в блоге: 1 |
|
06.12.2014, 12:27 |
2 |
system.out.p….. А попробуй System.out…..
0 |
0 / 0 / 0 Регистрация: 06.12.2014 Сообщений: 7 |
|
06.12.2014, 12:39 [ТС] |
3 |
Можно более четче
0 |
115 / 100 / 52 Регистрация: 14.09.2011 Сообщений: 694 Записей в блоге: 1 |
|
06.12.2014, 13:05 |
4 |
Заглавная буква у Класса System ,а не system.
0 |
0 / 0 / 0 Регистрация: 06.12.2014 Сообщений: 7 |
|
06.12.2014, 14:54 [ТС] |
5 |
Каким образом это исправить ?
0 |
115 / 100 / 52 Регистрация: 14.09.2011 Сообщений: 694 Записей в блоге: 1 |
|
06.12.2014, 15:27 |
6 |
В тексте программы , а текст программы находится в том файле, который ты запускаешь.
0 |
0 / 0 / 0 Регистрация: 28.11.2018 Сообщений: 4 |
|
13.02.2020, 00:33 |
7 |
0 |
Hi, I know what your all thinking, but i *have* spelt System correctly in my code!
The background is that I’ve always relied on Eclipse, Eclipse ME and J2ME-Polish, but we have recently had to dump J2ME Polish and switch to using Antenna.
Since the switch i’ve been plagued with problems, which i have managed to overcome, but this one has me stumped.
Basically I get an error whenever I use the «System» object.(‘System.out.println’ or ‘System.currentTimeMillis’ for example)
It always worked before when but now it is not, and I cannot find any info about it on the net.
When trying to compile my code for a different device, I get the following Error:
«Unable to locate package java.lang in classpath or bootclasspath»
and I think it is this which is why it cannot find the System package. As I said before I’ve relied on things to do all the setting up for me before, so now I don’t know the basics!! Could somebody tell me how to set this up to use Ant and Antenna or at least give me some hints?
Any help at all would be greatly appreciated!
EDIT: I’ve also just noticed that I get another error:
«cannot resolve symbol IOException»
I assume there is something fundamentaly wrong with the way I’ve set things up (env variables or whatever), any ideas?
[Edited by — Multiverse on July 7, 2006 11:29:51 AM]
Looks like the bootclasspath property in Antenna’s WtkBuild task is not pointing to the MIDP library. Which usually means you either have set the wtk.midpapi property incorrectly, or you haven’t set it and wtk.home either points to a wrong directory or a non-UEI compliant SDK. Your best bet is pointing wtk.midpapi directly to the MIDP library (usually classes.zip or midpapi.zip).
Some links you might find useful:
A working Antenna build file, and the related thread.
If you still have problems you might want to post the build file you’re using.
shmoove
Hi Shmoove,
Thanks for the hints, but my buildfile is already based on that example and I have already read that thread!
I’ve echo’d my path for the ${wtk.midpapi} in the build.xml and it points to the correct place:
c:WTK22libmidpapi10.jar
it is definitely pointing to midpapi.jar, but is that the right thing to point at?
I’ve also echo’d my ${wtk.home} and got this output:
c:WTK22
which is exactly where the WTK is on my drive.
My device.properties file (nokia7210.properties) looks like this:
<!-- Nokia 7210 Phone -->deploy.directory=/builds/nokia7210/distdevice.name=nokia7210device.midp=midp1mmapi.sound=noSoundmmapi.music=noMusicwtk.cldc.version=1.0wtk.midp.version=1.0wtk.midpapi=${wtk.home}/lib/midpapi10.jarwtk.mmapi.enabled=falsewtk.bluetooth.enabled=falsewtk.java3d.enabled=falsewtk.device=Nokia_S40_DP20_SDK_1_1wtk.heapsize=1mresources.path=res/nokia7210
and my local.properties:
<!-- local properties -->wtk_home =C:/WTK22wtk_proguard_home=C:/proguard3.6/libant_home=C:/ant_1_6_5/binnokia_home=C:/nokiaantenna_lib=tools/bin/antenna-bin-0.9.13.jarbasedir=c:/mobile/midpjava_home=c:/j2sdk1.4.2_09
[Edited by — Multiverse on July 6, 2006 4:42:10 AM]
The properties look OK.
I think I know what your problem is.
The old WTK only had one file that contained all the VM classes (it was called midpapi.zip). In the newer version (post 2.1 I think), they separated the bootstrap classes into separate files: cldcapi10.jar, cldcapi11.jar, midpapi10.jar, midpapi20.jar, mmapi.jar, etc.
So now you have to include more than one file in the bootclasspath property, including all the libraries you’re building for (ie, cldcapi10.jar, midpapi20.jar, and mmapi.jar if you’re targatting a CLDC 1.0/MIDP 2.0 phone). So the wtk.midpapi file should reflect that.
shmoove
Brilliant!!
Thanks Shmoove, that worked like a charm! But I’d like to check a couple of things just to make what I’ve done won’t cause me problems in the future. It works now, but maybe I havn’t done it *quite* right.
Basically what I did was extract the midpapi10.jar, and the cldcapi10.jar, and then zipped the extracted folders into a single .zip file.
I named the zip file «midp1_cldc1.zip» and I was planning on creating similar ones for different configurations. For example «midp1_cldc1_1.zip» (containing midpapi10.jar and cldcapi20.jar)
would i be better off just creating ‘one zip file to rule them all’ containing all the api’s I’m ever going to need? (and if i need a new one, i’ll just add it to the zip) Which is similar to what was done in WTK < 2.1 from what i can gather.
Thanks again, your a lifesaver!
What you did should be alright. I don’t expect any more problems.
A MIDP 2.0/CLDC 1.0 with all the extras (MMAPI, M3G, WMA, etc.) would be the the package to rule them all. Since there’s backward compatibility you can compile any profile/configuration with that. The down side is that any MIDP version errors won’t be caught during compilation.
You could also just use a colon separated list of the original libraries in wtk.midpapi:
wtk.midpapi=${wtk.home}/lib/midpapi10.jar:${wtk.home}/lib/cldcapi10.jar
shmoove
well in that case I think i’m going to use a colon seperated list for the wtk.midpapi property. I had read somewhere on the net ( here )that antenna wouldn’t be able to interpret it and their advice was to put the api’s you needed in a custom zip file. which is what i did, but shmoove to the rescue once again.
Lots of thanks once again.