java - 构建文件中“找不到主类错误”

标签 java apache ant

我使用 Ant 构建文件创建了一个 Java 应用程序,该文件包含从应用程序生成 jar 文件的 jar-task 任务。

<target name="jar-task" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="jar/guix.jar" basedir="${bin.dir}">
        <fileset dir="${basedir}">
            <include name="${basedir}/images/**/" />
            </fileset>
            <manifest>
                <attribute name="Main-Class" value="IO.Deep.clk.GUI"/>
                <attribute name="Class-Path" value="${basedir}/SPLASH-2.0.0.jar ${basedir}/lib/dist/* ${basedir}/user.properties"/>
            </manifest>
        <filelist dir="${basedir}" files="user.properties"/>
    </jar>
</target>

当我在命令行上执行时,NoClassDefFoundError 指出

Could not find the main class IO.Deep.clk.GUI. Program will exit.

现在,GUI 文件完全位于特定文件夹中且位于同一个包中,我真的不明白错误可能出在哪里......任何人都可以帮忙吗?

最佳答案

名称images建议 jar 文件仅包含图像。但实际的代码在哪里?

    <fileset dir="${basedir}">
        <include name="${basedir}/images/**/" />
    </fileset>

这首曲子

    <attribute name="Class-Path" value="${basedir}/SPLASH-2.0.0.jar ${basedir}/lib/dist/* ${basedir}/user.properties"/>

会将完整的限定路径名写入 list 中。确定这是正确的吗?另请注意,该代码将找不到 user.properties通过将文件放在类路径上。类路径只能包含将在其中搜索类或其他内容的目录或 jar 文件。但简单的文件不起作用。

我也不确定 lib/*部分。是类(class)IO.Deep.clk.GUI在该目录中的 jar 文件之一中?这将是一个提示,所有目录都必须明确列出。如果这不是问题 - 很好。

编辑: 可以通过添加任务manifestclasspath来避免类路径问题。 ( Ant docs ) 在调用 jar 之前并使用 manifest 内生成的类路径值属性。概要:

    <manifestclasspath property="jar.class.path" jarfile="jar/guix.jar">
        <classpath>
            <fileset dir="." includes="*.jar" />
            <fileset dir="." includes="lib/*.jar" />
        </classpath>
    </manifestclasspath>
    <echo message="Class-Path will be: ${jar.class.path}" />
    <jar ....>
        ....
        <attribute name="Class-Path" value="${jar.class.path}" />

关于java - 构建文件中“找不到主类错误”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7693240/

相关文章:

java - 使用 httppost 将文件上传到 apache 服务器

java - Gradle 中的 Sun JPEGImageEncoder 编译

android - 如何在 Android 测试项目(ant 构建)中引用库项目

java - 如何使用 Spring WebClient 禁用 cookie

java - 使用 HyperlinkListener 从 JEditorPane 获取 URL

java - 我正在尝试在 Android studio 中创建一个简单的保存/加载应用程序,但出现空对象引用错误(下面是完整错误)

java - 如何在 JEditorPane 中读取 HTML 文件?

php - 是什么杀死了这个进程以及如何阻止它这样做

ant - 如何使用Ant任务启动和停止jboss服务器?

PHP exec(python.py),写入文件的权限被拒绝