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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10875839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:56:25  来源:igfitidea点击:

.jar file keeps giving me " could not find the main class". Program will exit

javajarcompilation

提问by Laughy

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!

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

This is the manifest file inside the .jar file.

这是 .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
}

采纳答案by Ted Hopp

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):

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

  1. Enter the command: assoc .jar
  2. It should print .jar=jarfile. If it is not found (highly unlikely, given your symptoms), create the entry with the command assoc .jar=jarfile.
  3. Enter the command: ftype jarfile
  4. It should print something like
    "C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*
    (The path to javaw.exemight be different on your machine.)
  5. 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" %*
  1. 输入命令: assoc .jar
  2. 它应该打印.jar=jarfile. 如果未找到(极不可能,鉴于您的症状),请使用命令创建条目assoc .jar=jarfile
  3. 输入命令: ftype jarfile
  4. 它应该打印出类似的东西
    "C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*
    javaw.exe你的机器上的路径可能不同。)
  5. 如果未定义或打印错误值,请使用以下命令修复它:
    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.

您可能需要重新启动,或者至少打开一个新的 Windows 资源管理器窗口,以查看现在双击 .jar 文件是否有效。

回答by IvanGrasp

The Ted Hopp's answer it's correct but I would change somethings in different cases. If you already set Java's path in the system environment variable "path", you can put this on the command prompt:

Ted Hopp 的回答是正确的,但我会在不同的情况下改变一些东西。如果您已经在系统环境变量“path”中设置了 Java 的路径,则可以将其放在命令提示符中:

  1. assoc .jar=jarfile

  2. ftype jarfile=javaw.exe -jar %1 %*

  1. 关联 .jar=jarfile

  2. ftype jarfile=javaw.exe -jar %1 %*

'cause if you put the entire JRE path, you'll have to do it again every time you update JRE. In this case, you only need to change the environment variable and the system will do the rest.

因为如果您放置整个 JRE 路径,则每次更新 JRE 时都必须再次执行。在这种情况下,您只需要更改环境变量,系统会完成其余的工作。

回答by William

Please check you environment variables, your JAVA_HOME, CLASS_PATH and PATH setting. you could echo %JAVA_HOME%, in cmd window to check this. make sure you setting is correct.

请检查您的环境变量、您的 JAVA_HOME、CLASS_PATH 和 PATH 设置。您可以在 cmd 窗口中 echo %JAVA_HOME%, 以检查这一点。确保您的设置正确。