Netbeans - class does not have a main method

后端 未结 12 2623
别跟我提以往
别跟我提以往 2020-12-06 14:28

My program is just a simple System.out.println(\"\"); But netbeans cannot find the main method. Is netbeans 6.7.1 conflict with WIN7? Any possible error?

相关标签:
12条回答
  • 2020-12-06 15:06

    While this may be an old question, the problem is still occurring these days, and the exact question is still not answered properly.

    It is important to note that some projects have multiple classes with a main method.

    In my case, I could run the project via the main class, but I could not run a particular other class that had a main method. The only thing that helped me was refactoring the class and renaming it. I've tried:

    • restart NetBeans
    • re-open the project
    • clear NetBeans cache
    • delete the file and create a new one with same name and contents
    • delete the file and create a new one with same name but very simple contents with only main method and print out message
    • rename the class (refactor) so a temp name and back
    • delete the project and create a new one with the same sources

    The only thing that let me run this class is renaming it permanently. I think this must be some kind of a NetBeans bug.

    Edit: Another thing that did help was completely uninstall Netbeans, wipe cache and any configuration files. It so happened that a newer Netbeans version was available so I installed it. But the old one would have probably worked too.

    0 讨论(0)
  • 2020-12-06 15:08

    This destroyed me for a while.... I knew that there HAD to be an easier way with a world class IDE like Netbeans.

    The easiest method is to press Shift+F11 (Clean and Build Project), then hit F6 to run it.

    It refreshes Netbeans appropriately and finds your main without all the manual labor; and if you have multiple mains, it will give you the option to select the correct one.

    0 讨论(0)
  • 2020-12-06 15:08
    1. Check for correct method declaration

    public static void main(String [ ] args)

    1. Check netbeans project properties in Run > main Class
    0 讨论(0)
  • 2020-12-06 15:10

    Make sure it is

    public static void main(String[] argv)
    

    No other signature will do.

    0 讨论(0)
  • 2020-12-06 15:16

    It was most likely that you capitalized 'm' in 'main' to 'Main'

    This happened to me this instant but I fixed it thanks to the various source code examples given by all those that responded. Thank you.

    0 讨论(0)
  • 2020-12-06 15:17

    My situation was different I believe because non of the above solutions di work for me. Let me share my situation.

    1. I am importing an existing project (NewProject->Java->Import Existing Projects)
    2. I name the project to xyz. The 'main' function exists in Main.class.
    3. I try to run the code I modified in the main function but the error pops out. I tried the shift_f6, specifically rebuild. Nothing works.

      Solution: I took the project properties and saw the 'Source Package Folder' mappings in the Sources branch was blank. I mapped it and voila it worked.

    Now anyone might think that was very silly of me. Although I am new to Java and Netbeans this is not the first time I am importing sample projects and I saw all of them had the properties similar. The only difference I saw was that the main class was not having the name as the project which I believe is a java convention. I am using JDK7u51 (latest till date), is it causing the issue? I have no idea. But I am happy the project is running fine now.

    0 讨论(0)
提交回复
热议问题