Results 1 to 3 of 3
  1. #1
    Kalpesh is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Exclamation Receiving error "Could not find the main class"

    I have recently started learning Java...and I wrote first code of mine as below:
    [Code]
    public class Kalpesh
    {
    public static void main (String args[])
    {
    System.out.println("Kalpesh");
    }
    }


    I also have set classpath and path variables correctly. The program compiles fine using command line:
    javac C:\Kalpesh.java

    But on executing, I receive below error:
    java C:\Kalpesh

    Error is displayed as:

    C:\Program Files\Java\jdk1.6.0_23\bin>java C:\Kalpesh
    Exception in thread "main" java.lang.NoClassDefFoundError: C:\Kalpesh
    Caused by: java.lang.ClassNotFoundException: C:\Kalpesh
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    Could not find the main class: C:\Kalpesh. Program will exit.


    I tried using java -cp . Kalpesh command as well but still receiving same error. Can someone help me out?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The java executable takes a class name as an argument - not a filename like the javac executable. So it doesn't really make sense to say

    Java Code:
    java C:\Kalpesh

    The usual way of running the program is

    Java Code:
    java -cp . Kalpesh

    You have tried this, but there are a few things you can check.

    (1) The command must be issued from the directory containing Kalpesh.class
    (2) The file Kalpesh.class must really be present in that directory. Ie the compilation must have been sucessful. Check this using the dir command
    (3) The name of the class must really be Kalpesh. If the class belongs to a package then both the name of the class and the location where the command should be run will be different.
    (4) The Kalpesh class must have a well defined "entry point". If you make a mistake in how you declare the main() method there will be a problem. If you are unsure about this, post the Kalpesh.java code

  3. #3
    Kalpesh is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Hey....
    The issue is resolved...
    You were correct that I needed to issue 'Java' command from the directory where my .class file resides. I was receiving error saying 'java is not recognized command....

    I then set 'Path' variable to the directory containing .class file and the issue got resolved..

    Thanks a lot for help..!!!

Similar Threads

  1. Error message "could not find the main class"
    By srwpchelp in forum New To Java
    Replies: 8
    Last Post: 12-23-2010, 09:15 PM
  2. "Could not find the main class"
    By CCarr518 in forum New To Java
    Replies: 9
    Last Post: 09-25-2010, 06:11 PM
  3. Replies: 3
    Last Post: 08-25-2010, 10:21 AM
  4. Replies: 6
    Last Post: 05-18-2010, 05:11 PM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 PM

Posting Permissions

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