简体   繁体   中英

Exception in thread “main” java.lang.Error: Unresolved compilation problem: Program.main

I'm learning how to code java, and it is my first programming language, and I have a problem with the run and debug,

    public class Program {
        public static void main (String[] args) {
            String firstName, lastName;
            firstName = "David";
            lastName = "Williams";
            System.out.println("My name is " + firstName +" "+lastName);
        }
    }

I'm doing solo learning and when I write a simple task and try too run VScode says me, Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Program.main(class operazioni {.java:2)

Class operazioni is the title of the file. Can someone explain me why I cant run and debug?

Change your file name to Program.java

In Java, the java file name should be always the same as a public class name.

  1. While writing a java program first it is saved as a ".java" file, when it is compiled it forms byte code which is a ".class" file as such that if we made our program file similar to the class it will be comfortable for us to understand without any ambiguity. We are allowed to use any name for a filename only when class is not public. In the case of a public class, we can't use a different file name.

  2. The filename must have the same name as the public class name in that file, which is the way to tell the JVM that this is an entry point.

  3. Suppose when we create a program in which more than one class resides and after compiling a java source file, it will generate the same number of the.class file as classes reside in our program. In this condition, we will not able to easily identify which class need to interpret by java interpreter and which class containing Entry point for the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM