tags:

views:

355

answers:

7

what is the best practice to create a .jar file from a java project??

+6  A: 

Some points to consider:

  • Create proper MANIFEST.MF, which should contain:
    • Build date;
    • Author;
    • Implementation-version;
  • Use a script/program - like an Ant task - which takes care of properly creating a manifest file.


Here's a simple ant file which builds a java project, adapted from the Ant tutorial:

<project>

<target name="clean">
    <delete dir="build"/>
</target>

<target name="compile">
    <mkdir dir="build/classes"/>
    <javac srcdir="src" destdir="build/classes"/>
</target>

<target name="jar">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
        <manifest>
            <attribute name="Implementation-Version" value="1.0"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

</project>
Robert Munteanu
I think newcomers should learn to use the real tools -- namely those provided by the JDK -- before using third party software like Ant. It's like using an IDE when learning a new language.
atc
Perhaps. But I see no mention of 'newcomers', just 'best practices'.
Robert Munteanu
It's quite obvious the person asking the question is probably not an expert.
atc
I answered the question. And for an 'expert' I would've pointed to maven.apache.org, BTW.
Robert Munteanu
I agree. @fenec asked for best practice. Scripting the build is repeatable and easily passed to others. Would recommend Maven to an expert but it's still a bit raw to be considered "best practice" for everyone.
Chris Nava
If Sun would just publish their ^%#^ jars in a public repository... Then Maven could be the best practice out of the box.
Chris Nava
+5  A: 

Read up on the basics of Sun's JAR format at this tutorial. In my opinion you should learn the basics -- namely MANIFESTs, the SDK tools -- before jumping into third party tools such as the following.

The Jar command line tool distributed with the Sun JDK:

jar -cf myjar.jar base_src_folder

Where base_src_folder is the parent directory holding your source code.

Alternatively you could use a build tool such as Apache Ant. It has features such as the JAR target to do this for you.

atc
+1  A: 

Apache Ant

MattChurchy
A: 

Ant is not quite easy to begin with. Instead, I would suggest using an IDE, for instance NetBeans, which creates an Ant script under the hood. All you have to do is to hit "build", and you get a .jar file as result.

Joonas Pulakka
I don't agree that its not easy to start using Ant, but it really comes handy with Netbeans. +1
Adeel Ansari
Ant's pretty damn easy to pick up...
atc
+1 on atc's comment. it didn't hard for me to learn Ant. I felt quite easy.
Veera
Well, anybody can copy tutorial scripts to get started, but at least I find it tricky to really understand and customize them. Hitting F11 is easier, but maybe it's just me :-)
Joonas Pulakka
+3  A: 

Apache Maven, for projects which are dependent on other projects.

Admittedly, Maven may be overkill for learning projects, but it is invaluable for larger, distributed ones.

Chadwick
+1  A: 

Eclipse IDE is the best way for new comers. Just select the project right click export File, select jar anf filename.

If your not already familiar enough with Eclipse to create a jar, it is a rather daunting interface, as noted in the "Worst UI" thread: http://stackoverflow.com/questions/238177/worst-ui-youve-ever-used/568142#568142
Chadwick
Using the GUI is about the worst practice for making a .jar file. It's not scripted to be repeatable by others AND you have little control over the contents.
Chris Nava
A: 

I'm a relative novice (though not newcomber) to java but I will say that for me this thread is missing the mark for jar "Best Practices". It's relatively pedantic to simply "create a jar". The questions I have around best practices are:

1) Is it OK to have numerous versions of a particular class in a jar file? My local utilities tend to consume numerous other local utilities ("set A"). Which, in turn, consume other utilities - some of which consume other "set A" utilities. I end up with duplicate entries in the jar.

2) Is there a particularly good use for "Implementation-Version"? E.g., is that what should be used to provide the --version for an app? If so, is there a good (or any way), perhaps using annotations (?) that allow the jar file version to be displayed from the app?

3) In order to minimize the number of classes I need in the batch wrapper (script that starts the jar), I unjar other jar libraries (e.g., apache commons CLI, etc) and include it in the object jar. Is this frowned on?

Thanks for any suggestions!

  • Andy
AndyJ