# Thread: Class paths and Packages issue

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

2. ## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

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

4. ## Re: Class paths and Packages issue

Thank you pbrockway2

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

6. ## Re: Class paths and Packages issue

Where is the solve button!!!!!!!!!!!!!!!!!!!!!!

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

## Re: Class paths and Packages issue

8. Student
Join Date
Jul 2012
Location
United States
Posts
328
Rep Power
2

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

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

 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. Student
Join Date
Jul 2012
Location
United States
Posts
328
Rep Power
2

## Re: Class paths and Packages issue

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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

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

... but so are grammar mistakes
Last edited by pbrockway2; 07-12-2012 at 03:45 AM.

12. Student
Join Date
Jul 2012
Location
United States
Posts
328
Rep Power
2

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

## 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. Student
Join Date
Jul 2012
Location
United States
Posts
328
Rep Power
2

## Re: Class paths and Packages issue

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

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

## Re: Class paths and Packages issue

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