-3

I am writing my first program in java in VSCODE my java version is 10.0.2 when i run my code it shows an error I have my environmental variables and paths set.

Error: could not find or load main class (my class name) Caused by: java.lang.ClassNotFoundException: (my class name)

here's my code

class main {
public static void main ( String [] args)
{
    System.out.println("HELLO EVERYONE");
}
}

Is it due to the latest verion of java or anything else?

juzraai
  • 5,693
  • 8
  • 33
  • 47
Ayush Bali
  • 21
  • 2
  • 7
  • 2
    could not reproduce the error.Is your file saved as main.java? – Madhan Varadhodiyil Jul 29 '18 at 10:55
  • ```class main {...}``` implies "(my class name)" being "main" and the containing file being "main.java". What have you tried exactly? – tevemadar Jul 29 '18 at 11:13
  • You didn't tell how you compiled and ran. That's where the problem is. – JB Nizet Jul 29 '18 at 11:15
  • @MadhanM no, when I tried same thing on my pc it worked even when class name and program names were different.....the program was running perfectly on my PC in VSCODE without erros – Ayush Bali Jul 29 '18 at 11:18
  • (my class name )is the name i used for my class @tevemadar – Ayush Bali Jul 29 '18 at 11:20
  • 2
    A class name i Java can't contain parentheses or spaces. Post the **actual** code you used. Tell us what the **actual** file name is. How you **actually** compiled the class, and how you **actually** tried to execute it. – JB Nizet Jul 29 '18 at 11:20
  • I'm not sure why this question is marked as a duplicate, the link provided does not mention Visual Studio Code at all. I am using VSC and cannot debug because of this error, but can run things fine in command line. So really, answers would be more helpful if they considered VSC. My guess is it's a difference between debugging a Maven project vs. debugging a command line project. The folder structure and files are different (ex. Maven needs a pom.xml to run). – Azurespot Oct 11 '19 at 23:25

1 Answers1

3

Do you make sure the file name is the same as the class name?

  1. HelloWorld.java -> file name;
  2. HelloWorld -> class name;

A simple demo would be:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

In your case, if you have to use the word main as the class name. Then it's still fine.

You have two options to achieve that:

  1. make sure the file name is the same as the class name.
  2. or just remove the keyword public as follows:

    class main { public static void main(String[] args) { System.out.println("Hello world"); } }

Here is another thing I have to say:

Please follow Java naming convention:

  1. Class names should be UpperCamelCase;
  2. lowerCamelCase for variables, attributes, package, methods;

UPDATE

Why are the rules?

There is an answer.

Any class which is declared public should be kept in a file of the same name. It is a compilation error if the names of the public class and the file which contains it are different. A class which isn't declared public can be kept in a file of a different name.

Note that the generated class files are named after the java classes, not the file names.

Why the rules?

As for the design theory/purpose, here is a very comprehensive discussion.

But personally, I'd prefer this one by BlueRaja - Danny Pflughoeft:

It is just the convention set by Sun, the makers of Java. The purpose is organization; the reason is so that everyone who codes in Java will have a consistant way of naming files.

Hearen
  • 7,420
  • 4
  • 53
  • 63
  • I tried renaming the program same as the class name it worked ( java version on my laptop is 10.0.2 ), but when I code on PC I have my class name different than my program name and when I ran the program It worked ( java version on my PC is 1.8.0_161) why so? – Ayush Bali Jul 29 '18 at 11:37