0
votes

I wrote a very simple code (Hello World! ) by notepad, it compiled fine, but when I tired to ran it, I got a message saying Error:

Main method not found in class Test4, please define the main method as: public static void main (String[] args) or a JavaFX application class must extend javafx.application.Application.

when I use NeatBeans it's OK, but when using CM prompt the problem appears

class Test4 {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
2
What java version you are using. I tested in Java 8 the code works as it is.Karthikeyan Vaithilingam
I am using the latest java SE version No 12Hameed
Tested on Java "12.0.2" its working. how are you running the class. also what is the output of java -versionKarthikeyan Vaithilingam
java -version output is : java version "12.0.1" 2019-04-16 java (TM) SE Runtime Environment (Build 12.0.1+12) java HostSpot (TM) 64-Bit Server VM (build 12.0.1+12), mixed mode, sharing)Hameed

2 Answers

1
votes

Your class which contains the main method must be marked as public. This is the entry point. if you want to execute the main method in the non public class the only way to this is call that main from a main method of the public class.

public class Test4 {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
-1
votes

The usually structure of a basic java program is:

Documentation Section - You can write a comment in this section. Comments are beneficial for the programmer because they help them understand the code.

Package Statement - You can create a package with any name. A package is a group of classes that are defined by a name (only required if you are using packages).

Import Statements - This line indicates that if you want to use a class of another package, then you can do this by importing it directly into your program.

Interface Statement - Interfaces are like a class that includes a group of method declarations (optionally).

Class Definition - A Java program may contain several class definitions. Classes are the main and essential elements of any Java program.

Main Method Class - Every Java stand-alone program requires the main method as the starting point of the program. This is an essential part of a Java program. There may be many classes in a Java program, and only one class defines the main method. Methods contain data type declaration and executable statements.

//Name of this file will be "Test4.java"

public class Test4 {

    /* Author: Hameed
    Date: 28/10/2019
    Description:
    Writes the words "Hello World" on the screen */

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