Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a directory from the runtime classpath?

Tags:

java

classpath

My Java application needs to be able to find a myconfig/ directory which will be bundled inside the same JAR:

myjar.jar/
    com/
        me/
            myproject/
                ConfigLoader.java --> looks for myconfig/ directory and its contents
    myconfig/
        conf-1.xml
        conf.properties
        ... etc.

How do I actually go about reading this myconfig/ directory off the runtime classpath? I've done some research and it seems that the normal method of reading a file from the classpath doesn't work for directories:

InputStream stream = ConfigLoader.class.getResourceAsStream("myconfig");

So does anyone know how to read an entire directory from the runtime classpath (as opposed to a single file)? Thanks in advance!

Please note: It is not possible to load the files individually, myconfig is a directory with thousands of properties files inside it.

like image 394
IAmYourFaja Avatar asked Aug 17 '12 13:08

IAmYourFaja


People also ask

How do I find the classpath directory?

Now to check the value of Java classpath in windows type "echo %CLASSPATH" in your DOS command prompt and it will show you the value of the directory which is included in CLASSPATH.

How do I find classpath runtime?

Check your runtime classpath by going to Run -> Run Configurations and select your application configuration. Check the classpath setting there. There is another workaround for this also. Eclipse by default will include your output folder (usually named bin) in your classpath.

Is current directory in classpath?

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.


2 Answers

You can use the PathMatchingResourcePatternResolver provided by Spring.

public class SpringResourceLoader {

    public static void main(String[] args) throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        // Ant-style path matching
        Resource[] resources = resolver.getResources("/myconfig/**");

        for (Resource resource : resources) {
            InputStream is = resource.getInputStream();
            ...
        }
    }
}

I didn't do anything fancy with the returned Resource but you get the picture.

Add this to your maven dependency (if using maven):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>
like image 119
maba Avatar answered Sep 26 '22 10:09

maba


You could call ClassLoader.getResource() to find a particular file in the directory (or the directory itself, if getResource() will return directories). getResource() returns a URL pointing to the result. You could then convert this URL into whatever form the other library requires.

like image 26
Kenster Avatar answered Sep 24 '22 10:09

Kenster