Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run time error

 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Im trying to execute the below code
[Code]

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.

Output

D:\java1>set classpath=%classpath%;D:\Java; D:\java1;

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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

Im sorry. But I dint get You. "they need to be in subdirectories of the same name. " - Im not sure what this actually,means. Please Assist
 
Maneesh Godbole
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Ulf meant was that the class Sludge.java should be saved as D:\Java\cert\Sludge.java and Goo.java should be saved as D:\java1\book\Goo.java
 
Varshini Priya
Ranch Hand
Posts: 95
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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.

Output

D:\java1\book>javac Goo.java

D:\java1\book>set classpath=%classpath%;D:\Java\cert;D:\java1\book;

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)
 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try....

D:> cd \Java
D:\Java> javac cert\Sludge.java

D:\Java> cd \java1
D:\java1>javac book\Goo.java

D:\java1>java book.Goo

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 50216
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Have both Sludge and Goo compiled correctly?
  • 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
    Pie
    Posts: 50216
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 21500
    84
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    set classpath=%classpath%;D:\Java\cert;D:\java1\book;


    And the classpath should be the top of the packages... so this line should be...

    set classpath=%classpath%;D:\Java;D:\java1;

    Henry
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 21500
    84
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Henry
     
    Varshini Priya
    Ranch Hand
    Posts: 95
    Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot Henry. It did work
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50216
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:I thought I would be nice and fix the code tags.... But it would not matter.
    I tried too; if it works I usually say "see how much better it looks."
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic