File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Accessing default package classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing default package classes" Watch "Accessing default package classes" New topic
Author

Accessing default package classes

Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
OK, here's a problem that bugs me from time to time.
Say I have written (or bought in) a class in the default package (ie. no "package" statement). Let's call this "MyUtil".
Now I also have another class which _is_ in a package. Let's call this "frank.MyClient".
I can access frank.MyClient from MyUtil in a variety of ways - use "import frank.*", use "import frank.MyClient", use the fully-qualified name etc.
But how do I access MyUtil _from_ frank.MyClient. Every tim I try this the compiler seems to assume that a class name without a package must be in the same package as the caller (in this case "frank"). But it's not!
So the problem is: how do I tell Java to access a class in the default package from a class in a package?
A puzzled Frank.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

Frank,
I'm trying to wrap my brain around your question. Could you give a tiny code fragment example?

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Brent Worden
Ranch Hand

Joined: Nov 26, 2001
Posts: 50
Frank,
Forgive me for asking this, as it may be insulting. But, it the directory of the default package in your CLASSPATH?
Brent Worden
http://www.Brent.Worden.org/

Brent Worden
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
To Brent: yes;
To Paul: Here's a simple example. To try it out, make a new directory somewhere and make sure "." is in your CLASSPATH. Put MyUtil.java in this directory. Make a subdirectory under this directory named "frank", and put MyClient.java into it.
cd to your directory and type:
<code>javac MyUtil.java frank/MyClient.java</code>
<pre>
( *** contents of file MyUtil.java *** )
public class MyUtil
{
public void doSomething(String text)
{
System.out.println("MyUtil: " + text);
}
}
( *** contents of file frank/MyClient.java *** )
package frank;
public class MyClient
{
public static void main (String argv[])
{
MyUtil ut = new MyUtil();
ut.doSomething(argv[0]);
}
}
</pre>
[This message has been edited by Frank Carver (edited February 10, 1999).]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

Isn't this the backwards way of using packages?
If you are going to traverse directories, it seems that the stuff you are trying to use has to be in a package.
What you are trying to do just seems wrong to me although I can't quote a book or anything that says it is wrong.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Oh, I agree it's not recommended, but it's the fact that it _should_ be possible, but doesn't seem to be documented at all that worries me. I'll keep searching, and if I find a definitive answer I'll post it here.
Frank.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Just found this thread, and realized that I did find a definitive answer, and some while ago, too
The answer is to declare any classes you need from the default package in individual import statements:

I Hope that helps someone.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Doesn't everyone import every class explicitly ;-)


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Douglas Kent
Ranch Hand

Joined: Oct 06, 2000
Posts: 171
Under what version of the JDK did that solution work? Under 1.4.2, I get "'.' expected" and the end of the explicit import of the default package class, ie - import JniWrapper; <==(error shows up on semi-colon)
thanks...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

You've ressurected a four-year-old thread. Since that time, Sun's Java compilers have begun rejecting the unqualified-class-name import as a syntax error. The correct and complete answer to Frank's original question is now "you can't."


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing default package classes