Forums Register Login

package import confusion

+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.



+Pie Number of slices to send: Send

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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



Any time, and welcome to the Ranch!
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2318 times.
Similar Threads
Package declaration !
Packages
package access trouble
Abstract class and Access Modifiers
errors generated when running Ant build file in Eclipse
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:34:29.