.jar文件不断给我“找不到主类”。程序将会退出

【字号: 日期:2024-02-23浏览:48作者:雯心
如何解决.jar文件不断给我“找不到主类”。程序将会退出?

我怀疑为Jar文件注册的open命令未正确设置。这是一种从命令行检查的方法(至少在Windows 7上;我很确定它在Windows Vista上可以运行):

输入命令: assoc .jar它应该打印.jar=jarfile。如果未找到(鉴于您的症状,则不太可能),请使用命令创建条目assoc .jar=jarfile。输入命令: ftype jarfile

它应该打印类似'C:Program FilesJavajre6binjavaw.exe' -jar '%1' %*(机器上的路径javaw.exe可能不同。)

如果未定义或输出错误的值,请使用以下方法进行修复:ftype jarfile='C:Program FilesJavajre6binjavaw.exe' -jar '%1' %*

您可能必须重新启动,或至少打开一个新的Windows资源管理器窗口,以查看双击.jar文件是否现在可以正常工作。

解决方法

即使使用显示jframe并带有jlabel的简单文件,我在netbeans和eclipse上都遇到了问题。我的netbeans的项目属性显然将test2.hihi设置为我的Main类,并且我已经清理并构建了它,并在dist文件夹中生成了一个.jar文件。当我双击它时,它给我消息“找不到主类。程序将退出。”但是,如果我选择从命令提示符“ java -jar hello2.jar”运行它,它将正常运行!

这是.jar文件中的清单文件。

Manifest-Version: 1.0Ant-Version: Apache Ant 1.8.3Created-By: 1.7.0_04-b20 (Oracle Corporation)Class-Path: X-COMMENT: src/hihiMain-Class: testing2.hihipackage 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,ex); } catch (IllegalAccessException ex) {java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE,ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) {java.util.logging.Logger.getLogger(hihi.class.getName()).log(java.util.logging.Level.SEVERE,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 modifyprivate javax.swing.JLabel jLabel1;// End of variables declaration}

相关文章: