是否有一种单行工具或一些非常易于使用的工具来创建实用程序罐子?
我想向其他人展示如何从他们的代码创建 jar,以便他们可以与其他人共享。与此同时,我想让他们尽可能轻松地做到这一点。
最简单的方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的印象是 jar 实际上只是一个 .zip 文件。因此,您只需将类和清单放入正确的文件夹中,并使用 zip 等实用程序将它们压缩到最终的 .jar 文件中。
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.
jar
命令行工具怎么样?它最基本的形式非常简单。例如,您可以将类文件打包在一起,如下所示:另一个包含清单文件的示例:
您还可以添加一些其他标志和选项 - 请检查手册页以获取详细信息。
至于易于使用的工具,如果我没记错的话,大多数IDE(例如Eclipse)都有自己的内置JAR生成器。我自己发现这些非常容易使用。我猜你的里程可能会有所不同。
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:Another example with an included manifest file:
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.
转到您的类目录并使用以下命令创建一个 jar:
Go to your classes directory and create a jar using the command:
jar 命令非常容易使用(尽管不是很强大)。大多数大型项目都使用一些其他构建工具(ant、apb 等)。
为了轻松制作 jar,最好将所有类放在单独的目录中。您可以使用 javac -d来执行此操作
例如:
javac -d ./classes ./src/mypkg/*.java
将留下一个
classes
目录,其中包含所有那里有类文件。然后你可以使用 jar 命令来创建 jarfile:
jar cvf my-jar.jar ./classes
其中
cvf
部分代表“创建详细文件”,这意味着我们想要创建 jar 文件。我们希望 jar 工具详细说明它正在做什么,并且我们将为 jar 文件指定一个文件名(在我们的例子中为my-jar.jar
)。无论如何,学习创建 jar 文件(以及与 java 相关的任何内容)的最佳来源是 Java 教程
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 casemy-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 您的项目,然后只需键入:
要与其他人共享,将它们部署到企业存储库上(例如 Nexus)。通常,这将通过持续集成过程来完成。
Mavenize your project and then just type:
To share them with others, deploy them on a corporate repository (e.g. Nexus). Usually, this would be done by a continuous integration process.
如果您使用Eclipse 3.5,您可以开发您的程序,然后选择Export -> Export Java->可运行的罐子。
不同的选项主要处理程序使用的 jar 文件,它们最终都可以使用“java -jar ....”执行,而无需您自己处理 MANIFEST.MF。
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.