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

Unable to grasp compiling and running multiple files

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am going through Sierra/Bates code and am looking at chapter 1.

Created two java files in the same directory:

Beverage.java



Tea.java




I am able to compile Beverage.java. However, I get error when I run Beverage.java

Exception in thread "main" java.lang.NoClassDefFoundError: Beverage (wrong name: cert/Beverage)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:698)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)



When I compile Tea.java, I get errors

Tea.java:2: package cert does not exist
import cert.Beverage;
^
Tea.java:4: cannot find symbol
symbol: class Beverage
class Tea extends Beverage { }
^
2 errors



Is this something that will be covered later in Sierra/Bates book. From what I understand, Tea.java should not compile because Beverage is not public. However, I am unable to test this out because I cannot even run Beverage.java.

Thanks!

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your classes are in packages, then they need to be in specific directories that correspond to the package hierarchy. This is probably covered somewhere in the book, but in case it's not, start reading at http://java.sun.com/docs/books/tutorial/java/package/index.html, particularly "Managing Source and Class Files".
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized it's covered in chapter 10 of Sierra/Bates; will check out the link as well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic