Java <未找到主要类>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20841972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<No main classes found>
提问by user2403162
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?
遵循有关创建 GUI 的 oracle 教程,该教程承诺在运行应用程序时我必须选择主类,但是,Netbeans 没有找到任何 Main 类?
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
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();
}// </editor-fold>
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 */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* 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);
}
//</editor-fold>
/* 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[]) {" declared a main class. The class GUI.java is in the source package so I really don't understand.
我认为“public static void main(String args[]) {”声明了一个主类。类 GUI.java 在源包中,所以我真的不明白。
Please note I'm very new to Java, and really appreciate your time.
请注意,我对 Java 非常陌生,非常感谢您的时间。
回答by Tim B
Your understanding is correct, and I can't see anything obviously wrong with your main class.
你的理解是正确的,我看不出你的主班有什么明显的错误。
Although having said that:
虽然说过:
public static void main(String args[]) {
should be:
应该:
public static void main(String[] args) {
If you right click on the Java file in your project window and select "run this file" or "debug this file" that will tell netbeans to execute that file ignoring the project settings.
如果您右键单击项目窗口中的 Java 文件并选择“运行此文件”或“调试此文件”,这将告诉 netbeans 执行该文件而忽略项目设置。
If that works then go in the properties of your project and set the main class in there for future use.
如果可行,则进入项目的属性并在其中设置主类以备将来使用。
Your best bet to track this down will be divide an conquer. Create a new file, create an empty main()
method in it that just prints "hello world", then transfer your code across a section at a time until you find the thing that breaks it.
追踪此问题的最佳选择将是分而治之。创建一个新文件,main()
在其中创建一个只打印“hello world”的空方法,然后一次跨一个部分传输您的代码,直到找到破坏它的东西。
回答by Paul Samsotha
- Right click on your Project in the project explorer
- Click on properties
- Click on Run
- 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)
- Click OK.
- Right-click on your project; click 'Clean and Build`
- Run Project
- 在项目资源管理器中右键单击您的项目
- 点击属性
- 点击运行
- 确保您的主类是您想要作为入口点的类。(确保使用完全限定名称,即 mypackage.MyClass)
- 单击确定。
- 右键单击您的项目;单击“清理并构建”
- 运行项目
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)
如果您只想运行该文件,请在包资源管理器中右键单击该类,然后单击“运行文件”,或 ( Alt+ R, F) 或 ( Shift+ F6)
The mistake I've made before is when I create a new Project, I forget to unclick the "Create Main Class` check box and so it create a Class for me, which I don't like so I delete the class. In this case, I would have to do the above steps to fix the problem so it will run correctly. The Main class NetBeans creates for you, is the launching class. If you delete it, you need to manually specify the new launching class for the project, then clean and build.
我之前犯的错误是当我创建一个新项目时,我忘记取消单击“创建主类”复选框,因此它为我创建了一个类,我不喜欢它,所以我删除了该类。在这个这种情况下,我必须按照上面的步骤来解决问题,这样它才能正常运行。NetBeans 为您创建的 Main 类是启动类。如果删除它,则需要为项目手动指定新的启动类,然后清理并构建。