Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Tools / December 2003

Tip: Looking for answers? Try searching our database.

can't import class in default package???

Thread view: 
Kay - 11 Dec 2003 06:58 GMT
Hi:

I have a java fiile that resides at the default package under src folder. In
the same project, when I want to import that class, I just need to type

import ClassName

right? However, eclipse cannot resolve it. But I can see the class in the
automatic code completion pop up list if I just type the first 1 or 2 letter
of the ClassName.

So what should I do to import such class?

thanks,
-Kay
Jon Skeet - 11 Dec 2003 11:11 GMT
> I have a java fiile that resides at the default package under src folder. In
> the same project, when I want to import that class, I just need to type
[quoted text clipped - 6 lines]
>
> So what should I do to import such class?

You should put the class into a package, instead. As of JDK1.4 the
above won't work with javac either.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Michael Amling - 11 Dec 2003 14:40 GMT
>>I have a java fiile that resides at the default package under src folder. In
>>the same project, when I want to import that class, I just need to type
[quoted text clipped - 9 lines]
> You should put the class into a package, instead. As of JDK1.4 the
> above won't work with javac either.

  I noticed that. But I don't know who thought it would be a good idea.

--Mike Amling
Dale King - 11 Dec 2003 17:48 GMT
> >>I have a java fiile that resides at the default package under src folder. In
> >>the same project, when I want to import that class, I just need to type
[quoted text clipped - 11 lines]
>
>    I noticed that. But I don't know who thought it would be a good idea.

The creators of Java. Quoting from the JLS:

"For small programs and casual development, a package can be unnamed or have
a simple name, but if code is to be widely distributed, unique package names
should be chosen. This can prevent the conflicts that would otherwise occur
if two development groups happened to pick the same package name and these
packages were later to be used in a single program."

"Unnamed packages are provided by the Java platform principally for
convenience when developing small or temporary applications or when just
beginning development."

The problem becomes complicated when you start thinking of multiple
hierarchies in the class path. For example, if I have a program in multiple
jar files and a class uses a class in a class in an unnamed package.Where
would it get it from? From the root of the jar file that class is in or from
the root of the jar file of the class from which the program started?
Conceptually, you could have multiple unnamed packages.
--
 Dale King
Kay - 11 Dec 2003 19:23 GMT
The only reason I put it at the default package is that it's a JNIed Class
by someone else. If I put it to my own package, I'll have to reJNI. And it's
a pain because I am not sure I can get it compiled successfully.

-Kay
Michael Amling - 12 Dec 2003 02:53 GMT
> The only reason I put it at the default package is that it's a JNIed Class
> by someone else. If I put it to my own package, I'll have to reJNI. And it's
> a pain because I am not sure I can get it compiled successfully.

  Well, you may be able to work around, however painfully, by using a
Middleman class as your startup class, and using the JNIed class through
an interface.

/* no package here */
import JNIedClass; // The class you can't recompile in a package.
import com.kay.JNIedInterface; // New interface that you write.
class Middleman implements com.kay.JNIedInterface {
 private JNIedClass passthru=new JNIedClass();
 public static void main(String[] args) {
   com.kay.MainProgram.jniProxy=new Middleman();
   com.kay.MainProgram.main(args);
 }
 public void jnicall() {
  passthru.jnicall();
 }
}
-----------
package com.kay;
public interface JNIedInterface {
 void jnicall();
}
-----------
package com.kay;
/* Your old class that can't call JNIedClass directly. */
public class MainProgram {
 public static JNIedInterface jniProxy;
 public static void main(String[] args) {
  jniProxy.jnicall();
  ...
 }
}

--Mike Amling
Michael Amling - 12 Dec 2003 02:32 GMT
>>>>I have a java fiile that resides at the default package under src
>
[quoted text clipped - 39 lines]
> the root of the jar file of the class from which the program started?
> Conceptually, you could have multiple unnamed packages.

  That's all been true since 1.0.3.

--Mike Amling
Dale King - 16 Dec 2003 21:03 GMT
> >>>>I have a java fiile that resides at the default package under src
> >
[quoted text clipped - 41 lines]
>
>    That's all been true since 1.0.3.

There is no such thing as 1.0.3. Java went from 1.0.2 to 1.1. And I fail to
see where I put any limits on when that was true. What has not been true is
that javac would not compile a file importing from the default package. That
did not happen until 1.4. There were other compilers, most notably Visual
Age for Java that did not support this as allowed by the JLS.
--
 Dale King
Ville Oikarinen - 17 Dec 2003 08:49 GMT
> The problem becomes complicated when you start thinking of multiple
> hierarchies in the class path. For example, if I have a program in multiple
> jar files and a class uses a class in a class in an unnamed package.Where
> would it get it from? From the root of the jar file that class is in or from
> the root of the jar file of the class from which the program started?
> Conceptually, you could have multiple unnamed packages.

This "problem" isn't tied to the default package only. Whenever you have the
same fully qualified class in several jars, the JRE needs to decide what version
to use. The default ClassLoader will use the one mentioned first in the
classpath.

Ville Oikarinen
Raymond DeCampo - 13 Dec 2003 23:39 GMT
> Hi:
>
[quoted text clipped - 8 lines]
>
> So what should I do to import such class?

In theory, if the class is in the default package, you shouldn't have to
import it.  You are using the fully qualified name already.

Ray
Andrew Marshall - 13 Dec 2003 21:27 GMT
>> Hi:
>>
[quoted text clipped - 13 lines]
>
> Ray

Standard programming practices say that the default package is only used
for simple programs. Any multi-packaged programs you make should be
defined in their own packages, that have descriptive titles so that
another programmer 4 years down the road can pick up your project and pick
up where you left off.

Andrew
Moshe Sayag - 14 Dec 2003 08:54 GMT
A detailed discussion is can be found in the bug report:
http://developer.java.sun.com/developer/bugParade/bugs/4361575.html

I think login is required

Moshe

> Hi:
>
[quoted text clipped - 11 lines]
> thanks,
> -Kay
Y2KYZFR1 - 17 Dec 2003 15:27 GMT
> Hi:
>
[quoted text clipped - 11 lines]
> thanks,
> -Kay

you don't import things from the default package, that is why it is
called the default package everything is already imported by default
Dale King - 17 Dec 2003 23:11 GMT
> > Hi:
> >
[quoted text clipped - 11 lines]
> you don't import things from the default package, that is why it is
> called the default package everything is already imported by default

Wrong on two counts. First it isn't called the default package. It is called
the unnamed package. And nothing from the unnamed package is imported by
default. They are visible to other members of the unnamed package since they
are in the same package, but they are not visible to compilation units in
other packages.

--
 Dale King


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.