制作一个单jar的java应用程序 [英] making a single-jar java application

查看:111
本文介绍了制作一个单jar的java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个由几种不同类型的文件(图片,声音等)和多个jar依赖项组成的java项目,那么将它们全部打包到一个可以双击的jar中的好方法是什么?

If I have a java project that consists of several different types of files (pictures, sound, etc) and multiple jar dependencies, what is a good way to package it all into one single jar that can just be double-clicked?

我知道罐子本身非常愚蠢,因为他们不会把自己所依赖的文件放在自己内部(这是我在一丝沮丧之后才意识到的事情)轻描淡写))。 - 如果jar A依赖于jar B中包含的类,那么将jar B放入jar A中是行不通的。 Jar A必须和jar B在同一个目录中。

I know jars by themselves are pretty dumb in that they don't look within themselves for files that they depend on (something I only realized after the slightest bit of frustration (understatement)). -- If jar A depends on classes contained inside jar B, putting jar B inside jar A will not work. Jar A must be in the same directory as jar B.

...现在,我知道我可以从所有其他jar中提取所有文件并将所有文件放入同一目录。那有点工作,但我不想这样做,因为:1。它会很混乱2.它仍然无法解决需要将声音文件放入与最终jar相同的目录。 (无论出于何种原因,声音文件的行为与内部jar完全相同)

...Now, I know I could just extract all the files from all the other jars and put everything in the same directory. That would sort of work, but I don't want to do it because: 1. It would be messy and 2. it still wouldn't solve the problem of needing to have the sound files in the same directory as the final jar. (For whatever reason, sound files act the exact same way as internal jars)

基本上,我只是想让它应用程序所依赖的文件不是令人讨厌的可见和炫耀。所以,如果有一些解决方案,我可以将所有内容放在一个jar中并让它成为运行整个程序的唯一必要文件,那将是最佳的。但是,我愿意接受创造性/创造性的方法来绕过这个问题,比如让父目录中的批处理脚本执行jar或其他东西。 (我说或者其他什么因为那个确切的场景只适用于Windows操作系统....你知道我的意思!)

Basically, I just want to make it so that the files my application depends on aren't obnoxiously visible and ostentatious. So, if there were some solution where I could put everything inside a jar and have it be the only necessary file to run the entire program, that would be optimal. But, I am willing to accept creative/inventive ways to bypass the problem, such as having a batch script in a parent directory execute the jar or something. (I say "or something" because that exact scenario would only work on windows operating systems. ...you know what I mean!)

推荐答案

您可以提取所有JAR并将它们合并到一个公共JAR中。有ANT任务和Maven插件可以随时使用。此外,如果您的应用程序已正确编写,则无法阻止您将媒体文件和其他资源放入JAR中。您只需要确保这些资源是从类路径加载,而不是从当前工作目录加载。

You can extract all the JARs and merge them into one common JAR. There are ANT tasks and Maven plugins readily available for doing just this. Additionally, if you're application is properly written, there is nothing preventing you from putting media files and other resources in the JAR as well. You just need to ensure that these resources are "loaded from the classpath", rather than being loaded from the current working directory.

这篇关于制作一个单jar的java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆