Improve company productivity with a Business Account.Sign Up

x
?
Solved

A Default Package Question ( Urgent )

Posted on 2002-06-14
11
Medium Priority
?
183 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 1200 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

579 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