Results 1 to 11 of 11
  1. #1
    bks504 is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default Problem while executing programs

    while executing a java prog i am getting a Error.:
    Exception in thread "main" java.lang.NoClassDefFoundError: first
    Program: first.java
    class first
    {
    public static void main(String args[])
    {
    System.out.println("Hello");
    }
    }

  2. #2
    paty is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default

    it could be:
    * Name of the file class that must be executed does not have the same name that the principal class (that one that it contains the method main)

    * One has written the extension class later of the name of the principal class on having tried to execute the program.

  3. #3
    Swamipsn is offline Member
    Join Date
    Jul 2007
    Posts
    74
    Rep Power
    0

    Default

    Hi,
    Everyday somebody thread this same problem!! anyway the rootcasts may be
    1.Filename & class name mismatch
    2.Class Path problem

  4. #4
    levent Guest

    Default

    Everyday somebody thread this same problem!!
    You are right, Swamipsn.

    We can create a FAQ for these, put a sample question there and place references to similar threads.

    Then when we see a similar thread we can give the FAQ url and question number. Then the user having same problem can follow the links to solve his problem. And if he can not solve he can continue asking..

    What do you think?

  5. #5
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    I like it very much. There does seem to be quite a bit of repetition in certain questions.

  6. #6
    bks504 is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default the filename i typed is same

    this is the way i executed
    >javac first.java
    this is compiling correctly but when i am executing
    >java first
    it is giving Exception
    i even set the classpath in this manner
    >set CLASSPATH=c:\java\jdk1.6.0\bin
    but the same error i am getting please help me

  7. #7
    bks504 is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default the filename i typed is same

    this is the way i executed
    >javac first.java
    this is compiling correctly but when i am executing
    >java first
    it is giving Exception
    i even set the classpath in this manner
    >set CLASSPATH=c:\java\jdk1.6.0\bin
    but the same error i am getting please help me

  8. #8
    Swamipsn is offline Member
    Join Date
    Jul 2007
    Posts
    74
    Rep Power
    0

    Default

    Hi,
    Check first.class file is crated or not? after the successful compilation .class
    file must be there. Then check .class is presented in the same directory which
    is you try to run via command prompt. Then check java.exe file is presented in c:\java\jdk1.6.0\bin or not..? if it presented try to open it if its corrupted you cant open it.

  9. #9
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    It is the class path. From the directory where first.class is, run this
    >java -cp . first

    The "-cp ." tells it to look in the current directory for class files.

  10. #10
    bks504 is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    the solution of >java -cp . filename is giving correct results , where as when i am not typing the -cp i am getting the Exception:
    Exception in thread "main" java.lang.NoClassDefFoundError: filename
    why it is happening like this , can any one give me the correct solution what should i do.Help me folks.

  11. #11
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    Think of it this way, PATH tells windows where to find executable files when one is typed on the command line. CLASSPATH does the same thing but for Java so it can find class files to run. There are two ways to set the CLASSPATH: 1. set the environment variable CLASSPATH and the second is to use the -cp option when running java. With that said if the CLASSPATH=c:\java;c:\myclasses, java will only look in those two locations for class files. OK that is not entirely true it will also look in JAVA_HOME/lib/ext but that is besides the point. Now with the above set say you are trying to run a program in c:\code\myproject and you have a class Test.class in that directory. Java will not find the Test class because it is looking at the CLASSPATH only. The current directory is not implied. If you want Java to find your class you must tell it where to look. The "-cp ." says, "Hey look in the current directory too". Some like to put the "." at the end of the CLASSPATH env variable like so "c:\java;c:\myclasses;."

    The best solution is to have a build script that sets the CLASSPATH explicitly for your project as you compile and run it.

Similar Threads

  1. Executing a jar file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 02-14-2014, 09:02 AM
  2. I need a simple programs
    By mikau in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 03:37 AM
  3. Problem with executing
    By mcal in forum New To Java
    Replies: 2
    Last Post: 02-09-2008, 01:51 PM
  4. Replies: 1
    Last Post: 01-05-2008, 07:48 PM
  5. Problem with executing .jar files
    By hiranya in forum NetBeans
    Replies: 2
    Last Post: 10-02-2007, 07:24 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
  •