fenec
fenec

Reputation: 5806

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

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

Upvotes: 3

Views: 2939

Answers (6)

Alex
Alex

Reputation: 8313

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 task to do this for you.

Upvotes: 7

Robert Munteanu
Robert Munteanu

Reputation: 68328

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>

Upvotes: 11

Hitesh Agarwal
Hitesh Agarwal

Reputation:

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

Upvotes: 2

Chadwick
Chadwick

Reputation: 12673

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.

Upvotes: 5

Joonas Pulakka
Joonas Pulakka

Reputation: 36577

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.

Upvotes: 0

Matt
Matt

Reputation: 983

Apache Ant

Upvotes: 1

Related Questions