我在netbeans和eclipse上遇到了这个问题,即使是一个显示带有jlabel的jframe的简单文件。我的netbeans的项目属性显然将testing2.hihi设置为我的Main类,我已经清理并构建它,在dist文件夹中生成.jar文件。当我双击它时,它会给我一条消息“无法找到主类。程序将退出。”但是,如果我选择从命令提示符“java -jar hello2.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
}
答案 0 :(得分:3)
我怀疑为Jar文件注册的open命令设置不正确。这是从命令行检查的方法(至少在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文件是否有效。
答案 1 :(得分:0)
请检查环境变量,JAVA_HOME,CLASS_PATH和PATH设置。您可以在cmd窗口中回显%JAVA_HOME%来检查这一点。确保你的设置是正确的。
答案 2 :(得分:0)
Ted Hopp的回答是正确的,但我会在不同的情况下改变一些事情。如果您已在系统环境变量“path”中设置了Java的路径,则可以将其放在命令提示符下:
assoc .jar = jarfile
ftype jarfile = javaw.exe -jar%1%*
'因为如果你放入整个JRE路径,每次更新JRE时都必须再次执行此操作。在这种情况下,您只需要更改环境变量,系统将完成剩下的工作。