wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unable to grasp compiling and running multiple files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unable to grasp compiling and running multiple files" Watch "Unable to grasp compiling and running multiple files" New topic

Unable to grasp compiling and running multiple files

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434

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

Created two java files in the same directory:



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.


Marriage Made in Heaven
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 39535
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".

Ping & DNS - updated with new look and Ping home screen widget
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
I just realized it's covered in chapter 10 of Sierra/Bates; will check out the link as well.
I agree. Here's the link: http://aspose.com/file-tools
subject: Unable to grasp compiling and running multiple files
Similar Threads
packages from KB
Question on Packages from Kathy Sierra Book
what happens at runtime?
Slightly different compile error msgs using 1.4.1
Access modifier doubt