java 从eclipse和一个runable jar阅读资源

sbtkgmzw  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(94)

我试图从可运行的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?
nvbavucw

nvbavucw1#

尝试使用下面的代码来使用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);
    }
}

相关问题