在.jar中获取一个目录 [英] Getting a directory inside a .jar

查看:103
本文介绍了在.jar中获取一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问我的jar文件中的目录。
我想通过目录本身的每个文件。例如,我尝试使用以下内容:

  URL imagesDirectoryURL = getClass()。getClassLoader()。getResource(Images ); 

if(imagesFolderURL!= null)
{
文件imagesDirectory = new File(imagesDirectoryURL.getFile());
}

如果我测试这个小程序,它的效果很好。但是一旦我把内容放到罐子里面,这不是因为几个原因。
如果我使用这个代码,URL总是指向jar外面,所以我必须把 Images 目录放在那里。
但是如果我使用新文件(imagesDirectoryURL.toURI()); ,它在jar内不起作用,因为我收到错误 URI不分级。我确定该目录存在于jar内。
我应该如何在jar中获取图像的内容?

解决方案

Jars中的路径是路径,而不是实际的目录,可以在文件系统上使用它们。要获取Jar文件特定路径中的所有资源:




  • 获取 URL c code code code code code code code code code

  • InputStream 中构建一个 ZipInputStream

  • 迭代每个 ZipEntry ,寻找匹配到所需的路径。






..我仍然可以测试我的Applet,当它不在那个罐子里面?或者我要编制两种方式来获取我的图像?


ZipInputStream 不能在文件系统中的目录中使用松散的资源。但是,我强烈建议您使用Ant等构建工具来构建(编译/ jar / sign等)applet。编写脚本和编写脚本可能需要一个小时左右的时间。检查它,但之后您可以通过几次击键和几秒钟来构建项目。


如果我总是这样会很讨厌如果我想测试我的Aplet


我不知道你的意思是在那里提取并签名我的jar 提取物进入哪里?如果我不清楚,沙盒applet可以通过这种方式加载资源,从归档属性中提到的任何Jar。另一件可以做的事情是将资源Jar与小程序Jar分开。资源通常比代码更改更少,因此您的构建可能会采取一些快捷方式。


我想我真的要考虑把我的图像进入jar之外的一个独立的目录。


如果你的意思是在服务器上,将没有实际的方式来获取图像文件缺少从服务器的帮助。例如。某些服务器不安全地设置为任何没有默认文件(例如index.html)的目录生成一个基于HTML的文件列表。







我只有一个jar,我的类,图像和声音是。


OK - 考虑移动声音&图像成一个独立的罐子。或者至少把它们放在罐里,不要压缩。虽然Zip压缩技术在课堂上工作得很好,但它们在压缩(也就是已压缩)的媒体格式上效率较低。


我必须签名因为我使用首选项类来保存用户设置。


/ code>对于小程序,如cookie。在插件2架构小程序的情况下,您可以使用 Java Web Start 。JWS提供PersistenceService。这是我的小 PersistenceService的演示



说到JWS,这带给我:你绝对肯定吗>这个游戏会比applet更好,而不是使用JWS启动的应用程序(例如使用 JFrame



<小宝贝会给你没有压力的结束, nd JWS提供了 PersistenceService ,因为它是在Java 1.2中引入的。


I am trying to access a directory inside my jar file. I want to go through every of the files inside the directory itself. I tried, for example, using the following:

URL imagesDirectoryURL=getClass().getClassLoader().getResource("Images");

if(imagesFolderURL!=null)
{
    File imagesDirectory= new File(imagesDirectoryURL.getFile());
}

If I test this applet, it works well. But once I put the contents into the jar, it doesn't because of several reasons. If I use this code, the URL always points outside the jar, so I have to put the Images directory there. But if I use new File(imagesDirectoryURL.toURI());, it doesn't work inside the jar because I get the error URI not hierarchical. I am sure the directory exists inside the jar. How am I supposed the get the contents of Images inside the jar?

解决方案

Paths within Jars are paths, not actual directories as you can use them on a file system. To get all resources within a particular path of a Jar file:

  • Gain an URL pointing to the Jar.
  • Get an InputStream from the URL.
  • Construct a ZipInputStream from the InputStream.
  • Iterate each ZipEntry, looking for matches to the desired path.

..will I still be able to test my Applet when it's not inside that jar? Or will I have to program two ways to get my Images?

The ZipInputStream will not work with loose resources in directories on the file system. But then, I would strongly recommend using a build tool such as Ant to build (compile/jar/sign etc.) the applet. It might take an hour or so to write the build script & check it, but thereafter you can build the project by a few keystrokes and a couple of seconds.

It would be quite annoying if I always have to extract and sign my jar if I want to test my Aplet

I'm not sure what you mean there. Where does the 'extract' come into it? In case I was not clear, a sand-boxed applet can load resources this way, from any Jar that is mentioned in the archive attribute. Another thing you might do, is to separate the resource Jar(s) from the applet Jar. Resources typically change less than code, so your build might be able to take some shortcuts.

I think I really have to consider putting my Images into a seperate directory outside the jar.

If you mean on the server, there will be no practical way to get a listing of the image files short of help from the server. E.G. Some servers are insecurely set up to produce an HTML based 'file list' for any directory with no default file (such as an index.html).


I have only got one jar, in which my classes, images and sounds are.

OK - consider moving the sounds & images into a separate Jar. Or at the very least, put them in the Jar with 'no compression'. While Zip comression techniques work well with classes, they are less efficient at compressing (otherwise already compressed) media formats.

I have to sign it because I use the "Preferences" class to save user settings."

There are alternatives to the Preferences for applets, such as cookies. In the case of plug-in 2 architecture applet, you can launch the applet (still embedded in the browser) using Java Web Start. JWS offers the PersistenceService. Here is my small demo. of the PersistenceService.

Speaking of JWS, that brings me to: Are you absolutely certain this game would be better as an applet, rather than an app (e.g. using a JFrame) launched using JWS?

Applets will give you no end of stress, and JWS has offered the PersistenceService since it was introduced in Java 1.2.

这篇关于在.jar中获取一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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