aspose file tools*
The moose likes Beginning Java and the fly likes Run time error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Run time error " Watch "Run time error " New topic
Author

Run time error

Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

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
Marshal

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


Ping & DNS - updated with new look and Ping home screen widget
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

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

Joined: Jul 26, 2007
Posts: 9990
    
    7

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Varshini Priya
Ranch Hand

Joined: Feb 17, 2008
Posts: 93

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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
  • 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

    Joined: Oct 13, 2005
    Posts: 36486
        
      16
    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18117
        
      39

    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18117
        
      39

    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

    Joined: Feb 17, 2008
    Posts: 93

    Thanks a lot Henry. It did work
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36486
        
      16
    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."
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Run time error
     
    Similar Threads
    Public Members access
    doubt in package; compiling
    Problem with setting classpath
    Question No. 11- Chapter 10 K&B book SCJP 1.6 - Doubt on Answer
    Compiling multiple source files