Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for those two links!

Somehow I get the impression that understanding such details will strengthen one's foundation in the Java language...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic