HOWTO Create MANIFEST.MF Classpath From Ant

UPDATE It seems this is at least partly outdated, have a look at manifestclasspath.

It is possible to define .jar dependencies in the MANIFEST.MF of the main .jar file, it is just a comma seperated string which looks like this:

In a project I work on, we keep all our dependencies in a lib directory, where we add and remove libraries from time to time, therefore it would be nice if the above text can be generated automatically. Fortunately, this is possible using just Ant. Unfortunately, this is rather cumbersome. (If you want something more powerful and simpler to use, have a look at Rake).

In a nutshell, this is how I did it:

  1. First, let’s define some properties:
  2. Now to the fun part, create a nice .jar file. This generates the MANIFEST.MF but first generates a property libs.project that contains all .jar files, sperated with space. If you later look at the generated MANIFEST.MF you will notice that the paths are wrapped every 72 character, even in the middle of the word. Although this looks like an error, it works correctly.
  3. Finally we need to create a distribution by copying all .jar files into dist/lib while using a flat directory structure:

I use Ant 1.6.2, and this works perfectly for me. Have fun!

27 Comments on "HOWTO Create MANIFEST.MF Classpath From Ant"

Notify of
avatar

trackback

[…] 1 votes […]

trackback

[…] Tapi gimana klo di ant ? Wach puyeng nech ( Setelah tanya-tanya ama pakde google akhirnya dpt link ini: http://martin.ankerl.org/2005/11/30/howto-create-manifestmf-classpath-from-ant/ […]

brian liddle
Guest
brian liddle
8 years 5 months ago

Is the ClassPath defining the physical directory where the libs must exist? i.e. if ${libs.project} is defined as “c:/devl/workspace/libs”, how will this work for anyone else but the developer that wrote the code?

Martin Ankerl
Guest
8 years 5 months ago

brian, it is a relative path, like

So you just need to copy the generated .jar and the lib directory and everything works.

martin
Guest
martin
8 years 3 months ago

fantastic! you rule!

Michele Silva
Guest
Michele Silva
7 years 11 months ago

Great! Finally a solution that really works. Not complicated at all 😉

RIF
Guest
7 years 9 months ago

Hey everybody

Why not just use the core Ant task: “manifestclasspath”.

It’s in the AntManual

http://ant.apache.org/manual/index.html

--dnajam
Guest
--dnajam
7 years 8 months ago

Hi dear Martin Ankerl ,
I have tried your sample code its writing the Manifest.mf file correctly. but while invoking any class file from an external jar, for example StringUtils from apache common-lang. it says java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils.
However, commons-lang-2.4.jar is available within the folder name lib at the root of the jar archive as shown in above example. And the Manifest.MF also have an entry similar to following
Class-Path: lib/commons-lang-2.4.jar

Please help
regards
–danishnajam

Dan S
Guest
Dan S
7 years 2 months ago
Thank you, thank you, thank you. This was the answer to several hours of pain. I tried using but couldn’t figure out how to make it build relative paths. (I’m an Ant newbie and couldn’t figure how to pipe the flattenmapper result to the globmapper…chainedmapper was the missing link) I suspect dnajam’s errors are due to attempting to load classes from a jar within his project jar. If I understand correctly, that requires a custom classloader. The solution presented here is for referencing external jars placed in a relative path below where the project jar is.
Reynolds
Guest
Reynolds
7 years 26 days ago

Muchas gracias por la publicacion del script justo lo que necesitaba para generar el jar de mi proyecto

Martin Ankerl
Guest
7 years 26 days ago

de nada 🙂

Dimitri Koami Missoh
Guest
6 years 11 months ago

Thanks a lot for sharing your experience. Your blog entry helps me a lot when I get stuck today trying to make my deployment run.

Dimitri Missoh.

Bernhard Kabelka
Guest
Bernhard Kabelka
6 years 11 months ago

Thanks Martin for saving my day! When I was facing this problem today at my new job, I remembered our project and your nifty Ant script. However, I already feared the worst, as I have never really taken a closer look on the Ant script. Can you imagine my joy on finding this blog entry? 🙂

Martin Ankerl
Guest
6 years 11 months ago

Hi Bernhard! Service is our success 😉

Venkat
Guest
Venkat
6 years 9 months ago

Awesome!! It’s perfect and working great. Thanks a lot, Martin.

Amit
Guest
Amit
6 years 4 months ago

Great Help through this. Thanks!

Bruce
Guest
Bruce
6 years 1 month ago

My “Class-Path” in the generated manifest.mf file is truncating my classpath to 80 bytes using:

Results:

lass-Path: commons-beanutils.jar commons-collections-3.2.1.jar common
s-collections-3.2.jar commons-collections.jar commons-digester-1.8.ja
r commons-digester.jar commons-lang-2.1.jar commons-logging-1.1.1.jar
commons-logging.jar commons-pool-1.3.jar derby-10.2.2.0.jar el-api-1
.0.jar el-impl-1.0.jar geronimo-jpa_3.0_spec-1.0.jar geronimo-jta_1.1
_spec-1.1.jar jhighlight-1.0.jar jsf-api-1.2_04-p02.jar jsf-api.jar j
sf-facelets.jar jstl.jar openjpa-1.2.0.jar portlet.jar richfaces-api-
3.3.2.SR1.jar richfaces-impl-3.3.2.SR1.jar serp-1.13.1.jar servlet-ap
i-2.4.jar

Senthil
Guest
Senthil
6 years 1 month ago

Hello friends,

I need to convert my java application into an executable jar. My app have some third party jar files. In manifest file, I have set classpath also in relative path only as given above. But when I run my executable jar in my machine, its working fine. If I run in some other system, I am getting classNotFoundException, the class which is present inside my third party jar. Can anyone knows what s de reason and how to solve this problem ?

thanks,
Senthil.M

Jaap
Guest
Jaap
5 years 8 months ago

It worked like a charm! Thank you: I was starting to get cranky again as I am getting more used to Python which is much easier with this library management and stuff.

Edgard
Guest
5 years 7 months ago

Thank you very much, Martin!

This is exactly what I was looking for.
You’ve just saved my day !

Thank you so much.

Edgard

Gabriele
Guest
Gabriele
4 years 11 months ago

Thanks!

Gabriele

John Kelly
Guest
4 years 2 months ago

Very useful, good example, and well explained – thanks !

srinivasa
Guest
srinivasa
3 years 11 months ago

Thanks a lot for the help

Ahmed Yehia
Guest
3 years 9 months ago

Hi Martin , when i used the out put jar file in the classpath of my project , it didn’t refer to the dependencies that gathered in the jar file.
Could u please give me advice ?
thanks for your help

MartinAnkerl
Guest
MartinAnkerl
3 years 9 months ago

is the manifest correct?

Tom Hallman
Guest
Tom Hallman
3 years 29 days ago

THANK YOU!!! I’ve been banging my head against this problem for several hours… this worked perfectly!!

Ayyaz Shaukat
Guest
Ayyaz Shaukat
2 years 5 months ago

Goood I got idea from this topic and its solved my issue
Thanks a lot

wpDiscuz