aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cloo() imports package cert.Roo(), yet it cannot have it's own package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cloo() imports package cert.Roo(), yet it cannot have it Watch "Cloo() imports package cert.Roo(), yet it cannot have it New topic
Author

Cloo() imports package cert.Roo(), yet it cannot have it's own package

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello,

I have two programs based from Sierra/Bates Chapter 1:

saved in /one/src/Roo



saved in /one/src/Cloo




In the terminal, I navigate to one directory and type in:

javac -d ./classes/ ./src/Roo.java ./src/Cloo.java

This works. Then I navigate to classes directory and type in:

java Cloo

It works!

Here is the problem...notice how in Cloo.java, I commented out package notcert. If I uncomment it, I can compile the program. In order to run the program, I navigate to classes/notcert directory and type in

java Cloo

and it gives the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: Cloo (wrong name: notcert/Cloo)
....
Could not find the main class: Cloo. Program will exit.



At this point, I am unable to find an easy-to-understand explanation and resolution. My understanding is that a subclass can belong to a separate package and STILL inherit protected methods.

Please assist.


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Here is the problem...notice how in Cloo.java, I commented out package notcert. If I uncomment it, I can compile the program. In order to run the program, I navigate to classes/notcert directory and type in

java Cloo

When you put a package statement in Cloo.java, you'll have to use the fully qualified name of Cloo class to run it i.e.
java notcert.Cloo
And this command needs to be run from the parent directory of notcert folder which contains Cloo.class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Thanks Ankit, it now works!

You know of any articles that discusses this. I am on chapter 5 of Sierra/Bates and so far, this seems to be a rather tough concepts to grasp
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well this problem in itself should not have been hard for you to solve. The error message even indicated that you are using the wrong class name (wrong name: notcert/Cloo). With java command you always use the fully qualified class name. I'm not really sure what type of article can help you with this. For the java command itself, you can see this page and for general javac, java commands with packages, this article should help...
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Thanks for those two links!

Somehow I get the impression that understanding such details will strengthen one's foundation in the Java language...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cloo() imports package cert.Roo(), yet it cannot have it's own package