tags:

views:

91

answers:

6

I'd like to show other people how to create jars from their code, so they can share it with others. In the meantime, I'd like to make this as easy as possible for them.

What would be the easiest way of doing that?

+2  A: 

The impression I have is that jars are really just a .zip files. So you just need to put your classes and manifests into the right folders and use a utility like zip to zip them up into a final .jar file.

Paul Hsieh
+2  A: 

How about the jar command-line tool? It's pretty simple in its most basic form. For example, you could package class files together like:

jar cf archive.jar *.class

Another example with an included manifest file:

jar cmf ManifestFile archive.jar *.class

There are some other flags and options you can throw in—check the man pages for details.

As for easy-to-use tools, if I remember correctly, most IDEs (e.g. Eclipse) have their own built-in JAR generator. I've found these pretty easy to use, myself. Your mileage may vary, I guess.

htw
+1  A: 

Go to your classes directory and create a jar using the command:

jar cvf myapp.jar *
dogbane
+1  A: 

The jar command is quite easy to use (although not very powerful). Most large projects use some other build tools (ant, apb, etc.)

To make jars easily it is best if you put all your classes in a separate directory. You can do so using javac -d <directory> <source classes>

So for example:

javac -d ./classes ./src/mypkg/*.java

Will leave a classes directory with all the class files there.

Then you can use the jar command to create the jarfile:

jar cvf my-jar.jar ./classes

The cvf part stands for "Create Verbose File" which means we want to create the jar file. We want the jar tool to be verbose about what it's doing and that we will specify a file name for the jar file (in our case my-jar.jar).

In any case, the best source for learning about creating jar files (and about anything java related) is the The Java Tutorial

juancn
+1  A: 

Mavenize your project and then just type:

$ mvn package

To share them with others, deploy them on a corporate repository (e.g. Nexus). Usually, this would be done by a continuous integration process.

Pascal Thivent
+1  A: 

If you use Eclipse 3.5, you can develop your program, and then choose Export -> Java -> Runnable jar.

The different options mostly deal with jar-files used by your program, and they all end up with something you can execute with "java -jar ...." without you having to deal with MANIFEST.MF yourself.

Thorbjørn Ravn Andersen