wood burning stoves*
The moose likes Beginning Java and the fly likes Help!!! I know packages cant be this difficult. :-( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help!!! I know packages cant be this difficult. :-(" Watch "Help!!! I know packages cant be this difficult. :-(" New topic
Author

Help!!! I know packages cant be this difficult. :-(

C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
I am trying to understand packages but its getting a bit difficult.



...compiles fine



Why is my second class giving me this compile error?
package com.example.test does not exist
[ February 23, 2005: Message edited by: Crusty Magmanum ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

Crusty, can you change your displayed name to conform to the JavaRanch
naming policy. In short, your name must be a first name and last name, separated by a space and not obviously fake. You can do that here.
Now, about packages. Unless you tell the compiler differently, it will write the class file it generates into the current working directory. Packages work like the file system, so if you have a package named com.example.test, the compiler will expect to find classes in that package in a directory named com/example/test under the current one (or in the classpath, but that's another story). To have the compiler do this automatically, compiler with the -d flag and specify a directory ("." for the current directory is fine). Consult the Java Tutorial chapter on Packages for more info.
[ February 23, 2005: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might also refer to the chapter on packages in Bruce Eckel's Thinking in Java...
http://www.faqs.org/docs/think_java/TIJ307.htm


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I suspect that the issue is with your directory structure in the file system. Packages correspond to directory names, so for the example you gave, you should have the following structure:

You can compile all of your classes from C:\java:

If MyClass.java is not compiled or has been modified, javac should detect this and compile it before it compiles MyClass2.java.

If this doesn't help fix your problem, please describe the directory structure you are using. Hopefully it will help us think of other things you can try.

Keep Coding!

Layne
[ February 23, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
Thanks so much for ur suggestions but I'm still not getting anything positive. This is my commandline input

c:\>javac -classpath /somefile/foo.jar -d classes MyClass2.java

The structure is how u suggested but I am not sure how -classpath (in the commmand-line) affects the import in MyClass.java



...my mum calls me crusty
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help!!! I know packages cant be this difficult. :-(