Hello - I have a real mental block about classpaths and packages. Can someone please set me straight?
I have a layout manager at
~ / JavaStuff / LAYOUT_MANAGERS / Test01LayoutManager.java which looks like
I compile it successfully from ~ / JavaStuff / LAYOUT_MANAGERS with
javac -d bin/com/danf ./Test01LayoutManager.java so that the layout manager class file ends up as
~ / JavaStuff / LAYOUT_MANAGERS / bin / com / danf / testLayout / Test01LayoutManager.class Good so far.
I also have a source file that calls this layout manager at
~ / JavaStuff / TEST / Test01Harness.java which looks likeI compile it with
javac -d bin -classpath "~/JavaStuff/LAYOUT_MANAGERS/bin" ./Test01Harness.java and it produces this error:
Test01Harness.java:8: cannot access com.danf.testLayout.Test01LayoutManager
bad class file: ~/JavaStuff/LAYOUT_MANAGERS/bin/com/danf/testLayout/Test01LayoutManager.class
class file contains wrong class: testLayout.Test01LayoutManager
Please remove or make sure it appears in the correct subdirectory of the classpath.
where line 8 isI apologize ahead of time if this is embarrassingly obvious. I didn't want to clutter the post with the entire programs since I thought this was enough to go on. I guess what I really need (beyond the scope of this post) is a simple primer about classpaths, packages, and import statements.
Thanks very much for any attention you can give this.
Your package declaration in Test01LayoutManager should be like
Joined: Feb 03, 2009
Thanks Narendira, but when I do that and recompile it the resulting layout manager class ends up farther down in ~ / JavaStuff / LAYOUT_MANAGERS / bin / com / danf / com / danf / testLayout / Test01LayoutManager.class.
The directory structure of your source files and compiled class files must match the package hierarchy that you're using. So if your class is in package com.danf.testLayout, it should be in a directory com/danf/testLayout.
Note that in Test01LayoutManager.java you do "package testLayout;" but in Test01Harness.java you have "import com.danf.testLayout.Test01LayoutManager;". Those two are not in accordance with each other.
Thanks very much Jesper - My thinking was that compiling the layout manager placed the class at
~ / JavaStuff / LAYOUT_MANAGERS / bin / com / danf / testLayout / Test01LayoutManager.class
Then when compiling the test source with classpath
~ / JavaStuff / LAYOUT_MANAGERS / bin
the remaining part (com / danf / testLayout / Test01LayoutManager.class) would be resolved by the import statement
Your comment about the package and import statements being in accordance must be key though - I was assuming that as long as combining the classpath with the import statement gave the same path as the location of the class, that was enough.
Off now to read the Sun Java Tutorial (which I have in the past but obviously need a refresher).