java 运行 JAR 文件找不到主类。程序将会退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26351365/
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-11-02 09:46:33  来源:igfitidea点击:

Running JAR file could not find main class. Program will exit

javanetbeansjar

提问by Jerson Calinawan

I've been looking around the google for this very common problem but found no answer. When i build and clean my project through NetBeans, I only have this content in my manifest.mf

我一直在谷歌上寻找这个非常常见的问题,但没有找到答案。当我通过 NetBeans 构建和清理我的项目时,我的 manifest.mf 中只有这些内容

Manifest-Version: 1.0 X-COMMENT: Main-Class will be added automatically by build

清单版本:1.0 X-COMMENT:主类将通过构建自动添加

I was expecting of something like what others have:

我期待其他人拥有的东西:

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
Main-Class: dpsr.DPSR

..but I don't have it. I already have followed setting up my main class on the properties. and here's my main class by the way:

..但我没有。我已经按照属性设置了我的主类。顺便说一下,这是我的主要课程:

package dpsr;

import javax.swing.UIManager;


public class DPSR {

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e){
            System.out.println("Nimbus isn't available");
        }
        new frmLogin().setVisible(true);
    }
}

..when I run the command in the machine

..当我在机器上运行命令时

java -jar dpsr.jar

java -jar dpsr.jar

it only says

它只说

Could not find the main class: ... Program will exit.

找不到主类:...程序将退出。

I do not want to continue my project unless I resolve this. I need this so that other computers can use my project.

除非我解决了这个问题,否则我不想继续我的项目。我需要这个以便其他计算机可以使用我的项目。

回答by Paul Samsotha

Follow these instructions.

请遵循这些说明。

  1. Right click on Project, select [Properties].
  2. Select [Run] node on the left.
  3. In the [Main Class] test field, use the browsebutton to select the main class you want as the launching class. Press OK
  4. Clean an Build you project.
  5. Open the files window and look in YourProject/dist/yourproject.jar/META-INF/MANIFEST.MF. You should see the Main-Classhas been added/changed
  1. 右键单击项目,选择[属性]。
  2. 选择左侧的【运行】节点。
  3. 在 [Main Class] 测试字段中,使用browse按钮选择您想要作为启动类的主类。按OK
  4. 清理构建你的项目。
  5. 打开文件窗口并查看 YourProject/dist/yourproject.jar/META-INF/MANIFEST.MF。您应该看到Main-Class已添加/更改