No main class found in netbeans java

No Main class found in NetBeans

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.

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.

16 Answers 16

  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 )

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?

I had the same problem today. I ended up closing and reopening my project and it then found the main method. HTH.

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».

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).

Читайте также:  Bootstrap css cdn https

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.

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

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).

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.

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.:

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.

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).

Читайте также:  Создать свою cms php

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.

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.

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.

Scanning projects.

What I learnt is that upon starting NetBeans, it should perform a step called «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.(FSDirectory.java:113) Caused: java.lang.RuntimeException at org.apache.lucene.store.FSDirectory.(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 :))

Читайте также:  Setting icon in html

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.

Источник

No main class found in netbeans java

Find centralized, trusted content and collaborate around the technologies you use most.

Connect and share knowledge within a single location that is structured and easy to search.

Followed an oracle tutorial on creating a GUI, the tutorial promised that upon running the application I will have to select the main class, however, Netbeans doesn’t find any Main classes? Here is my code:

public class GUI extends javax.swing.JFrame < /** * Creates new form GUI */ public GUI() < initComponents(); >/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() < jTextField1 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Bookshop"); jTextField1.setText("Enter Title"); jTextField1.setName(""); // NOI18N jTextField1.addActionListener(new java.awt.event.ActionListener() < public void actionPerformed(java.awt.event.ActionEvent evt) < jTextField1ActionPerformed(evt); >>); jLabel1.setText("Book Title"); jButton1.setText("Submit"); jButton1.addActionListener(new java.awt.event.ActionListener() < public void actionPerformed(java.awt.event.ActionEvent evt) < jButton1ActionPerformed(evt); >>); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jLabel1) .addContainerGap(244, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE) .addComponent(jButton1) .addGap(228, 228, 228)) ); pack(); >// private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) < // TODO add your handling code here: >private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) < // TODO add your handling code here: //Parse degrees Celsius as a double and convert to Fahrenheit. String Title = (String)(jTextField1.getText()); jLabel1.setText("Selected Title:" + Title); >/** * @param args the command line arguments */ public static void main(String args[]) < /* Set the Nimbus look and feel */ ///* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try < for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) < if ("Nimbus".equals(info.getName())) < javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; >> > catch (ClassNotFoundException ex) < java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); >catch (InstantiationException ex) < java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); >catch (IllegalAccessException ex) < java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); >catch (javax.swing.UnsupportedLookAndFeelException ex) < java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); >// /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() < public void run() < new GUI().setVisible(true); >>); > // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JTextField jTextField1; // End of variables declaration > 

I thought that «public static void main(String args[])

Источник

Оцените статью