This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Referencing classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Referencing classes" Watch "Referencing classes" New topic
Author

Referencing classes

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Rob,

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.


[Jess in Action][AskingGoodQuestions]
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
Thanks for the info. Here's what I'm getting

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.

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
Here's also the skeleton of the class attempting to be instantiated.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

OK, can you show me the directory layout, including the file names, and the package statements, and the compile command line you're using ?
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
/srv/www/cgi-bin/md/
---> html page location with embedded applet
---> mods/md/mDraw.class
---> mods/md/MDrawing.class



Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
I go into the md/mods/md directory and run :

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Rob Hunter wrote:I go into the md/mods/md



OK, that's it -- remember, I told you above you'll need to go to the /md directory (i.e., two level above where you are now) and type

javac mods/md/*.java

The compiler and the Java runtime will both translate the package names into directory paths and use those paths to look for classes.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
Tried that as well and it didn't work either. Now the java classes are both in md/mods/md/, should they be under md (first level)?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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 .
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
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).
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
The errors keep the class file from getting written. I still get the same errors running it in this way from the first level md directory.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
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
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 815
I was using javac /dir1/dir2/*.java but was not using -cp .. It now compiles fine.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Please avoid long lines in code tags; hey are difficult to read.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Referencing classes
 
Similar Threads
Abstract Class
package urgent!
Access modifier confusion
Inheriting a "package" accessible member
package problem