我有这个代码从目录中读取所有文件.
File textFolder = new File("text_directory");
File [] texFiles = textFolder.listFiles( new FileFilter() {
public boolean accept( File file ) {
return file.getName().endsWith(".txt");
}
});
Run Code Online (Sandbox Code Playgroud)
它很棒.它使用目录"text_directory"中以".txt"结尾的所有文件填充数组.
如何在 JAR文件中以类似的方式读取目录的内容?
所以我真正想做的是,列出我的JAR文件中的所有图像,所以我可以加载它们:
ImageIO.read(this.getClass().getResource("CompanyLogo.png"));
Run Code Online (Sandbox Code Playgroud)
(那个是有效的,因为"CompanyLogo"是"硬编码的",但JAR文件中的图像数量可以是10到200个可变长度.)
编辑
所以我想我的主要问题是:如何知道我的主类所在的JAR文件的名称?
当然,我可以使用它来阅读它java.util.Zip.
我的结构是这样的:
他们就像:
my.jar!/Main.class
my.jar!/Aux.class
my.jar!/Other.class
my.jar!/images/image01.png
my.jar!/images/image02a.png
my.jar!/images/imwge034.png
my.jar!/images/imagAe01q.png
my.jar!/META-INF/manifest
Run Code Online (Sandbox Code Playgroud)
现在我可以使用以下命令加载例如"images/image01.png":
ImageIO.read(this.getClass().getResource("images/image01.png));
Run Code Online (Sandbox Code Playgroud)
但只是因为我知道文件名,其余的我必须动态加载它们.
我正在尝试将我的spring应用程序从glassfish 4导出到JBoss wildfly 8.x或9 alpha,但是当我的应用程序在我的代码的某些部分启动时抛出异常:
Caused by: java.lang.RuntimeException: java.nio.file.FileSystemNotFoundException: Provider "vfs" not installed
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:218)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
... 3 more
Caused by: java.nio.file.FileSystemNotFoundException: Provider "vfs" not installed
at java.nio.file.Paths.get(Paths.java:147) [rt.jar:1.7.0_72]
at com.springmvcangular.backend.utils.entity.BaseEntityInitializer.extendsEntities(BaseEntityInitializer.java:123)
at com.springmvcangular.backend.utils.entity.BaseEntityInitializer.initializeBaseEntities(BaseEntityInitializer.java:88)
at com.springmvcangular.backend.config.ApplicationInitializer.onStartup(ApplicationInitializer.java:60)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:178)
... 7 more
Run Code Online (Sandbox Code Playgroud)
在我的班级BaseEntityInitializer中,我有以下例外情况:
packagepath = Paths.get(this.getClass().getClassLoader()
.getResource(path.replace('.', '/')).toURI());
Run Code Online (Sandbox Code Playgroud)
其中,path它的一个包路径一样com.something.model,为什么在我的GlassFish 4服务器这完美的作品和我需要在wildfly用呢?我不知道wildfly中缺少什么,或者我是否需要包含一些库.