我一直在netbeans和eclipse上都遇到這個問題,即使有一個簡單的文件,它顯示一個帶有jlabel的jframe。我的netbeans的項目屬性清楚地將tests2.hihi設置爲我的Main類,並且我已經清理並構建它,它會在我的dist文件夾中生成一個.jar文件。當我雙擊它時,它會提示我「找不到主類,程序將退出」。但是,如果我選擇從命令提示符「java -jar hello2.jar」運行它,它將正常運行!.jar文件一直給我「找不到主類」。程序將退出
這是.jar文件中的清單文件。
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_04-b20 (Oracle Corporation)
Class-Path:
X-COMMENT: src/hihi
Main-Class: testing2.hihi
package testing2;
public class hihi extends javax.swing.JFrame {
/**
* Creates new form hihi
*/
public hihi() {
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() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("hihi");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jLabel1)
.addContainerGap(334, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1)
.addContainerGap(253, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @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(hihi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(hihi.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 hihi().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
你的清單文件是什麼樣的?你指定一個主類嗎? –
聽起來像用於執行.jar文件的shell規則可能是borked。你在運行什麼操作系統? –
@Ted Hopp我正在運行windows vista.May我知道你指的是哪個清單文件?在.jar裏面或者在項目主文件夾裏面的那個(對不起,我仍然是這個的初學者)。無論如何,我正在使用netbeans,並且我已經指定主類爲testing2.hihi,在項目屬性的運行選項卡下。感謝你的幫助! – Laughy