首页 > 解决方案 > 无法构建jar项目

问题描述

我需要有关构建 jar 文件的帮助。我总是收到消息错误:尝试执行 jar 文件时无法找到或加载主类 Main。

这是我的项目结构

我的 MANIFEST.MF 在这里:

Manifest-Version: 1.0
Main-Class: Main

还有我的 Main.java 文件:

public class Main {    
public static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1;
public static final Charset UTF_8 = StandardCharsets.UTF_8;
public static void main(String[] args) throws IOException, InvalidFormatException {
for (String arg : args) {
        System.out.println("Arg " + arg);
    }
//Other code removed
}

在构建看不到 Main 类的 jar 文件时,我真的不知道自己做错了什么。

标签: javajar

解决方案


在我添加 env 变量后,它开始看到 main,但现在我又遇到了一次崩溃:

Error: A JNI error has occurred, please check your installation and try again
    Exception in thread "main" java.lang.NoClassDefFoundError: 
    org/apache/poi/openxml4j/exceptions/InvalidFormatException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
    Caused by: java.lang.ClassNotFoundException: 
    org.apache.poi.openxml4j.exceptions.InvalidFormatException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

看起来java在我的jar文件中看不到库文件。如果我使用 7zip 检查 jar 文件,我可以在其中看到库文件。 在此处输入图像描述


推荐阅读