wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes package import confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package import confusion" Watch "package import confusion" New topic
Author

package import confusion

Jt Walker
Greenhorn

Joined: Sep 21, 2012
Posts: 2

I've been struggling with this for a day or two now and cannot seem to grasp what is going on.

The following code allows me to create a reference to the public class JOptionPane. As the wildcard import includes all the types in the package.the full class name is not needed.


If I create a public class in a pkg :


and then try to create a reference the compiler chokes :


Sample run :

I can't figure out why it fails and why is it referring to the source file ? The file pkg1/Qwerty.class exists. CLASSPATH is not set and so includes the current directory.


Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Your package structure should be mapped to folder structure where your source files reside. Otherwise, you'll confuse javac.
But here I see you have all the source files in the same directory. Once you compile Qwerty.java, you should get subfolder named pkg1 in your current folder. Either move your Qwerty.java to that folder, or delete it after Qwerty.class is created in the subfolder, to avoid mentioned confusion.

Edit:
This is how it is supposed to look like:
.../Java/WorkSpace/src/MessDialog.java
.../Java/WorkSpace/src/Asdf.java
.../Java/WorkSpace/src/pkg1/Qwerty.java

and after you compile it, you should get something like this (suppose the bin is subfolder where you put you .class files):

.../Java/WorkSpace/bin/MessDialog.class
.../Java/WorkSpace/bin/Asdf.class
.../Java/WorkSpace/bin/pkg1/Qwerty.class


The quieter you are, the more you are able to hear.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jt Walker wrote:CLASSPATH is not set and so includes the current directory.



That's where the problem lies. You need to have classes in directories corresponding to their packages and include the parent directory of the package root in your classpath.



My classes are


So to compile DatabaseMatcher.java, I'd do


Note that you cannot "trade off" elements of your classpath by cd-ing deeper into its directory. The classpath element must include the parent directory of the package root.



Jt Walker
Greenhorn

Joined: Sep 21, 2012
Posts: 2


Thanks for the excellent help. I was led astray by SCJP 6 ( Raposa ) page 19 where it states
You might have also noticed that the source code files in \src use the same directory
structure as their package names. This is not a requirement for your .java files; they can
be stored in any directory.


Moved the source to pkg1/Qwerty.java and everything is peachy.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jt Walker wrote:
Thanks for the excellent help. I was led astray by SCJP 6 ( Raposa ) page 19 where it states
You might have also noticed that the source code files in \src use the same directory
structure as their package names. This is not a requirement for your .java files; they can
be stored in any directory.


Moved the source to pkg1/Qwerty.java and everything is peachy.


Yeah, that comment, while correct, can lead to confusion.

The Java compiler spec does not require that .java files be in a directory that matches their package. A given implementation of the compiler might require it though. And it's a good idea to do it that way regardless, to avoid confusion.

Additionally, the standard classloader mechanism does require that a class's package name corresponds to its subdirectory relative to a classpath root.

So even though it's not strictly required in all situations, it's best just to act as if it is.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Jt Walker wrote:
Thanks for the excellent help. ...


Any time, and welcome to the Ranch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package import confusion
 
Similar Threads
Abstract class and Access Modifiers
Packages
package access trouble
errors generated when running Ant build file in Eclipse
Package declaration !