No Main class found in NetBeans
Asked Answered
D

16

30

I have been working on an assignment for my class in programming. I am working with NetBeans. I finished my project and it worked fine. I am getting a message that says "No main class found" when I try to run it. Here is some of the code with the main:

package luisrp3;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class LuisRp3 {

public static void main(String[] args) throws FileNotFoundException  {

    java.io.File newFile = new java.io.File("LuisRamosp4.txt");

    if (newFile.exists()) {
        newFile.delete();
    }

    System.setOut(new PrintStream(newFile));

    Guitar guitar = new Guitar(); 

I posted this before but had a couple issues. i have fixed the others and now have just this one remaining. Any advice will be greatly appreciated.

Donndonna answered 16/12, 2013 at 0:49 Comment(9)
Do you have any other classes in the project aside from LuisRp3?Frustrate
How do you run your program?Shewmaker
I have a feeling the throws declaration might be confusing it. Try adding try/catch blocks instead of using the throws declaration and try again.Ice
I do, I have the Guitar class.Donndonna
@Shewmaker - I just hit the green play arrow.Donndonna
@LuisRamos: click inside your class LuisRp3 and press the green button again. Did it work?Poseidon
Select the tab LuisRp3 and press SHIFT+F6. Otherwise you need to define the main class in your project.Shewmaker
@LuisRamos there are a few solutions at this link. Perhaps they will help you at least isolate the problem: frickelblog.wordpress.com/2010/04/30/no-main-class-found Chances are it's just a bug in NetBeans. I ran into a similar issue in Eclipse, and had to restart it a few times to solve the issue.Ice
@JeroenVannevel: No same issue.Donndonna
K
40
  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. 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)

Karyotin answered 16/12, 2013 at 0:58 Comment(2)
I too have the same problem and followed your steps. But still the problem persists. I am able to run the project in my system but when I copy this dist folder to other computer and try to execute I am getting this error. What could be the problem?Mauceri
I had the same problem today. I ended up closing and reopening my project and it then found the main method. HTH.Nadiya
U
10

Also, for others out there with a slightly different problem where Netbeans will not find the class when you want when doing a browse from "main classes dialog window".

It could be that your main method does have the proper signature. In my case I forgot the args.

example: public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above.

Args: You can name the argument anything you want, but most programmers choose "args" or "argv".

Read more here: http://docs.oracle.com/javase/tutorial/getStarted/application/

Usia answered 23/9, 2014 at 20:46 Comment(0)
N
9

When creating a new project - Maven - Java application in Netbeans the IDE is not recognizing the Main class on 1st class entry. (in Step 8 below we see no classes).

When first a generic class is created and then the Main class is created Netbeans is registering the Main class and the app could be run and debugged.

Steps that worked for me:

  1. Create new project - Maven - Java application (project created: mytest; package created: com.me.test)
  2. Right-click package: com.me.test
  3. New > Java Class > Named it 'Whatever' you want
  4. Right-click package: com.me.test
  5. New > Java Main Class > named it: 'Main' (must be 'Main')
  6. Right click on Project mytest
  7. Click on Properties
  8. Click on Run > next to 'Main Class' text box: > Browse
  9. You should see: com.me.test.Main
  10. Select it and click "Select Main Class"

Hope this works for others as well.

Nerty answered 3/9, 2016 at 23:34 Comment(0)
M
3

The connections I made in preparing this for posting really cleared it up for me, once and for all. It's not completely obvious what goes in the Main Class: box until you see the connections. (Note that the class containing the main method need not necessarily be named Main but the main method can have no other name.)

enter image description here

Monegasque answered 29/5, 2020 at 19:2 Comment(0)
F
2

I had the same problem in Eclipse, so maybe what I did to resolve it can help you. In the project properties I had to set the launch configurations to the file that contains the main-method (I don't know why it wasn't set to the right file automatically).

Feculent answered 1/2, 2014 at 12:38 Comment(0)
X
2

In project properties, under the run tab, specify your main class. Moreover, To avoid this issue, you need to check "Create main class" during creating new project. Specifying main class in properties should always work, but if in some rare case it doesn't work, then the issue could be resolved by re-creating the project and not forgetting to check "Create main class" if it is unchecked.

Xanthene answered 3/1, 2016 at 16:19 Comment(0)
T
1

If that is all your code, you forgot to close the main method.

Everything else looks good to me.

public class LuisRp3 {

public static void main(String[] args) throws FileNotFoundException  {

    java.io.File newFile = new java.io.File("LuisRamosp4.txt");

    if (newFile.exists()) {
        newFile.delete();
    }

    System.setOut(new PrintStream(newFile));

    Guitar guitar = new Guitar(); 
}}

Try that.

Turki answered 16/12, 2013 at 0:53 Comment(2)
This is not the whole code. I am only posting a bit of it because it is a class assignment. Thank you though.Donndonna
I am working on the "try/catch" that was suggested. Thank you.Donndonna
H
1

If the advice to add the closing braces work, I suggest adding indentation to your code so every closing brace is on a spaced separately, i.e.:

public class LuisRp3 {

    public static void main(String[] args) throws FileNotFoundException  {

    // stuff

    }
}

This just helps with readability.

If, on the other hand, you just forgot to copy the closing braces in your code, or the above suggestion doesn't work: open up the configuration and see if you can manually set the main class. I'm afraid I haven't used NetBeans much, so I can't help you with where that option is. My best guess is under "Run Configuration", or something like that.

Edit: See peeskillet's answer if adding closing braces doesn't work.

Hollingshead answered 16/12, 2013 at 1:0 Comment(0)
P
1

There could be a couple of things going wrong in this situation (assuming that you had code after your example and didn't just leave your code unbracketed).

First off, if you are running your entire project and not just the current file, make sure your project is the main project and the main class of the project is set to the correct file.

Otherwise, I have seen classmates with their code being fine but they still had this same problem. Sometimes, in Netbeans, a simple fix is to:

  1. Copy your current code (or back it up in a different location)
  2. Delete your current file
  3. Create a new main class in your project (you can name it the old one)
  4. Paste your code back in

If this doesn't work then try to clear the Netbeans cache, and if all else fails, then just do a clean un-installation and re-installation of Netbeans.

Primitivism answered 16/12, 2013 at 1:6 Comment(0)
T
1

In the toolbar search for press the arrow and select Customize... It will open project properties.In the categories select RUN. Look for Main Class. Clear all the Main Class character and type your class name. Click on OK. And run again. The problem is solved.

Tameratamerlane answered 30/12, 2017 at 17:33 Comment(0)
A
1

I also experienced Netbeans complaining to me about "No main classes found". The issue was on a project I knew worked in the past, but failed when I tried it on another pc.

My specific failure reasons probably differ from the OP, but I'll still share what I learnt on the debugging journey, in-case these insights help anybody figure out their own unique issues relating to this topic.

What I learnt is that upon starting NetBeans, it should perform a step called "Scanning projects..." Scanning projects...

Prior to this phase, you should notice that any .java file you have with a main() method within it will show up in the 'Projects' pane with its icon looking like this (no arrow):

icon before scanning

After this scanning phase finishes, if a main() method was discovered within the file, that file's icon will change to this (with arrow):

icon after scanning

So on my system, it appeared this "Scanning projects..." step was failing, and instead would be stuck on an "Opening Projects" step.

I also noticed a little red icon in the bottom-right corner which hinted at the issue ailing me:

Unexpected exception

Unexpected Exception
java.lang.ExceptionInInitializerError

Clicking on that link showed me more details of the error:

java.security.NoSuchAlgorithmException: MD5 MessageDigest not available
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
    at java.security.Security.getImpl(Security.java:695)
    at java.security.MessageDigest.getInstance(MessageDigest.java:167)
    at org.apache.lucene.store.FSDirectory.<clinit>(FSDirectory.java:113)
Caused: java.lang.RuntimeException
    at org.apache.lucene.store.FSDirectory.<clinit>(FSDirectory.java:115)
Caused: java.lang.ExceptionInInitializerError
    at org.netbeans.modules.parsing.lucene.LuceneIndex$DirCache.createFSDirectory(LuceneIndex.java:839)

That mention of "java.security" reminded me that I had fiddled with this machine's "java.security" file (to be specific, I was performing Salvador Valencia's steps from this thread, but did it incorrectly and broke "java.security" in the process :))

Once I repaired the damage I caused to my "java.security" file, NetBeans' "Scanning projects..." step started to work again, the little green arrows appeared on my files once more and I no longer got that "No main classes found" issue.

Abominable answered 19/8, 2019 at 0:10 Comment(0)
P
0

You need to add }} to the end of your code.

Phantasm answered 16/12, 2013 at 0:56 Comment(0)
U
0

You need to rename your main class to Main, it cannot be anything else.

It does not matter how many files as packages and classes you create, you must name your main class Main.

That's all.

Unleavened answered 21/8, 2016 at 13:21 Comment(1)
There must be a main method in some class, but the name of the class containing main method may be named anything.Monegasque
A
0
import java.util.Scanner;
public class FarenheitToCelsius{
    public static void main(String[]args){
     Scanner input= new Scanner(System.in);
     System.out.println("Enter Degree in Farenheit:");
     double Farenheit=input.nextDouble();
     //convert farenheit to celsius
     double celsuis=(5.0/9)*(farenheit 32);
     system.out.println("Farenheit"+farenheit+"is"+celsius+"in celsius")
             {
Afrikaans answered 2/12, 2016 at 13:17 Comment(0)
C
0

Had the same problem after opening a project that I had downloaded in NetBeans. What worked for me is to right-click on the project in the Projects pane, then selecting Clean and Build from the drop-down menu. After doing that I ran the project and it worked.

Crete answered 14/9, 2021 at 5:49 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewSilurid
C
-2

Make sure the access modifier is public and not private. I keep having this problem and always that's my issue.

public static void main(String[] args)

Craquelure answered 29/2, 2016 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.