Link to home
Start Free TrialLog in
Avatar of howesd
howesd

asked on

A Default Package Question ( Urgent )

I think I may have made a big mistake ....

I have a web application where the majority of my classes are not explicitly defined as being in a package so, if I understand things correctly, they live in the "default package". I have a smaller number of clases which I have split in to packages within the "default package". I'm now finding that I need to refer to some statically declared objects within the default package from within these sub-packages, and I'm also finding that I don't know how to do it! I guess I need to import the default package in to the relevant classes in the sub-packages but I don't know how to refer to it. Is there a special syntax to refer to the default package or am I faced with having to put all my classes in to a "real" package?

Dave
Avatar of Igor Bazarny
Igor Bazarny
Flag of Switzerland image

Hi,

just import individual classes, not whole default package. Like this:

public class A{
   public static String somevalue;
}

package b;

import A;

public class B{
    public static void main(String[] args){
        System.out.println("A.somevalue: "+A.somevalue);
    }
}

But you would better put your classes to packages anyway.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
ASKER CERTIFIED SOLUTION
Avatar of yoren
yoren

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of howesd
howesd

ASKER

Yoren
I'm in the process of doing that and I'm having all sorts of problems.

my directory structure =  /home/howesda/java/dev/code/OrchidRouter
   /home/howesda/java/dev/code/OrchidRouter/BslException
   /home/howesda/java/dev/code/OrchidRouter/globals
   /home/howesda/java/dev/code/OrchidRouter/BslToolKit

I've been through all the java files on the OrchidRouter directory and added the following statement:
package OrchidRouter;

I've also been through all the files in the BslException directory and added
package OrchidRouter.BslException;
( I know I need to do the other directories - I just wanted to see if I was going in the right direction )

In my OrchidRouter.java file I have
import OrchidRouter.BslException.*;

When I come to compile it I get
OrchidRouter/OrchidRouter.java:26: cannot resolve symbol
symbol  : class BslException  
location: class OrchidRouter.OrchidRouter
import OrchidRouter.BslException.*;


I'm compiling it from the /home/howesda/java/dev directory and calling
javac OrchidRouter/OrchidRouter.java

What I am I doing wrong? I've never got to grips with the way packages work!

Dave
You've almost got it. You need to be in the /home/howesda/java/dev/code directory when compiling, so that any directories you see are packages. The command to compile your OrchidRouter package will be "javac OrchidRouter/*.java"
Avatar of howesd

ASKER

I'm still not getting anywere with this - its the same erros as before ( I mistyped the directory names on my earlier message the /code bit is not supposed to be there )

So, I alter my class path to have /home/howesda/java/dev on it and I can call

javac OrchidRouter/BslException/*.java
and it compiles clean

I then call OrchidRouter/*.java and I get a load of errors saying that the import of OrchidRouter.BslException.* is not valid

the full message is
OrchidRouter/AdvisoryClientRetriever.java:14: cannot resolve symbol
symbol  : class BslException  
location: class OrchidRouter.OrchidRouter
import OrchidRouter.BslException.*;

I can't see what I'm doing wrong ....
Avatar of Mick Barry
> I alter my class path to have /home/howesda/java/dev

That's not the right directory to add to you classpath, add the following instead:

/home/howesda/java/dev/code

When the classloader is looking for a class it appends the package name to the directories specified in the class path. So if it's looking for OrdhichRouter.BslException.MyClass, then it will append OrdhichRouter\BslException\ to the directory in the classpath and look for the classfile MyClass.class in that directory.

Let me know if you have any questions.
howesd,

It's probably easiest to just add "." to your classpath and be sure (with ls) that you see OrchidRouter as one of the directories within your current directory.
Adding "." limits you to have to be in the /home/howesda/java/dev/code directory to compile or run your classes, and that you have to specify the path to your files you want to compile.
Add /home/howesda/java/dev/code instead to your classpath makes things a lot easier because you can compile your files from the actual directory containing the source, and you can run your classes from any directory.
Avatar of howesd

ASKER

Thanks for your responses - I've actually got the package to build know ( after about 7 hours of messing about ). I tried so many different things that it's difficult to tell which one actually made the difference, but I believe that my major problem was that my package had the same name ( OrchidRouter ) as one of the classes within it. As soon as I altered the name of my package, it built OK.

Is that expected behaviour?

Dave
No, it's legal to have the same package and class name.
Avatar of howesd

ASKER

Thanks for your help on this - I've finally got the build resolved. I've still not been able to have a package with the same name as a class which is within it, but I haven't got the time to investigate it at the moment.

Dave