• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package declaration !

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to put my classes in a package e.g
package pkg1;
class disp {
int a;
..
...
}
package pkg1;
class disp2 {
disp d1 =new disp();
}
its giving error in class disp2 that class disp not found.Whats wrong i'm doing or i'm missing something?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i copied ur code and compiled it exactly as it is i did not get any errors on both classes. if u are still getting an error i think u should add an import statement to the the second class desp2 and leave the package statement like this : package pkg1;import pkg1.disp; i tried it just now and it compiled hope it works for u
------------------
khella smith
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't a situation where you need to use an import statement.
Your package/class struture must match a corresponding directory/file structure. When you compile a .java file, the compiler must be able to find any referenced packages or classes.
After compiling disp which you have defined to be in the package pkg1, the class file must be in a directory named pkg1 in order to use it and the compiler must be able to find the directory named pkg1.
Perhaps it is somewhat confusing at first, but the compiler does not recognize the working directory as being a referenced named package (unless it is specifically mentioned in the classpath setting). So, if you are working in the pkg1 directory, where you've already compiled disp, and you try to compile your other source code file (that is also in this directory) from this directory (javac disp2.java), without specifying the parent directory of pkg1 in the classpath, then it won't compile.
You'll need to compile from the parent directory (taking advantage of the automatic inclusion of the current directory in the classpath setting) or include the package pkg1 in your classpath by specifying it's parent directory (parent package). A quick and dirty way to include any parent directory in your classpath is to compile with javac -classpath "..;%classpath%" SomeClass.java .
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic