2

I have downloaded a java developers kit for my 64bit windows 7, wrote down my code in the notepad, though the code is compiling from the command prompt and creating a .class file, but its refusing to run showing the error code:

java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first   
  at java.net.URLClassLoader$1.run(Unknown Source)   
  at java.security.AccessController.doPrivileged(Native Method)   
  at java.net.URLClassLoader.findClass(Unknown Source)   
  at java.lang.ClassLoader.loadClass(Unknown Source)   
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)   
  at java.lang.ClassLoader.loadClass(Unknown Source) 
Could not find the main class: first.  Program will exit. Exception in thread "main"

I have made sure more than once that the file name and the class name are exactly same(i have kept them smallcase 'a' just to be sure). But still no avail, could you please suggest a few solutions please.. I'm new to java i'm basically a C/C++ programmer.

6
  • 1
    Hi, can you provide your source code here? Commented Jul 1, 2011 at 15:15
  • I am assuming you're trying to run your Java program from the command line -- can you share with us what you're running? Commented Jul 1, 2011 at 15:15
  • 1
    What is the command for executing? do you use 'java -jar YourClass.jar'? Have you get the jar file? Commented Jul 1, 2011 at 15:17
  • 2
    @Ron: welcome to Java! note that unlike c++, java has a strong convention that class names start with a capital letter, so you should rename your class to First (and not first). note that it is only a convention and will NOT solve your problem. Commented Jul 1, 2011 at 15:18
  • 1
    Echoing above, please provide the command-line you are using to run java. The problem seems to be that java is not finding the class you compiles on its classpath, which is the list of directories it searches looking for .class files Commented Jul 1, 2011 at 15:20

5 Answers 5

5

A java program has this basic structure:

ClassName.java

public class ClassName
{
    public static void main(String[] args)
    {

    }
}

try using this outline to generate your code.

compile and run with:

javac ClassName.java
java ClassName
Sign up to request clarification or add additional context in comments.

Comments

1

I used to get this error when I ran a Class file.

Try doing: java NameOfClass

You don't need the .java extension when running, but you need it for compiling. That was always the problem I used to have.

Comments

1

Did you set your classpath?

http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html

java -classpath <path> <classname>

1 Comment

if the program has compiled correctly , what is the point of asking Did you set your classpath?
0

You must provide with the code.

Anyway, from the Java Docs, class ClassNotFoundException:

Thrown when an application tries to load in a class through its string name using:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.

Must read link : Tip: Causes of java.lang.ClassNotFoundException

Comments

-1

It is quite possible after compiling your code you would be writing java filename.java. Because this sought of exception occurs then.

After compiling your program using javac filename.java use the command to start your interpreter java filename.

As you enter this command java interpreter automatically starts interpreting filename.class.

commands :

javac filename.java   // to start compiling
java filename         // to start interpreting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.