• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Referencing classes

 
Ranch Hand
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rob Hunter
Ranch Hand
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's also the skeleton of the class attempting to be instantiated.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I go into the md/mods/md directory and run :

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was using javac /dir1/dir2/*.java but was not using -cp .. It now compiles fine.
 
Marshal
Posts: 79809
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid long lines in code tags; hey are difficult to read.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic