创建可以在任何机器上执行的jar文件
我有一个简单的 java 应用程序,可以打印“hello world!”在控制台上。它打包在app.jar 中。 罐子结构:
main/Hello.class - 我的带有单一 println 方法的主类
META-INF/MANIFEST.MF
清单文件包含以下内容:
Manifest-Version: 1.0
Main-Class: main.Hello
一切顺利。
但当你产生依赖性时,麻烦就开始了。我不确定,但认为在这种情况下你必须将所有库放入 jar 文件中。如果我将它们放在 META-INF/lib 中,我必须在清单中指定“Class-Path”。 “类路径”会是什么样子?
PS 有一些类似的问题,但我还没有找到合适的答案。
I have simple java app that prints `hello world!' on console. It is packed in app.jar.
Jar structure:
main/Hello.class - my main class with singe println method
META-INF/MANIFEST.MF
Manifest file contains following:
Manifest-Version: 1.0
Main-Class: main.Hello
Everything goes fine.
But when you have a dependency than troubles begin. I'm not sure but think in this case you have to put all libs to jar file. If I put them in META-INF/lib I must specify "Class-Path" in manifest. How "Class-Path" will look?
P.S There are some resembling questions but I haven't found appropriate answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我倾向于使用 ANT 构建脚本来打包我的应用程序和所有必需的 jar 文件。我发现一旦你让它正常工作,这会让生活变得更加轻松。
build.xml 文件看起来像这样:
请注意,如果您使用 Eclipse,您可以简单地执行 File / Export.../Runnable jar file,它会为您完成所有操作(包括生成 ANT build.xml)。
I tend to use an ANT build script to package my application and all necessary jar files. I find this makes life much easier once you've got it working properly.
build.xml file looks something like:
Note that if you use Eclipse, you can simplly do File / Export... / Runnable jar file and it will do everything for you (including generating the ANT build.xml).
如果您解压这些库并将它们集成到您的项目中,则无需指定任何特殊内容。如果这样做,您应该有一个“main”文件夹,如果您有 org.apache.foo 作为外部库,那么您还将在顶层有一个“org”文件夹。
You don't have to specify anything special if you unpack the libraries and integrate them into your project. If you do this, you should have a "main" folder, and if you have org.apache.foo as an external library, you'll also have an "org" folder at the top level.