Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What's the deal with "Main" classes/methods...?

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Location
    UK
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's the deal with "Main" classes/methods...?

    Every tutorial I've followed so far gets me to start by creating (or simply automatically creates) a class called "Main", which is then referred to by the "public static void main(String args[])" line in that class.

    However, NONE of the completed projects I've downloaded from GitHub or other source-sharing sites contain a "Main" class. So, whenever I load them into IntelliJ and try to run them, I invariably get the following error message:

    Error: Could not find or load main class com.Willicious.Main
    Caused by: java.lang.ClassNotFoundException: com.Willicious.Main

    Anyone know what's going on here?

    Here's an example from a project I recently downloaded. The class containing the "public static void main(String args[])" line is called "Maze" (because it's a maze game):

    public static void main(String args[]){
    new MainMenu();
    }

    Why is it even trying to find a class called "Main" when such a class has not been specified or created within the code?

    This is the problem with trying to teach oneself, far too many unanswered questions

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's the deal with "Main" classes/methods...?

    I don't know where the convention of having a Main class came from. Personally I have no use for it. I put the main() method into the class that is called to start the program. I suppose that is the same as you saw for programs downloaded from GitHub.

    Perhaps the IDEs use Main as a default class name when starting the construction of a new java project. You should always be able to rename it to fit the project.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Entry level: 2 classes and Exception in thread "main"
    By y0zh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 1st, 2014, 01:36 PM
  2. Replies: 6
    Last Post: August 16th, 2014, 01:34 AM
  3. Replies: 26
    Last Post: February 10th, 2013, 12:59 PM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM