应用程序如何在运行时浏览其软件包之一的内容? [英] How can an app walk through the contents of one of its packages at run time?

查看:47
本文介绍了应用程序如何在运行时浏览其软件包之一的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方法不适用:

Files.walk(Paths.get("folder name"))

因为当应用程序运行时(打包成jar形式),它不包含File对象.

Because when the app is running, packaged as a jar, it does not contain File objects.

有没有一种方法可以让应用在运行时浏览其中一个软件包的所有内容?

Is there a method for an app to walk through all the contents of one of its packages while it runs?

推荐答案

有;打开jar文件(使用 java.io.JarFile ),并逐步浏览 entries()返回的条目.如果您的jar是从其他地方流进来的,那么还有 JarInputStream .

There is; open the jar file (using java.io.JarFile) and walk through the entries returned by entries(). There's also JarInputStream if your jar is being streamed in from elsewhere.

但是,听起来您已经将'jar文件'等同于'我的应用程序,在运行时'.

But, it sounds like you have equated 'jar file' with 'my application, at runtime'.

这是一个棘手的动作.对于初学者来说,弄清楚自己的jar是可能的,但是有点hacky,更重要的是,这意味着您的应用程序除非以jar形式存在,否则就无法正常工作.这会使部署,开发和调试变得复杂.

That's a tricky move. For starters, figuring out your own jar is possible but a little hacky, and more importantly, it then means your app straight up fails to work unless it is in jar form. This complicates deployment, development, and debugging.

没有必要这样做.

您可以从当前正在拾取类文件的任何地方"向Java寻求资源.类文件是对应用程序运行至关重要的资源.例如GUI应用程序的图标文件.两者都应该来自同一个地方,那在哪里?这已经被抽象了,您应该遵循抽象,以便加载资源"代码在调试时的运行效果与在运行时一样好.

You can ask java for resources from 'whereever it is currently picking up class files'. A class file is a resource crucial to the running of your application. So is e.g. an icon file for a GUI app. Both should be coming from the same place, and where that is? That's abstracted away, and you should follow along with the abstraction, so that the 'load resource' code works just as well in debugging as it does at runtime.

用于此的系统是 MyClass.class.getResource("file.txt"),它将在同一位置 MyClass中查找 file.txt 即使位于jar文件中,.live 是实时生成的,还是从某个地方的数据库中的BLOB对象获得的,或者是通过网络流传输的..

The system for this is MyClass.class.getResource("file.txt") which will look for file.txt in the same place MyClass.class is located, even if it is in jar files, generated live, or obtained from a BLOB object in a database someplace, or streamed over a network.

缺点是,该系统没有具有用于列出文件的抽象.仅用于获取具有特定名称的资源.

The downside is, this system does not have an abstraction for listing files. Only for getting resources with a specific name.

解决方案是SPI系统:创建一个列出资源(每行一个资源)的文件-代替"list dir",而是解析CONTENTS.txt的每一行"以进行等效操作.然后,您可以根据需要使用注释处理器来自动创建和维护此内容文件.

The SPI system is the solution: Make a file that lists (one resource per line) the resources - instead of 'list dir', you 'parse each line of CONTENTS.txt' for the equivalent operation. You can then use annotation processors, if you must, to automatically create and maintain this content file.

ServiceLoader 包含在JDK本身中,但是它专门用于加载类文件而不是其他资源.但是该原理很简单,可以用大约5行代码手写.

ServiceLoader is baked into the JDK itself, but it's designed to load, specifically, class files and not other resources. But the principle is trivial and can be handwritten in about 5 lines of code.

这篇关于应用程序如何在运行时浏览其软件包之一的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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