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.

Luis Ramos
  • 529
  • 1
  • 6
  • 13

16 Answers16

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)

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 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? – Pawan Feb 12 '16 at 06:21
  • I had the same problem today. I ended up closing and reopening my project and it then found the main method. HTH. – JavaJeff Aug 29 '17 at 15:25
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/

TriMix
  • 231
  • 3
  • 4
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.

Felix
  • 213
  • 2
  • 9
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

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
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).

Stefan
  • 368
  • 5
  • 10
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.

Asmat Ali
  • 335
  • 1
  • 11
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.

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
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.

Community
  • 1
  • 1
tamul
  • 103
  • 1
  • 8
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.

EpicDavi
  • 6,732
  • 3
  • 19
  • 20
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.

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.

Gurce
  • 592
  • 7
  • 18
0

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

cmd
  • 11,622
  • 7
  • 51
  • 61
Foster Hao
  • 132
  • 1
  • 6
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.

Andy-Delosdos
  • 3,560
  • 1
  • 12
  • 25
  • There must be a `main` method in some class, but the name of the class containing `main` method may be named anything. – DSlomer64 Feb 03 '22 at 05:10
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")
             {
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.

ofa
  • 21
  • 1
  • 5
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/29818252) – Rishabh Deep Singh Sep 14 '21 at 08:34
-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)