Results 1 to 4 of 4
  1. #1
    bobg is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default How Does Java Find the "main" Method?

    In my third ever Java program I ran into the problem, discussed here before, that my "main" method was not found. I forget the exact error message but it was clearly complaining about not finding "main". The previous discussions did not help much but one suggested that there might be something magical about the name of the class containing the "main" method. So I fumbled about with that and finally got the program to execute without error when I changed the name of the class to "SwitchExperiment". What I guess is magical about that is that the name of the file containing the source is "SwitchExperiment.java". So my problem, apparently, was not that it couldn't find my "main" but that it couldn't find the class in which it expected to find "main". I hope that most of the error messages are more clear.

    If it matters I am using NetBeans IDE 7.0 on Windows Vista and running my program from the IDE (as opposed to a command line).

    What I am asking is: 1) am I right that the name of the class containing the "main" method is critical and must be the same as the name of the file containing it (minus the ".java" extension)?

    And 2) is there some similar magic involving the name of my package? I am using some code I found on the internet which has the statement "package pkgswitch.experiment". I have no idea at this point what the package statement does. I have it only because I copied some code from the Internet. But since it is included in code intended for beginners I assume it has some fundamental and important purpose and I am wondering if soon I will encounter another confusing error message because I have not used the right magical name for my package.

    Thanks for whatever help anyone can offer.

    Bob

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    All public classes must be in a .java file with the same name as the class name.
    public class AClass ... must be in AClass.java.
    Non public classes can be in any java file.
    The java command reads the class file it is given on the command line:
    java AClass ...
    and looks for the public static main(String[] args) method as the place to start execution.

    package names are a way to separate classes into name spaces. The path to the java and class files must match the package name. Java uses the classpath + the package name path to find classes. For your example the class files should be in the folder:
    pkgswitch\experiment\ and the pkgswitch folder should be in the folder at the end of the classpath.
    For your simple experiments you can remove the package statement and have the file in the current directory.

  3. #3
    pawdlk is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    What I am asking is: 1) am I right that the name of the class containing the "main" method is critical and must be the same as the name of the file containing it (minus the ".java" extension)?
    Yes, that's true.

    And 2) is there some similar magic involving the name of my package? I am using some code I found on the internet which has the statement "package pkgswitch.experiment". I have no idea at this point what the package statement does. I have it only because I copied some code from the Internet. But since it is included in code intended for beginners I assume it has some fundamental and important purpose and I am wondering if soon I will encounter another confusing error message because I have not used the right magical name for my package.
    package statement declares that all classes and interfaces defined in the source file are part of the the package. A package name should prevent collisions with other packages, so choosing a name that's both meaningful and unique is an important aspect of package design.

  4. #4
    SubZero is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    You may be referring to the extremely noobie question.

    If you're class is named correctly, as you stated before, maybe you are missing out on some code.


    class Displayer {

    -----------------public static void main(String args[]) {------------
    int x = ....
    int y = ....


    --------
    it is possible that you have made this mistake, I know I have many times.
    You simply don't enter your whole main method. You can write a whole script without having a main method and get a main method error. Usually the error appears as "not being able to find main"

    This is just my advice, thank you.

Similar Threads

  1. Receiving error "Could not find the main class"
    By Kalpesh in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 06:31 PM
  2. Error message "could not find the main class"
    By srwpchelp in forum New To Java
    Replies: 8
    Last Post: 12-23-2010, 09:15 PM
  3. Replies: 3
    Last Post: 08-25-2010, 10:21 AM
  4. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •