简体   繁体   English

Java-获取当前JAR中的文件名列表

[英]Java - Get list of file names in the current JAR

I need to get the file names of each file that is in a particular folder inside my program's JAR while it is running. 运行程序时,我需要获取程序JAR内特定文件夹中每个文件的文件名。 Is it possible to do this? 是否有可能做到这一点? I'm not sure where to start. 我不确定从哪里开始。

It needs to be done programmatically and be platform independent. 它需要以编程方式完成并且与平台无关。

To list the contents of a jar file, simply run: 要列出jar文件的内容,只需运行:

$jar tf MyJar.jar

Are you looking to do this programmatically from within java? 您是否希望从Java中以编程方式执行此操作?

To do it programmatically, see this example. 要以编程方式执行此操作,请参见以下示例。

You are asking for something that isn't available in general. 您要的是一些通常不可用的东西。 Classes are loaded via ClassLoader instances, which may get class bytecode from many different places (network, jar file, .class files in a directory, dynamically generated). 类是通过ClassLoader实例加载的,该实例可以从许多不同的地方(网络,jar文件,目录中的.class文件,动态生成)获取类字节码。

The most you can know is the package hierarchy, which you can get from 您最了解的是包层次结构,可以从中获取

myObject.getClass().getClassLoader().getPackages()

which returns a list of packages available at the point of invocation. 它返回调用点可用的软件包列表。 For a given package you probably won't be able to tell where it came from. 对于给定的程序包,您可能无法分辨其来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM