Results 1 to 5 of 5
  1. #1
    ITSlave is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Error: Could not find or load main class

    I've searched through the forum and reviewed the other threads on this topic and can't figure out what I'm doing wrong. I'm a java noob taking a grad class that someone didn't tell me would require java. My first assignment was to modify 2 classes, a client and server class for outputing the current date. Instead, I'm supposed to make it output a random joke. The code is working in the Netbeans IDE, but I cannot get it to run from the command line. I am able to compile the code just fine, but when I go to run it here is what I get:

    c:\Temp>java JokerServer
    Exception in thread "main" java.lang.NoClassDefFoundError: JokerServer (wrong na
    me: Assignment1/JokerServer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :791)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:14
    2)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:472)

    I also tried:

    c:\Temp>java c:\temp\JokerServer
    Error: Could not find or load main class c:\temp\JokerServer

    And:

    c:\Temp>java -cp . JokerServer
    Exception in thread "main" java.lang.NoClassDefFoundError: JokerServer (wrong na
    me: Assignment1/JokerServer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :791)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:14
    2)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:472)


    What am I doing wrong?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Error: Could not find or load main class

    Your class is in a package named 'Assignment1' so its full name is 'Assignment1.JokerServer' and you should run it as such (your JVM was trying to tell you so).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: Error: Could not find or load main class

    try java <package-name>.JokeServer
    Ig that doesn't work just post the code so that we can see what might be wrong.

  4. #4
    ITSlave is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Error: Could not find or load main class

    It gave me an error when I tried to use java Assignment1.JokerServer. However, I went into the project, and deleted the package. Once the class files were in the default package, I was able to run them. Thanks for the help.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Error: Could not find or load main class

    If it gave you an error then you ought to post it here.

    I suspect you put the class in a package Assignment1, but didn't actually have the class file in an Assignment1 directory.
    Removing the package statement simply stuck it in the default package, which is not a good habit to get into.

Similar Threads

  1. Replies: 10
    Last Post: 02-02-2012, 08:26 PM
  2. Replies: 29
    Last Post: 02-02-2012, 08:23 PM
  3. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  4. runtime error 'could not find main class' ?
    By ruud00000 in forum Java Applets
    Replies: 5
    Last Post: 12-31-2010, 05:58 PM
  5. [SOLVED] Could not find the main class: error...
    By bobleny in forum New To Java
    Replies: 21
    Last Post: 03-02-2010, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •