Solved

A Default Package Question ( Urgent )

Posted on 2002-06-14
11
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 68
Desingning Refactoring existing code 2 46
eclipse console opening separately 2 49
batch vs regular insert in spring DAO 2 51
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses

739 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