Results 1 to 15 of 15
Like Tree3Likes
  • 1 Post By pbrockway2
  • 1 Post By pbrockway2
  • 1 Post By pbrockway2

Thread: Class paths and Packages issue

  1. #1
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Question Class paths and Packages issue

    I have a few packages and files that I am using.
    To quickly explain my issue, I have 3 class files.
    1. First class file is in <parent directory>/
    2. Second class file is in <parent directory>/com
    3. Third class file is in <parent directory>/com/fo



    I want the first and second file to be able to access the information and functions of the third file. How can I get this to work?

    At the moment, the package names for the second class file is
    Java Code:
    package com;
    and the third class file has
    Java Code:
    package foo;
    with packages like this, the second class file can access the third but the first cannot. I tried using 'package com.foo' for the third file, but that causes the second file to lose access but the first file can.

    How can I format the java 'package' line to give all of my files access to the third file. If this is not possible, please tell me.

    A visual example of what I am currently doing is here:
    File 1: /
    Java Code:
    import com.*;
    
    public class my_first_class
    {
    public static void main(String args[]) {
    my_second_class c2 = new my_second_class(); //this works at the moment
    }
    }
    File 2 /com
    Java Code:
    package com;
    
    public class my_second_class
    {
    my_third_class c3 = new my_third_class(); //this works at the moment
    }
    File 3 /com/foo
    Java Code:
    package foo;
    
    public class my_third_class
    {
    //Some code that is needed in both the first and second class.
    }
    Last edited by pbrockway2; 07-12-2012 at 03:37 AM. Reason: marked as solved
    My API:
    Java Code:
    cat > a.out || cat > main.class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Class paths and Packages issue

    First off, *every* class should be in a package. You should not be using the default package anywhere. Next of all, if you need access to a class then you'll want to import that class using its full name, package included.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class paths and Packages issue

    To keep things straight (and simple) make your package structure analogous to the file structure on disk.

    So... Put the first class into a package (creating a directory for it - a sibling of com - if necessary).

    Make the third class com.foo.ThirdClass not foo.ThirdClass. Ie change the package line in the thrid class to "package com.foo".

    With these changes all three classes will be found providing the directory (folder) containing com is part of the classpath.

    -----

    Classes should begin with a CapitalLetter. Although often abbreviated in code, a class includes the package part of its name. Think of them that way and recognise that foo.bar.Baz - to give it its full name - will correspond to the .class file <somewhereOnClasspath>\foo\bar\Baz.class.
    kammce likes this.

  4. #4
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Class paths and Packages issue

    Thank you pbrockway2

    With your advise, I was able to work everything out. Consider this thread solved!
    My API:
    Java Code:
    cat > a.out || cat > main.class

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class paths and Packages issue

    Great. I'm glad you've got it sorted out. There should be a link on this page (somewhere!) letting you mark the thread as closed.

  6. #6
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Class paths and Packages issue

    Where is the solve button!!!!!!!!!!!!!!!!!!!!!!
    My API:
    Java Code:
    cat > a.out || cat > main.class

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class paths and Packages issue


  8. #8
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Class paths and Packages issue

    Hey pbrockway2!

    I was actually discussing this issue with another moderator earlier today. When I go to my thread and click on 'thread tools', the first 4 options show up but not the 5th 'Mark this thread as solved' option. I have a standard account with standard privileges as far as I know. It seems that there is not a way for a non-moderator to close a thread.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class paths and Packages issue

    Umm... It could be that the "mark as solved" functionality is broken.

    Jos' advice in that thread was "edit your original post; go to the 'advanced mode' and set the prefix of the title to '[solved]'" which might work in this case as well.

    [Edit] Yes, that seems to be the way to do it. (Eg Just testing...) Ignore the faq and edit the original post (in advanced mode) to add the [Solved] prefix.
    Last edited by pbrockway2; 07-12-2012 at 02:01 AM.

  10. #10
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Class paths and Packages issue

    Yes, I actually had an in-depth conversation with Jos about this

    What I've found out is that the option to edit a post expires for us members after a certain amount of time (I think it might be 24 hours.) Here is a thread that I created yesterday morning: Does the Compiler Remember Classes Throughout a Single Compliation?

    My first two posts have the 'reply', 'reply with quote', and 'multi-quote this message' buttons, but there is no 'edit post' button. However, my subsequent 4 posts that I made today do have the 'edit post' button.

    So it seems that if a post takes longer than a day to be answered, it can't be solved by a non-moderator.

    Edit:I realize that the thread I linked to is already solved, that's because a mod did it for me.
    Last edited by awinston; 07-12-2012 at 02:11 AM. Reason: format

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class paths and Packages issue

    I'll bring it to the attention of the other moderators and link to here. The functionality used to be accessible via the "thread tools" (as described in the faq) rather than editing a post and that's a better place for it: edits of posts long after the fact is^H^H are annoying.

    [edit]... but so are grammar mistakes
    Last edited by pbrockway2; 07-12-2012 at 03:45 AM.
    awinston likes this.

  12. #12
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Class paths and Packages issue

    Just curious...do moderators have the power to play with the code of this website? Or do you guys have to contact somebody else?

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class paths and Packages issue

    No. We have the power to take out the spam trash, the glory of tidying up posts put in the wrong forum and now, it seems, the glamour of marking solved threads as solved. Things of that sort. I guess we can also ban users, but that's seldom done here (I never have for instance).

    Any changes to the site are made by the "forum's owner". Who seems to be referred to always as the "forum's owner": an anonymity that strikes me as having a sort of James Bond feel to it.

    There's a "Suggestions and Feedback" forum (near the bottom of the forum list) where you can ... make suggestions and give feedback.

  14. #14
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Class paths and Packages issue

    Are you...suggesting ...that I make a post about solving threads in that forum?

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class paths and Packages issue

    No, not really. The matter's been raised so we'll see what comes of it.
    awinston likes this.

Similar Threads

  1. Replies: 1
    Last Post: 04-03-2012, 09:29 AM
  2. Timer Class Issue
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 10:31 PM
  3. Class cast issue
    By AedonetLIRA in forum New To Java
    Replies: 5
    Last Post: 11-19-2010, 04:34 PM
  4. Calendar class issue
    By OlegKo in forum New To Java
    Replies: 3
    Last Post: 10-24-2010, 06:36 PM
  5. Replies: 3
    Last Post: 02-12-2008, 11:03 AM

Tags for this Thread

Posting Permissions

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