This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main() & package question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main() & package question" Watch "main() & package question" New topic
Author

main() & package question

Mark Supinski
Greenhorn

Joined: May 14, 2010
Posts: 2
Hi All-

Working my way through S&B and ran into a behavior I don't understand.

Here's the code:


This code compiles fine. When trying to run the code, however, I get a runtime exception as follows:


My question is this: If I remove the package statements from this file and the associated Zoo.java file, the code runs fine. With the package statements, I get the runtime error above.

I can't find an explanation of this behavior anywhere. Can someone enlighten me?

Thanks,

MArk
Mark Supinski
Greenhorn

Joined: May 14, 2010
Posts: 2
So I found my own solution.

package classes must be in a directory with a name corresponding to the package declaration. Moving the files into a foobar directory eliminated the runtime exception.

Note, however that java must be invoked from the root level. Thus, if one runs java Moo from within the foobar directory, the runtime exception occurs. If one steps back to the parent directory and runs java foobar/Moo it works properly.

Mark
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

java foobar/Moo it works properly.

I'm assuming you meant java foobar.Moo


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: main() & package question