File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Retro import Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Retro import" Watch "Retro import" New topic

Retro import

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I have a bunch of classes in a directory without any package references. So the directory contains all the java and class files. If I want to add a package, I go into one of these java files and stick the package reference at the top. Now it won't compile because all the other classes are not in that package (and this class interacts with them): they're not in any package.
How can I compile this class now and eventually add a package to all of the classes? Is there a way to do something like . In other words, import classes that has no package.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
In 1.3 and before you can import classes from a default package by just importing the name of the class like:

I'm really not sure if you can do this:

but you can give it a shot. If that doesn't work you will need to import all the class as in the first import. Note that importing from the default (or nameless) package is not allowed in 1.4 nor should it have ever been allowed. Here is what Sun has to say about the subject:
The compiler now rejects import statements that import a type from the unnamed namespace. Previous versions of the compiler would accept such import declarations, even though they were arguably not allowed by the language (because the type name appearing in the import clause is not in scope). The specification is being clarified to state clearly that you cannot have a simple name in an import statement, nor can you import from the unnamed namespace.
To summarize, the syntax import SimpleName; is no longer legal. Nor is the syntax import ClassInUnnamedNamespace.Nested; which would import a nested class from the unnamed namespace. To fix such problems in your code, move all of the classes from the unnamed namespace into a named namespace.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
I agree. Here's the link:
subject: Retro import
It's not a secret anymore!