This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
package cert;
public class Sludge
{
public void testIt()
{
System.out.println("sludge");
}
}
[\code]
I have stored the above code in this location D:\Java\Sludge.java and the below one. I have saved it in D:\java1\Goo.java
[code]
package book;
import cert.*;
class Goo
{
public static void main(String [] args)
{
Sludge o = new Sludge();
o.testIt();
}
}
[\code]
I was able to compile the code fine. I did set the below classpath. But was still getting errors.
D:\java1>java Goo
Exception in thread "main" java.lang.NoClassDefFoundError: Goo (wrong name: book
/Goo)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
D:\java1>
Im not sure why Im getting the above error. Please assist. Many Thanks
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35252
7
posted
0
The directory hierarchy must correspond to the package hierarchy. Since those classes are in packages "cert" and "book", respectively, they need to be in subdirectories of the same name.
I did put the respective classes in the subdirectories and tried to run the program. getting the same error as above. Im able to compile it fine. But not able to run it.
D:\java1\book>java Goo
Exception in thread "main" java.lang.NoClassDefFoundError: Goo (wrong name: book
/Goo)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
If so, navigate to the folder which contains the cert and book folders. I think that means D:\Java
You need to call the class with the main method by its fully-qualified name: java cert.Goo
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
4
posted
0
By the way: please use the CODE button and maintain indentation. Don't write the code tags by hand; the button is quicker and less prone to spellling errors.
Campbell Ritchie wrote:By the way: please use the CODE button and maintain indentation. Don't write the code tags by hand; the button is quicker and less prone to spellling errors.
BTW, I thought I would be nice and fix the code tags.... But it would not matter. There is no formatting to the code that has been included.