Java Web Start 的非类文件
如何分发应用程序所需的不在 jar 文件中的其他文件?例如,位于 http://www.javabeginner.com/ 的应用程序java-swing/java-swing-shuffle-game 。下载内容包含 Shuffle.jar、Shuffle.bat、Score.dat 和一个包含 3 个图像的图像文件夹。我可以看到可能将图像直接放入 Shuffle.jar 中,但您不会希望将 Score.dat 放入 jar 文件中,因为它会发生变化。 jnlp 中是否有地方可以识别这种类型的文件?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非 java 文件应存储为资源。对于发生更改的文件,您还可以将原始文件或模板文件作为资源存储在 jar 中。当程序启动时,您让它检查本地系统以查看该文件是否存在。如果没有,它将通过从 JAR 资源复制模板文件来创建本地文件。如果该文件已存在,则按原样使用。
要将文件保存到本地系统,即使在沙箱(未签名)中运行时,您也可以使用
PersistenceService
(javadoc / 示例)。如果您的 java 应用程序已签名,则您可以使用常规文件 api 将文件写入本地计算机,例如用户主文件夹下的“.yourgame”子文件夹中。The non-java files should be stored as resources. For files that change, you store the original or template file also as a resource in your jar. When the program starts, you have it check the local system to see if that file exists. If not, it creates the local file by copying the template file from the JAR resource. If the file already exists, then it is used as is.
To save files to the local system, even when running in the sandbox (unsigned), you can use the
PersistenceService
(javadoc / example). If your java application is signed, then you can use the regular File apis to write the file to the local machine, such as in a ".yourgame" subfolder under the user's home folder.您可以将所有这些文件(除了分数文件)放入 jar 文件中,并使用资源加载来加载内容。
You can put all those files (except the scores file) in your jar file and load the contents using resource loading.
我刚刚删除并重新启动了两次回复,每次都更改我的答案;这很令人困惑,需要更多澄清。
您确定该应用程序应该是 Web Start 应用程序吗?在您链接到的网站上,它似乎不是。您是否试图将一个未设计为 Web Start 应用程序的应用程序更改为可以 Web Start 的应用程序?
如果它不是您的标签所暗示的 Web Start 应用程序,那么这个问题就是开放式的。您可以通过 100 种不同的方式分发它。
如果您确实想将其转换为 Web Start 应用程序,您可以首先将图像打包到 jar 中,如果您只是从那里读取它们而不是从 File() 读取它们,这将减轻您的第一个头痛。如果是 Web Start,那么您需要决定如何保存分数。在决定如何进行之前,您必须先决定评分系统是什么样的;所有分数都会保留在托管 Web Start 应用程序的网站上吗?那部分仍然是本地的吗?如果您想访问本地文件系统,则需要对 jar 进行签名,然后您可以将 Score.dat 提取到文件系统,并在最终用户接受的情况下对其执行任何您想要的操作。
在你做之前,你需要弄清楚你想做什么,或者如果你已经知道的比我们知道的多,至少要为我们弄清楚。
I've just deleted and restarted my reply twice now, changing my answer each time; this is confusing and needs a bit more clarification.
Are you SURE that application is supposed to be a Web Start app? On the site you linked to, it doesn't appear to be. Are you trying to take an application that was not designed as a Web Start application and change it into one that can be Web Start?
If it's not a Web Start app as your tag implies, then this question is open ended. You can distribute it 100 different ways.
If you are indeed trying to convert it into a Web Start app, you can start by packaging the images into the jar and that will alleviate your first headache if you just read them from there instead of from a File(). If it's going to be Web Start, then you need to decide how you want to keep scores. You have to decide what the scoring system is like before you can decide on how to go about it; will all the scores be kept on the web site hosting the Web Start app? Will that part still be local? If you want to get access to the local file system, you need to sign the jar, then you can extract the score.dat to the file system and do whatever you want with it if the end user accepts.
You need to figure out what you want to do before you can do it, or at least clear it up for us if you already know more than we know you know.