Solved

A Default Package Question ( Urgent )

Posted on 2002-06-14
11
169 Views
Last Modified: 2011-10-03
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
Comment
Question by:howesd
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7078883
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
 
LVL 7

Accepted Solution

by:
yoren earned 300 total points
ID: 7078988
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
 
LVL 1

Author Comment

by:howesd
ID: 7079003
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
 
LVL 7

Expert Comment

by:yoren
ID: 7079097
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
 
LVL 1

Author Comment

by:howesd
ID: 7079131
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
ID: 7079620
> 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
 
LVL 7

Expert Comment

by:yoren
ID: 7079750
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
 
LVL 92

Expert Comment

by:objects
ID: 7079771
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
 
LVL 1

Author Comment

by:howesd
ID: 7080489
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
 
LVL 7

Expert Comment

by:yoren
ID: 7080523
No, it's legal to have the same package and class name.
0
 
LVL 1

Author Comment

by:howesd
ID: 7083251
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now