.jar文件不断给我“找不到主类“。程序将退出 [英] .jar file keeps giving me " could not find the main class". Program will exit
问题描述
我一直在这两个netbeans和eclipse的问题,即使一个简单的文件,显示一个jframe的jlabel。我的netbeans的项目属性清楚设置testing2.hihi作为我的主类,我有干净,并生成它在我的dist文件夹中产生一个.jar文件。当我双击它,它给我的消息找不到主类,程序将退出。但是,如果我选择从命令提示符java -jar hello2.jar运行它,它会正常运行!
这是.jar文件文件。
Manifest-Version:1.0
Ant版本:Apache Ant 1.8.3
Created- By:1.7.0_04-b20(Oracle Corporation)
类路径:
X-COMMENT:src / hihi
主类:testing2.hihi
b
$ b package testing2;
public class hihi extends javax.swing.JFrame {
/ **
*创建新表单hihi
* /
public hihi (){
initComponents();
}
/ **
*此方法从构造函数中调用以初始化表单。
*警告:请勿修改此代码。此方法的内容总是
*由表单编辑器重新生成。
* /
@SuppressWarnings(unchecked)
//< editor-fold defaultstate =collapseddesc =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参数命令行参数
* /
public static void main(String args []){
/ *
*设置Nimbus的外观和感觉
* /
//< editor-fold defaultstate =collapseddesc =观感设置代码(可选)>
/ *
*如果Nimbus(在Java SE 6中引入)不可用,保持
*默认的外观和感觉。详情请参阅
* 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 .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>
/ *
*创建并显示表单
* /
java.awt.EventQueue.invokeLater(new Runnable(){
public void run(){
new hihi()。setVisible(true);
}
});
}
//变量声明 - 不要修改
private javax.swing.JLabel jLabel1;
//变量声明结束
}
我怀疑为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文件现在工作。
I have been having problems with this on both netbeans and eclipse even with a simple file that displays a jframe with a jlabel on it. My netbeans's project properties clearly sets testing2.hihi as my Main class and I have clean and build it which produces a .jar file in my dist folder. When I double click on it, it gives me the message" could not find the main class. Program will exit." However, if I choose to run it from the command prompt "java -jar hello2.jar" it will run as normal!
This is the manifest file inside the .jar file.
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
}
I suspect that the open command registered for Jar files is not set up correctly. Here's a way to check from the command line (at least on Windows 7; I'm pretty sure it works on Windows Vista):
- Enter the command:
assoc .jar
- It should print
.jar=jarfile
. If it is not found (highly unlikely, given your symptoms), create the entry with the commandassoc .jar=jarfile
. - Enter the command:
ftype jarfile
- It should print something like
"C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*
(The path tojavaw.exe
might be different on your machine.) - If it isn't defined or prints the wrong value, fix it with:
ftype jarfile="C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*
You may have to reboot, or at least open a new Windows Explorer window, to see if double-clicking on the .jar file now works.
这篇关于.jar文件不断给我“找不到主类“。程序将退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!