我使用 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/