Log4j 未添加到 Ant 中的类路径中
我正在尝试将 Log4j 添加到 Ant 中项目的类路径中,这会创建一个可执行 JAR,但似乎未正确添加它。
这是我的 Ant 构建脚本的 path
组件:
<path id="classpath.compile">
<fileset dir="${dir.myLibs}">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${dir.webContent}/WEB-INF/lib/log4j.jar" />
</path>
编译目标如下所示:
<target name="-compile">
<javac destdir="${dir.binaries}" source="1.6" target="1.6" debug="true" includeantruntime="false">
<src path="${dir.source}"/>
<classpath refid="classpath.compile"/>
</javac>
</target>
创建 JAR 的目标:
<target name="-createJar" >
<jar jarfile="${path.jarFile}"
manifest="${dir.source}\META-INF\MANIFEST.MF">
<fileset dir="${dir.binaries}" casesensitive="yes">
<exclude name="**/*.java"/>
</fileset>
</jar>
</target>
最后,MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: ../../../WebContent/WEB-INF/lib/log4j.jar (what is this pathing relative to?)
Main-Class: foo.Bar
JAR 已创建,但是当我执行它时,我得到:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger...
关于我做错了什么有什么想法吗?
I'm attempting to add Log4j to my project's classpath in Ant which creates an executable JAR, but it appears that it's not being added properly.
Here is the path
component of my Ant build script:
<path id="classpath.compile">
<fileset dir="${dir.myLibs}">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${dir.webContent}/WEB-INF/lib/log4j.jar" />
</path>
The compile target looks like this:
<target name="-compile">
<javac destdir="${dir.binaries}" source="1.6" target="1.6" debug="true" includeantruntime="false">
<src path="${dir.source}"/>
<classpath refid="classpath.compile"/>
</javac>
</target>
Tthe target that creates the JAR:
<target name="-createJar" >
<jar jarfile="${path.jarFile}"
manifest="${dir.source}\META-INF\MANIFEST.MF">
<fileset dir="${dir.binaries}" casesensitive="yes">
<exclude name="**/*.java"/>
</fileset>
</jar>
</target>
Lastly, the MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: ../../../WebContent/WEB-INF/lib/log4j.jar (what is this pathing relative to?)
Main-Class: foo.Bar
The JAR is created, but when I execute it, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger...
Any thoughts as to what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 MANIFEST 中的类路径看来,您正在尝试引用 jar 内的 jar。据我所知,实现这一点的唯一两种方法是 1) 一个特殊的类加载器,如 @infosec812 提到的,或 2) 通过将 jar 依赖项直接分解到 jar 的根目录中。两者都是可行的,但我没有看到它们出现在你的蚂蚁脚本中。
如果您尝试引用 jar 外部的 jar,则相对类路径是相对于您正在执行的 jar 的位置。确保引用的 jar 存在于该位置。
It looks from the classpath in your MANIFEST that you are trying to reference a jar inside your jar. The only two ways to make that work AFAIK are 1) a special classloader, like @infosec812 mentions, or 2) by exploding the jar dependencies directly into the root of your jar. Either is workable, but I don't see either of them happening in your ant script.
If you're trying to reference a jar outside of your jar, your relative classpath is relative to the location of the jar you are executing. Make sure the referenced jar exists in that location.
我猜测您正在运行 Java 程序,如下所示。
在这种情况下,您需要在清单中指定 Class-Path 属性。我建议您还查看 manifestclasspath 任务
I'm guessing that you're running the Java program as follows
In this case you'll need to specify the Class-Path attribute in the manifest. I suggest you also check out the manifestclasspath task
创建 jar 不包括 jar 中的链接库。您必须在执行类路径中包含所需的 jar 才能以这种方式运行它。或者,您可以使用我使用的解决方案,即创建一个 one-jar 存档。它将应用程序的专用类加载器添加到生成的 jar 中,并将所需的 jar 打包到最终的可执行 jar 中。它非常适合部署整洁、易于使用的包。
Creating the jar does not include the linked libraries in the jar. You would have to have the required jars in your execution classpath in order to run it that way. Or, you could use the solution I use, which is to create a one-jar archive. It adds a specialized class loader for your application into the resulting jar and also packages your required jars in to the final executable jar. It works really well for deploying neat, simple to use packages.