开发者

Is there a one-liner or some extremely easy to use tool to create utility jars?

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 th开发者_C百科at?


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.


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.


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

jar cvf myapp.jar *


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜