26

So I'm making a java application in Neatbeans 7.4, been working at it for a while, everything was fine, running the project worked fine, but now when I hit run project, I get the error

Error: Could not find or load main class phleveledit.MainWindow
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I can't think of what I did right before this started happening, so I don't know what I should change.. The code doesn't appear to have any errors. Here's a screenshot of the IDE+project folder

Image (http://puu.sh/5ldYB) :

enter image description here

Any ideas?

Edit: Unfortunately I happened to fix this problem by removing some code, which probably had some errors Netbeans couldn't detect, but I don't know what was exactly wrong so can't vote on a correct answer.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Matis Lepik
  • 1,140
  • 2
  • 9
  • 18
  • read the answer here, simple and it worked for me. http://stackoverflow.com/questions/18814602/error-could-not-find-or-load-main-class-netbeans-and-java – Rudy Duran Dec 04 '14 at 22:18
  • I came here for the same title but details were different, I was creating a simple hello world maven project in Netbeans. In my case I had added the main function in a class but the IDE kept giving the same error. What I did was a "Clean and Build" from the right click menu on the main project node in "Projects" pane. Then on running it asked me to choose from the main file which it detected successfully – shabby Jan 02 '19 at 09:43

23 Answers23

35
  1. Right click on your Project in the project explorer
  2. Click on properties
  3. Click on Run
  4. Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
  5. Click OK.
  6. Clean an build your project
  7. Run Project :)

If you just want to run the file, right click on the class from the package explorer, and click Run File, or (Alt + R, F), or (Shift + F6)

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
12

Just close the Netbeans. Go to C:\Users\YOUR_PC_NAME\AppData\Local\Netbeans and delete the Cache folder. The open the Netbeans again and run the project. It works like magic for me.

(AppData folder might be hidden probably, if so, you need to make it appear in Folder Options).enter image description here

Zin Win Htet
  • 2,448
  • 4
  • 32
  • 54
6

You can :

  • RightClick on project node and go to Set configuration
  • Select the main class for your application.
  • Then clean and build.

Even if the above steps don't work for you then then delete the Netbeans cache by deleting the (index) folder

User\.netbeans\SOME_NUMBER_reflecting_your_version\var\cache\index\
grepit
  • 21,260
  • 6
  • 105
  • 81
4

Sometimes due to out of memory space error, NetBeans does not load or find main class.

If you have tried setting the properties and still it is not working then try

  1. Select the project from the project explorer
  2. Click on Run in the Menu Bar
  3. Click on Compile

It worked for me.

InCh
  • 529
  • 1
  • 5
  • 18
4

This condition happens to me every 6-months or so. I think it happens when closing NetBeans under very low memory conditions. I discovered that it could be easily corrected by (1) Rename your project, including its folder name using right-click on project explorer's project name---I put a simple suffix on the original name ("_damaged"). (2) Try BUILD. If that is successful, which it is for me, give three cheers. (3) Repeat step (1) to restore the original project name. BUILD and RUN should start without trouble. I guess that the 'rename the project and folder' process causes a special rediscovery of the applications main location.

RF Moore
  • 101
  • 1
  • 4
4

Try to rename the package name and the class/jframe names... The clean and build the application.

  1. Right Click on the package name
  2. Go to Refactor
  3. Select Rename
  4. Give it a meaningful name, preferably all in small letters
  5. Click on Refactor

    Do the same for the class/jframe names.

  6. Last Select Run from Menu 7.Select Clean and build main project

That should do it!!! All best

3

I had the same issue once. The problem was not in the code. The cause was... renaming the project folder to some other non supporting name. My project name was "MobStick" and I renamed it to "MobStick - May 26, 2014 04:00PM". Renaming it back to normal solved my problem.

Rohit Dodle
  • 376
  • 4
  • 13
3

I have run into this error a couple of times as well and for me the above solutions did not work. What does seem to work is going to the Project Properties, and under Compiling toggling Compile on Save.

2

Using NetBeans 8.1, I got the dread

Error: Could not find or load main class 

from carelessly leaving an empty line in the Project Properties > Run > VM Options field. Until you click in the field, you may not see the caret flashing out of place. Remove the empty line to restore equanimity.

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I just ran into this problem. I was running my source from the command line and kept getting the same error. It turns out that I needed to remove the package name from my source code and then the command line compiler was happy.

The solutions above didn't work for me so maybe this will work for someone else with a similar problem.

Mary Martinez
  • 314
  • 5
  • 12
1

I had the same problem, I had the package and class named the same. I renamed the class, then clean and build. Then I set the main class in the "run" under the properties of the project. I works now.

bob
  • 11
  • 1
1

I found the following steps useful:

  1. Right-click on the project in the left toolbar.
  2. Hover over the 'Set Configuration' item.
  3. Click on 'Customize...'
  4. Click on 'Browse...' by the 'Main Class:' item.
  5. Select the correct class.
  6. Click 'Select Main Class'.
  7. Click 'OK'.

My problem was that, apparently, my package name was being listed twice. Selecting the class using the dialog changed 'aclass.MainClass' to just 'MainClass'.

Hope this helps,

-HewwoCraziness

Edit: This is expanding on Mary Martinez's answer.

Eric Reed
  • 377
  • 1
  • 6
  • 21
1

You can solve it in these steps

  1. Right-click on the project in the left toolbar.
  2. Click on properties.
  3. Click on Run
  4. Click the browse button on the right side.(select your main class)
  5. Click ok
Zong
  • 6,160
  • 5
  • 32
  • 46
bipin
  • 11
  • 1
1

I had the same problem for 3,4 days. On my PC my Jar file snapshot would give me this error while on my laptop it would work fine, I tried all the tricks shown above and on other forums like deleting cache, selecting main project file, etc, but somehow I was sure the reason it cannot find the main class when I would execute the JAR file was may be due to classpath issue in maven configuration, and I was right and I fixed it using following steps:

  1. Right-click on the project, and go to the properties
  2. Inside the properties go to Actions
  3. On the right side in Actions select "Run Project" and you will see properties below
  4. Inside "Set Properties" make sure exec.args=classpath %classpath "package_name"

In my case, the package name was accompanied by the main class. So my main class was Login while the package name was com.mycompany.islamic_center_app1, When I checked the entry was

com.mycompany.islamic_center_app1.Login

All I did was remove ".Login" from com.mycompany.islamic_center_app1 and it was fixed, no more errors.

enter image description here

0
  1. close netbeans.
  2. open netbeans again.
  3. choose new project>>java application.
  4. click next.
  5. deselect create main class.
  6. now make the application clean build run

For more reference watch this video

Nithin
  • 9,661
  • 14
  • 44
  • 67
Runni Kumari
  • 398
  • 3
  • 6
0

try this it work out for me perfectly go to project and right click on your java file at the right corner, go to properties, go to run, go to browse, and then select Main class. now you can run your program again.

0

I had the same problem and I moved the project to a location where the path had no none-english letter and that fixed the problem

Girasim
  • 11
0

if you are on window os, then try to start NetBeans via administrative mode. right click on NetBeans icon and "Run as Administrative".

0

If none of the above works (Setting Main class, Clean and Build, deleting the cache) and you have a Maven project, try:

mvn clean install

on the command line.

Sempfer
  • 171
  • 1
  • 4
0

Had the same problem here. Usually Clean and Build solves much of the problem. It happened to be caused by a wrongly installed plugin.

0

I faced the similar issue with Netbeans 10 and JDK 1.8. I was not able to choose the right class to launch the project When I compile or run the project, it shows me the Class name as "initializing view, please wait ...", I could not select the class name. The issue was resolved with the NetBeans11.3, I am able to choose the correct Class file without any other changes, and the project is launched without any issues.

digestit
  • 1
  • 1
0

Possible Fixes:

Fix 1

  1. Go to project properties (right click on the folder of your project in netbeans)
  2. On left tab where it shows the categories, click on the "Run" selection
  3. Then click on Browse to find the Main class you use on your project

Fix 2

  1. Go to C:\Users\name\AppData\Local\Netbeans
  2. delete the Cache folder.
  3. Rebuild and Run

Fix 3 Download most recent version of Netbeans

Fix 4 Download most recent version of JDK and configure Netbeans to use that

Alexandros Kourtis
  • 539
  • 2
  • 6
  • 20
  • 1
    Fix 2 has worked for me so far. Error occurred after updating from 11.3 to 12.6. –  Mar 07 '22 at 17:20
0

I had the same issue but none of this thread's solutions worked for me. Finally, it was OneDrive that caused the issue (for once more). So, I simply moved the NetBeansProjects folder from Documents which is synced with OneDrive, to C:\Users\yourName\AppData\Local\NetBeans (selected this path as there is already a NetBeans folder) and that was it, case closed.

If you also have NetBeansProjects to a OneDrive syncing folder it is worth trying this solution, just be sure that the path you will select is not synced with OneDrive. Also, remember to close Netbeans before making the folder change and after you move the folder to the new path you need just to open Netbeans again, go to file menu/open project and select your project from the new path.

George
  • 1
  • 1