Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing default package classes

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paul wheaton
Trailboss
Pie
Posts: 21326
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,
I'm trying to wrap my brain around your question. Could you give a tiny code fragment example?
 
Brent Worden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21326
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't everyone import every class explicitly ;-)
 
Douglas Kent
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic