-2

MY code:

package JAVA_PROGRAMS;

public class fibonacci{

public static void main(String args[])
{
    int n1=0,n2=1,n3,i,count=10;

    System.out.print(n1+" "+n2);

    for(i=2;i<count;i++)
    {
        n3=1+n2;
        System.out.print(" "+n3);
        n1=n2;
        n2=n3;
    }
}

}

Error : Error: Could not find or load main class fibonacci Caused by: java.lang.NoClassDefFoundError: JAVA_PROGRAMS/fibonacci (wrong name: fibonacci)

  • What is the name of the java file? – Chota Bheem Feb 04 '21 at 07:57
  • Because of the `package` statement, the class is named `JAVA_PROGRAMS.fibonacci`, and if you run with a simple `java JAVA_PROGRAMS.fibonacci` command, then you need file `JAVA_PROGRAMS/fibonacci.class` relative to the current working directory. See duplicate link up top for more information. – Andreas Feb 04 '21 at 08:21

1 Answers1

-1

This generally happens if you saved your java file with a name that is not the same as the "public class" name of your file.

In your case the file should be saved as 'fibonacci.java'.

And the way you compile it on a terminal is :- javac fibonacci.java, and to run the program you do java fibonacci.

But on vscode you probably just have to hit the "run" button, I am not sure of it as I like working on terminal.

  • on terminal it woks fine but while using run the error occurs – Aman Chauhan Feb 04 '21 at 07:27
  • Hmm...strange. So the problem is only with VSCode?, or do you mean that everything is fine up till the compile step and then things are not working? It would be more helpful if you could share the screenshot of your problem through a drive link. – Avhijit Nair Feb 04 '21 at 07:30
  • I moved few programs in the new folder and try to run them but the error occurs as above but simply using the terminal it works fine. Don't know what happened there. – Aman Chauhan Feb 04 '21 at 07:35
  • https://imgur.com/zZZhP9v – Aman Chauhan Feb 04 '21 at 07:46
  • This answer is entirely ignoring the very important effect of the `package JAVA_PROGRAMS;` statement, making the answer useless. – Andreas Feb 04 '21 at 08:23