java 如何在 Netbeans 的清单类路径中包含 lib 文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1702511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include the lib folder in the manifest classpath in Netbeans
提问by FabienB
A library that my java application uses needs looks for a file (log4j.xml) in the class path. I use netbeans to manage my project, but I can't find a way to include the lib/ folder.
我的 java 应用程序使用的库需要在类路径中查找文件 (log4j.xml)。我使用 netbeans 来管理我的项目,但我找不到包含 lib/ 文件夹的方法。
Netbeans automatically creates a MANIFEST.MF file inside the application jar and also creates a folder called lib/ which includes all dependencies. This manifest specifies a Class-Path attribute that overrides any -cp argument provided on the command line. I can select an arbitrary folder in netbeans' library panel, but it creates a sub folder in the manifest's classpath. I'd like all dependencies and the log4j.xml file inside the lib/ folder.
Netbeans 会在应用程序 jar 中自动创建一个 MANIFEST.MF 文件,并且还会创建一个名为 lib/ 的文件夹,其中包含所有依赖项。此清单指定了一个 Class-Path 属性,该属性覆盖命令行上提供的任何 -cp 参数。我可以在 netbeans 的库面板中选择任意文件夹,但它会在清单的类路径中创建一个子文件夹。我想要 lib/ 文件夹中的所有依赖项和 log4j.xml 文件。
Hopefully it's possible to do this in the IDE. I include a snippet of the auto-generated build-impl.xml file.
希望可以在 IDE 中执行此操作。我包含了自动生成的 build-impl.xml 文件的片段。
<target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries">
<property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
<pathconvert property="run.classpath.without.build.classes.dir">
<path path="${run.classpath}"/>
<map from="${build.classes.dir.resolved}" to=""/>
</pathconvert>
<pathconvert pathsep=" " property="jar.classpath">
<path path="${run.classpath.without.build.classes.dir}"/>
<chainedmapper>
<flattenmapper/>
<globmapper from="*" to="lib/*"/>
</chainedmapper>
</pathconvert>
<taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/>
<copylibs compress="${jar.compress}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}">
<fileset dir="${build.classes.dir}"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
</copylibs>
<echo>To run this application from the command line without Ant, try:</echo>
<property location="${dist.jar}" name="dist.jar.resolved"/>
<echo>java -jar "${dist.jar.resolved}"</echo>
</target>
Thanks.
谢谢。
采纳答案by FabienB
I found a way to acheive this modifying the build-impl.xml.
我找到了一种方法来实现这个修改 build-impl.xml。
I changed:
我变了:
<attribute name="Class-Path" value="${jar.classpath}"/>
to:
到:
<attribute name="Class-Path" value="${jar.classpath} /lib"/>
The problem is that netbeans will overwrite it since this file is automatically generated.
问题是 netbeans 会覆盖它,因为这个文件是自动生成的。
回答by Derek
Instead of editing the build-impl.xml file you should add this entry to the build.xml file. When you modify anything in your project pertaining to the building of that project, it will generate a new build-impl.xml file.
您应该将此条目添加到 build.xml 文件中,而不是编辑 build-impl.xml 文件。当您修改项目中与该项目的构建有关的任何内容时,它将生成一个新的 build-impl.xml 文件。
Here is an example of what I put in my build.xml file:
这是我放入 build.xml 文件的示例:
<target depends="init" name="-do-clean">
<delete dir="${build.dir}"/>
<delete file="${dist.jar}"/>
<delete dir="${dist.dir}/lib"/>
<delete dir="${dist.dir}/resources"/>
</target>
Since I put this in the build.xml file, it will override the "-do-clean" section of the build-impl.xml file which contains:
因为我把它放在 build.xml 文件中,它将覆盖 build-impl.xml 文件的“-do-clean”部分,其中包含:
<target depends="init" name="-do-clean">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}" followsymlinks="false" includeemptydirs="true"/>
</target>
Furthermore, since it is in the build.xml it won't be modified by Netbeans.
此外,由于它在 build.xml 中,因此 Netbeans 不会对其进行修改。
回答by Vladimir
You can simply turn off project option Build/Packaging/Copy Dependent Library and manualy edit manifest.mf in root folder of your project (which is a template for manifest in jar file).
您可以简单地关闭项目选项 Build/Packaging/Copy Dependent Library 并手动编辑项目根文件夹中的 manifest.mf(它是 jar 文件中清单的模板)。
回答by Guss
It seems that your problem is the "globmapper" that stores your log4j.xml file in /lib - you'd want it on the "/" or the jar.
看来您的问题是将您的 log4j.xml 文件存储在 /lib 中的“globmapper”-您希望将其放在“/”或 jar 中。

