简体   繁体   中英

How to find specific file from the classpath in java

I have a project structure like below:

在此处输入图像描述

Now, I want to pass a specific keyword-based (which will help to find properties file in the project) on which I should directly look for that file in classpath and get it. Below is my code:

public class WithoutSubfolders {
    public static void main(String[] ar) throws Exception {
        WithoutSubfolders t = new WithoutSubfolders();
        String inputString = "Encounter";
        File path = t.getFileFromURL(inputString);
        HashMap< String, String> hMap = new HashMap< String, String>();
        File[] listOfFiles = path.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            System.out.println(listOfFiles[i]);
        }
    }

    private File getFileFromURL(String keyword) {

        URL url = this.getClass().getClassLoader().getResource(keyword+".*");
        File file = null;
        try {
            file = new File(url.toURI());
        } catch (Exception e) {
            file = new File(url.getPath());
        } finally {
            return file;
        }
    }  

When I run this code, I get Null Pointer Exception for below line:

File[] listOfFiles = path.listFiles();

Also, I don't want to pass any extensions for the file. Something like .* should work. So, where am I going wrong?

Your problem will be a lot more obvious if you add some temporary debug output for path and check value of listFiles() which can be null.

    System.out.println("path="+path);
    System.out.println("exists ? "+path.exists());
    System.out.println("isDirectory ? "+path.isDirectory());
    File[] listOfFiles = path.listFiles();
    for (int i = 0; listOfFiles != null && i < listOfFiles.length; i++) {
        System.out.println(listOfFiles[i]);
    }

But the real cause of the error is probably because you've appended ".*" to the keyword and missed that url == null in getFileFromURL. Move return outside finally :

private File getFileFromURL(String keyword) {

    URL url = this.getClass().getClassLoader().getResource(keyword+".*");
    File file = null;
    try {
        file = new File(url.toURI());
    } catch (Exception e) {
        file = new File(url.getPath());
    } 
    return file;
}  

The NPE occurs because the provided path does not contain any files and isn'ta directory. Your input string must be a valid URL to the „mappings“ directory. The path to use depends on your selected working dir of the application.

You'll need to examine the URL and have specific code for each protocol. This answer shows how to interpret the URL. Something like:

try (JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile()) {
    JarEntry entry =
        jar.stream()
        .filter(t -> ...)
        .findFirst().orElse(null);

    try (InputStream in = jar.getInputStream(entry)) {
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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