How to include the lib folder in the manifest classpath in Netbeans
Asked Answered
L

4

2

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.

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.

Hopefully it's possible to do this in the IDE. I include a snippet of the auto-generated build-impl.xml file.

<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.

Laevorotatory answered 9/11, 2009 at 17:29 Comment(0)
L
1

I found a way to acheive this modifying the 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.

Laevorotatory answered 13/11, 2009 at 20:54 Comment(2)
Perhaps you could build a custom ANT script outside of Netbeans so that it doesn't get overwritten.Penicillium
I might it nothing else works. I have no knowledge of Ant and don't have much time to pass on learning Ant. I thought my question would be a no brainer. :(Laevorotatory
N
3

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.

Here is an example of what I put in my build.xml file:

<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:

<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.

Neoteny answered 17/2, 2011 at 22:6 Comment(0)
L
1

I found a way to acheive this modifying the 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.

Laevorotatory answered 13/11, 2009 at 20:54 Comment(2)
Perhaps you could build a custom ANT script outside of Netbeans so that it doesn't get overwritten.Penicillium
I might it nothing else works. I have no knowledge of Ant and don't have much time to pass on learning Ant. I thought my question would be a no brainer. :(Laevorotatory
A
1

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).

Academicism answered 17/8, 2012 at 8:38 Comment(0)
H
0

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.

Hayes answered 10/11, 2009 at 14:46 Comment(4)
If I remove the globmapper instruction, then the Class-Path is pointing to the wrong folder (not lib/). Same thing if I just modify it. The jar files are correctly bundled. I want to add the lib/ folder to the Class-Path.Laevorotatory
Then just change the <path> builder to point at lib/ directly, so instead of ${run.classpath.without.build.classes.dir} use ${run.classpath.without.build.classes.dir}/libHayes
Changing the <path> adds a lib/lib in the classpath which is not correct either. I want everything (jar and xml) in the lib/. Also, any modification to the build-impl.xml will be lost next time Netbeans recreates it. (This seems random though)Laevorotatory
Then it means netbeans tries to be intelligent about its build process - which is always a source for problems when a software tries to be more intelligent then the developer ;-) . I'll play with Netbeans a bit and try to come up with a solution.Hayes

© 2022 - 2024 — McMap. All rights reserved.