从 eclipse 和可运行的 jar 中读取资源

问题描述 投票:0回答:1

我正在尝试从可运行的 jar 中读取目录中的文件,但以下代码仅在 eclipse 中有效,但是,它在使用 java 命令的运行中的 jar 中不起作用


import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
public class mmm {
    
public static void main(String[] args) throws IOException, URISyntaxException {
        mmm mmm1=new mmm();
        ClassLoader classLoader = mmm1.getClass().getClassLoader();
         String locationPattern = "classpath*:/cc/ff/rr/tttt**";  // tttt is a resource folder contains 2 files nn.txt ,ss.txt 
         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver( );
         //Resource[] resources = resolver.getResources(locationPattern);
         Resource[] resources = resolver.getResources(locationPattern);  ======> 
         System.out.println("Number of files "+resources.length);
         for (Resource resource : resources) {
        System.out.println("file :"+resource.getFilename());
             
}```

in java commande line i'm getting the follwing error  

'''
java.io.FileNotFoundException: URL [rsrc:cc/ff/rr/] cannot be resolved to absolute file path because it does not reside in the file system: rsrc:cc/ff/rr/
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:204)
        at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
        at org.springframework.core.io.UrlResource.getFile(UrlResource.java:168)
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:528)
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:349)
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:267)
        at cc.ff.rr.mmm.main(mmm.java:36)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)'''

Please, what is the issue with the code?
java spring classloader executable-jar
1个回答
0
投票

尝试使用以下代码使用 Java 读取可运行的 JAR 文件

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ReadDirectoryFromJar {
    public static void main(String[] args) throws IOException {
        String directoryName = "/directoryName"; // Replace with the name of the directory you want to read
        InputStream inputStream = ReadDirectoryFromJar.class.getResourceAsStream(directoryName);
        Files.walk(Paths.get(inputStream))
             .filter(Files::isRegularFile)
             .forEach(System.out::println);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.