I have the following directory hierarchy : /a/b/.
If I created 2 classes whereby I wanted to package them in /a/b/ would I simply add package a.b; at the top of their respective .java files?
If the first class needs to create an instance of the second class created would I need to do an import of the current package?
I've tried a few things out but when I try calling the first class it complains about referencing the second class. The first class extends JApplet while the second extends JPanel. Any suggestions would be appreciated. Thanks.
Yes, just add "package a.b", and no, you don't need to import the one class in the other file. The thing that trips up most people in dealing with packages is that in general, you have to "do things" from the directory containing "a", not the directory containing /a/b . For example, if your current directory is the one containing "a", then you can type "javac a/b/.java" to compile your classes, and "java -cp . a.b.Foo" to run the class in a/b/Foo.class . Trying to operate out of the directory containing the source files just won't work.
This is the class' init that tries to instantiate a MDrawing object (consider it class A and B, respectively). The MDrawing class is separate but in the same package.
So class mdDraw extends JApplet and below is the init method, MDrawing extends JPanel.
When trying to compile the mdDraw class I get the following.
So what's your compile command line, and what's your current directory when you type it?
Also, one silly thing: make sure any copies of your old, un-packaged MDrawing.java and MDrawing.class are deleted; if javac finds them it will mess things up.
No, they should be where they are -- that's where Java will expect to find them based on the package name. Now you'll need to specify the Applet class name as mods.md.mDraw .
The thing I find strange is the 2 parameters that are chars seem to be getting marked in the 2 error messages (arrow pointing to them in compile line errors).
Ernest, any other suggestions? Just doesn't seem to make much sense in why it isn't working. So javac *.java in the top level md directory doesn't do it as well.
C:....\cgi-bin\md>javac /mods/md/mDraw.java