3

NetBeans 12.0

Jdk 15

I cannot launch my program, clicking the green run button doesn't do anything, and right click + run file on MainFrame gives the error "Class does not have a main method". But as you can see in the GIF below, it does have a main(String args[]).

Does anyone have any idea why this happens?

enter image description here

enter image description here

enter image description here

SJ19
  • 1,933
  • 6
  • 35
  • 68
  • Two suggestions: (1) Make sure all files are saved first. If you try to run a program where the class containing the main method has not been saved, this can happen. Also (2) Right-click on the project name (in the Projects explorer), then select Properties > Run - and then make sure the main class is selected there. Again, if you have not saved the files NetBeans may not be able to find the main method (yet). – andrewJames Jan 04 '21 at 19:59
  • @andrewjames (1) just saved the file, didn't make a difference. (2) Mainclass says "cp.gui.MainFrame" which seems correct, however when I click Browse, there are no available main classes. (see new pictures in my post). Thanks for the tips! – SJ19 Jan 04 '21 at 20:53
  • 1
    The only other suggestion I have is the NetBeans equivalent of "_try switching it off and on again..._": Go to Help > About, and note the location of the cache directory. Exit from NetBeans. Delete the `Cache` directory. Restart NetBeans. – andrewJames Jan 04 '21 at 20:59
  • 1
    @andrewjames That fixed it! Thank you so much, you're a legend. Make an answer so I can mark this question resolved. :) – SJ19 Jan 04 '21 at 21:18
  • Regardless of how well things appear to be going with NetBeans12 and JDK 15, please be aware that NetBeans 12 does not support the use of JDK 15. See [Does Apache NetBeans 12.1 support JDK 15?](https://stackoverflow.com/q/63954870/2985643). Also see the documentation for NetBeans 12.1 which states [_"Apache NetBeans 12.1 runs on JDK LTS releases 8 and 11, as well as on JDK 14, i.e., the current JDK release at the time of this NetBeans release."_](https://netbeans.apache.org/download/nb121/nb121.html) – skomisa Apr 07 '21 at 20:13
  • Does this answer your question? [Does Apache NetBeans 12.1 support JDK 15?](https://stackoverflow.com/questions/63954870/does-apache-netbeans-12-1-support-jdk-15) – skomisa Apr 07 '21 at 20:24

2 Answers2

1

Initial suggestions:

  1. Make sure all files are saved first. If you try to run a program where the class containing the main method has not been saved, this can happen.

  2. Right-click on the project name (in the Projects explorer), then select Properties > Run - and then make sure the main class is selected there. Again, if you have not saved the files NetBeans may not be able to find the main method (yet).

It may also be worth checking whether you have any other classes in your project which also have a public static void main(String args[]) method.

If all else fails:

Clearing the NetBeans cache may help.

Go to Help > About, and note the location of the cache directory. Exit from NetBeans. Delete the Cache directory. Restart NetBeans.


WARNING

Please note what @skomisa points out in the comments:

NetBeans 12.0 does not support Java 15, and while it may work for a trivial project, there are still issues (as shown by the comments here). It would be reckless to use that combination for production code, and the IDE (as opposed to the compiler) may not handle new features in Java 15 appropriately.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • what's weird with netbeans is that my project is ant but the new > java ant from existing sources does not work anymore. i cant load it correctly since netbeans always adds a pom.xml thereby rendering my project not runnable. All classes with main method had the Run menu item disabled. – chitgoks Mar 20 '21 at 13:24
  • @chitgoks - I am not able to recreate that behavior when creating an Ant-based project from existing sources (I am using NetBeans12). If this is a sufficiently big problem for you, you can create a new question, and show the specific steps needed to recreate the issue (a [mre]). – andrewJames Mar 20 '21 at 14:13
  • i got it to work. but i had to restructure thw source folder to match maven to src/main/java. before it was just src and i had it working in previous netbeans version but 12.3 is different so i was forced to do this – chitgoks Mar 21 '21 at 15:06
  • 1
    NetBeans 12 does not support Java 15, and while it may work for a trivial project, there are still issues (as shown by the comments here). It would be reckless to use that combination for production code, and the IDE (as opposed to the compiler) may not handle new features in Java 15 appropriately. – skomisa Apr 07 '21 at 20:22
  • Your warning is well taken @skomisa. Do you know if (or how) this may have changed in 12.2 and 12.3? I understand they are not a long-term support releases - but I do see some [references](http://netbeans.apache.org/download/nb122/index.html) to JDK 15 support - although nothing which says "12.2 or 12.3 supports JDK 15". I guess I should be equally as cautious as I am (or, as I will be, from now on) with 12.0? – andrewJames Apr 07 '21 at 20:56
  • 1
    @andrewjames My previous comment was invalid, or at best highly misleading. Here's the deal: the latest JDK supported for NB 12.0 and NB 12.1 is **JDK 14**, but for NB 12.2 and 12.3 it is **JDK 15**. This is formally stated in the _"Deployment Platforms"_ section of each release document, which has a title of the form _"Downloading Apache NetBeans 12.x"_. For example, this is the [release documentation for _"Downloading Apache NetBeans 12.3"_](https://netbeans.apache.org/download/nb123/nb123.html). – skomisa Apr 09 '21 at 07:26
  • @andrewjames Presumably, if NetBeans 12.4 is released in the next few months it will support JDK 16, or even JDK 17 if that is the current version of Java at the time. Also, you may want to update your answer, since the information you quoted from me was incorrect/incomplete. Sorry about that, and thanks for raising this issue. – skomisa Apr 09 '21 at 07:30
1

Here is what worked for me on Netbeans 12.3 JDK 11:

  1. Right Click Project -> Properties
  2. In the Sources Tab, click Add Folder under Source Package Folders. Choose src.
  3. Press Ok.

It appears Netbeans re-scans the project folder, and the main class is found.

Don Su
  • 129
  • 9