我正在尝试为一个使用 hibernate 的项目编译一个可运行的 jar 文件。我正在尝试构建一个 ant build.xml 文件来简化我的构建过程,但我在将 hibernate3.jar 包含在最终 jar 文件中时遇到了麻烦。
如果我运行 ant 脚本,我设法包含我所有的库 jar,并将它们放在最终 jar 文件的根目录中。当我运行 jar 文件时,我得到一个
java.lang.NoClassDefFoundError: org/hibernate/Session
错误。如果我在 Eclipse 中使用内置的 jar 导出,只有当我选择“将所需的库提取到 jar 中”时它才会起作用。但这会使 jar 膨胀,并且包含太多我的项目(即单元测试)。
下面是我生成的 list :
Manifest-Version: 1.0
Main-Class: main.ServerImpl
Class-Path: ./ antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar
hibernate3.jar javassist-3.9.0.GA.jar jta-1.1.jar slf4j-api-1.5.11.jar
slf4j-simple-1.5.11.jar mysql-connector-java-5.1.12-bin.jar rmiio-2.0.2.jar
commons-logging-1.1.1.jar
build.xml 的部分如下所示:
<target name="dist" depends="compile" description="Generates the Distribution Jar(s)">
<mkdir dir="${dist.dir}" />
<jar destfile="${dist.dir}/${dist.file.name}.jar" basedir="${build.prod.dir}" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="${main.class}" />
<attribute name="Class-Path" value="./ ${manifest.classpath} " />
<attribute name="Implementation-Title" value="${app.name}" />
<attribute name="Implementation-Version" value="${app.version}" />
<attribute name="Implementation-Vendor" value="${app.vendor}" />
</manifest>
<zipfileset refid="hibernatefiles" />
<zipfileset refid="slf4jfiles" />
<zipfileset refid="mysqlfiles" />
<zipfileset refid="commonsloggingfiles" />
<zipfileset refid="rmiiofiles" />
</jar>
</target>
zip 文件集的 refids 指向项目根目录下的库目录 lib 中的目录。 manifest.classpath-variable 获取所有这些库 jar 文件的类路径,并使用 pathconvert 和 mapper 将它们扁平化。
我还尝试将 list 类路径设置为“.”、“./”并且仅设置库 jar,但没有任何区别。我希望有一个简单的方法可以解决我的问题...
最佳答案
因为你不能在你的类路径中指定 jar-inside-jar,这种方式是行不通的。 您需要仅对 您的 代码进行 jar,然后使用 libs jar 压缩您的 jar 并启动脚本,例如
#!/bin/sh
java -cp ./hibernate.jar:./mycode.jar... my.Main
(这是大多数人所做的),或者您可以将所有 lib jar 解压缩到包含您的已编译类的同一目录,然后将 jar 结果目录返回到单个 jar 文件(这就是 maven jar -with-dependencies 打包做)。
关于java - 在 ant build 中包含 hibernate jar 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2690143/