获得一个.jar内的目录 [英] Getting a directory inside a .jar

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

问题描述

我试图访​​问我的jar文件中的目录。
我想通过每一个目录本身里面的文件。我试过了,例如,使用以下内容:

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());
}

如果我测试这个小程序,它工作得很好。但是,一旦我把内容放入罐子,但它确实是因为几个原因并非如此。
如果我用这个code,该URL始终指向罐子外面,所以我必须把图片目录下有。
但是,如果使用新建文件(imagesDirectoryURL.toURI()); ,它不罐内工作,因为我得到的错误 URI不分层。我相信目录罐内存在。
我怎么一开始图片中的内容罐子里面呢?

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?

推荐答案

罐中的路径路径,而不是实际的目录,你可以在文件系统上使用。为了得到一个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:


  • 增益网​​址指着罐子。

  • 获得的InputStream 网​​址

  • 构造一个 ZipInputStream 的InputStream

  • 遍历每个的ZipEntry ,寻找到需要的路径相匹配。


  • 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?

ZipInputStream 不会松动资源中的文件系统上的目录工作。但后来,我强烈建议您使用一个构建工具,如Ant来构建(编译/罐/登录等)的小程序。这可能需要一个小时左右写的构建脚本和放大器;检查它,但此后你可以建立由几个按键项目,并几秒钟。

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.

这将是很烦人的,如果我总是要提取并签署我罐子,如果我想测试我Aplet

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

我不知道你的意思那里。在什么地方'提取'进入呢?在情况下,我并不清楚,一个沙箱的小程序可以加载资源这种方式,从在归档属性中提到的罐子。你可能会做的另一件事,是对资源的Jar(S)从applet罐分离。典型的资源比改变code少,所以构建也许能​​够采取一些捷径。

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.

如果你的意思是在服务器上,将有获得图像的列表文件短的从服务器上的帮助没有切实可行的办法。例如。有些服务器不安全设置来产生对于没有默认的文件(如一个index.html)任何目录基于HTML文件列表。

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.

确定 - 考虑移动的声音和放大器;图像到一个单独的罐子。或者至少,把它们与'没有COM pression'的罐子。虽然邮编comression与技术类工作得很好,他们是在COM pressing效率较低(否则已经融为一体pressed)的媒体格式。

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.

我有,因为我用preferences类来保存用户设置上签字。

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

有替代的 preferences 的小程序,如饼干。在插件2架构的小程序的情况下,可以使用的Java Web启动小程序(仍然嵌入在浏览器)启动。 JWS提供PersistenceService。这是我的小演示。的PersistenceService

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.

JWS的讲,这使我想到:你的绝对确定的这场比赛将是作为一个小应用程序,而不是一个应用程序更好的(例如,使用的JFrame )使用JWS启动?

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?

小程序会给你没有压力的结束,JWS还提供了 PersistenceService ,因为它是用Java 1.2。介绍

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天全站免登陆