Results 1 to 7 of 7
  1. #1
    JavaDreams is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Unhappy Package not working!

    //File:supperClassA.java
    package A;
    public class supperClassA
    {
    }
    ------------------------------
    //File:subClassB.java
    import A.*;
    public class subclassB
    {
    }
    -------------------------------
    Class supperClassA is compiling but when i am compiling subClassB there is an error.
    subClassB.java:1: package A does not exist
    import A.*;
    ^
    1 error

    Both the files are in the same directory.
    Now whats going wrong?
    Please help me :(

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I just built it and ran it and got it working by compiling super in the a class directory, and then moving sub down a directory and compiling it. I can't quite explain how it works, as I am a bit rusty too, usually when I use packages I just try different things to get something to work. Someone here may have a better explanation for you.

    I believe I figured out why it gives an error if you leave it in the package and import somethng from the package. I believe(and I am sure someone will correct me if I am incorrect) that the item needs to be either out of the package or in the same package as super.
    Last edited by sunde887; 02-10-2011 at 10:01 PM.

  3. #3
    JavaDreams is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    I just built it and ran it and got it working by compiling super in the a class directory, and then moving sub down a directory and compiling it. I can't quite explain how it works, as I am a bit rusty too, usually when I use packages I just try different things to get something to work. Someone here may have a better explanation for you.
    Sir I did not set the directory name A previously, but i changed it now and still it's not recognizing it.
    One more Question: When i clicked on quote button on your message i saw a completely different message...that you built the code.....
    // Oh i got it you eddited you earlier Message.. Ok sir
    Thanks for your help!! But i am still in this problem, and as you said i am experimenting with it.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,094
    Rep Power
    20

    Default

    You'll hve to show us your directory structure and where (and with what command) you are compiling this stuff.

  5. #5
    JavaDreams is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    You'll hve to show us your directory structure and where (and with what command) you are compiling this stuff.
    Exactly sir, At first both the package folders were at same level.
    C:\java\A , C:\java\B

    This did'nt worked, but then i put the called package in side the calling package, then it worked!!

    C:\java\B\A

    Is this a compulsory thing ???

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    If the package declaration in B is
    Java Code:
    import a.b.*;
    //or
    import a.b.B;
    then the 'b' package must be a folder inside the 'a' folder. Packages are nested in this way. However, if you declare A and B to both be in package 'a', then simply having them both in the 'a' folder should be enough. Could you verify this?

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,094
    Rep Power
    20

    Default

    In other words, the directory structure has to match your package structure.
    Java Code:
    package mypackage.anotherpackage;
    Would be represented on disk as:
    c:\...\src\mypackage\anotherpackage

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  2. run package inside anthor package
    By AhmedAdel in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2010, 11:52 AM
  3. What does package do?
    By Mattedatten in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 12:48 AM
  4. Replies: 7
    Last Post: 11-09-2009, 07:51 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •