使用内容分发Java程序 [英] Distributing a Java program with content

查看:122
本文介绍了使用内容分发Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几个月里,我一直在为一个大学项目开发​​java游戏。它即将到达项目的最后阶段,我想将项目编译成一个易于分发的文件。游戏当前从IDE内部运行,并依赖于特定地设置的工作目录(即具有声音/纹理的内容目录等)。将所有这些放在一起以便携带的最佳方法是什么?我希望有一些方法可以将内容编译到jar文件中...

For the past couple of months I've been working on a game in java for a university project. It's coming up to the end of the project and I would like to compile the project into a single file which is easy to distribute. The game currently runs from inside the IDE and relies on the working directory being set somewhere specific (i.e. the content directory with sounds/textures etc). What's the best way to put all this together for portability? I'm hoping there is some way to compile the content into the jar file...

NB。我正在使用NetBeans,因此任何易于使用netbeans的解决方案都会获得额外的功劳;)

NB. I'm using NetBeans, so any solutions which are easy in netbeans get extra credit ;)

附录::

为了将来的参考,我发现了一种按目录访问东西的方法,这可能不是最好的方法,但它可以工作:

For future reference, I found a way of accessing things by directory, ythis may not be the best way but it works:

File directory = new File(ClassLoader.getSystemResource("fullprototypeone/Content/Levels/").toURI());

现在我可以正常使用该文件对象

And now I can just use that file object as normal

推荐答案

你可以在jar文件中嵌入资源 - 毕竟这只是一个拉链。执行此操作的标准方法是将资源文件放在源层中的某个目录中。然后由Object.getClass()。getResourceAsStream()引用它们。因此,您需要更改在代码中检索它们的方式。

You can embed resources in jar file - this is just a zip after all. The standard way to do that is to put resource files in some directory in your sources hierachy. Then you refer to them by Object.getClass().getResourceAsStream(). So you will need to change the way you retrieve them in your code.

您可以在此处阅读更多内容: Object.getClass()。getResourceAsStream()。当然不是对象,而是使用你的包中的一些类。

You can read more here: Object.getClass().getResourceAsStream(). Of course instead of object you use some class from your package.

当你把这些资源文件放在你的src层次结构中时我相信Netbeans应该用标准版本为你打包它们该项目。

when you put those resource files in your src hierachy I believe Netbeans should jar them for you with standard build of the project.

这篇关于使用内容分发Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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