即使使用显示带有 jlabel 的 jframe 的简单文件,我在 netbeans 和 eclipse 上都遇到过这个问题。我的 netbeans 的项目属性清楚地将 testing2.hihi 设置为我的主类,我清理并构建了它,它在我的 dist 文件夹中生成了一个 .jar 文件。当我双击它时,它给我消息“找不到主类。程序将退出。”但是,如果我选择从命令提示符“java -jar hello2.jar”运行它,它将正常运行!
这是 .jar 文件中的 list 文件。
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 文件注册的打开命令设置不正确。这是一种从命令行检查的方法(至少在 Windows 7 上;我很确定它在 Windows Vista 上有效):
- 输入命令:
assoc .jar
- 它应该打印
.jar=jarfile
。如果未找到(考虑到您的症状,极不可能),请使用命令assoc .jar=jarfile
创建条目。 - 输入命令:
ftype jarfile
- 它应该打印类似
"C:\Program Files\Java\jre6\bin\javaw.exe"-jar "%1"%*
(javaw.exe
的路径在您的机器上可能不同。) - 如果未定义或打印出错误的值,请使用以下方法修复:
ftype jarfile="C:\Program Files\Java\jre6\bin\javaw.exe"-jar "%1"%*
您可能需要重新启动,或者至少打开一个新的 Windows 资源管理器窗口,以查看双击 .jar 文件现在是否有效。
关于java - .jar 文件一直给我 "could not find the main class"。程序将会退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875839/