Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

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
0
howesd
Asked:
howesd
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Igor BazarnyCommented:
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
0
 
yorenCommented:
Dave,

You've just hit the reason you should avoid the default package when developing "permanent" applications. You cannot import the default package. Also, the default package can't have sub-packages. When you declare a package name, you're putting them in a *different* top-level package.

You will need to move all your classes into a package. The convention for package names is "com.yoursite.somecategory". Just move all the java files into the com/yoursite/somecategory directory and add the package declaration at the top of your files.

0
 
howesdAuthor Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
yorenCommented:
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"
0
 
howesdAuthor Commented:
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 ....
0
 
objectsCommented:
> 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.
0
 
yorenCommented:
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.
0
 
objectsCommented:
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.
0
 
howesdAuthor Commented:
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
0
 
yorenCommented:
No, it's legal to have the same package and class name.
0
 
howesdAuthor Commented:
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now