Loading resources from a directory

Hello guys,

I want to load, using the Resources interface, a folder that contains .png images. I know about the cuba.confDir property that I can set in app.properties, but the problem here is that the path where each developer saves the project is different, so everyone would need to set the cuba.confDir property according to their location of the CUBA App. So, I wondered if there is any way of loading this resources folder using CLASSPATH environment variable, and if there is a way, I would really appreciate an example of how I can the CLASSPATH variable from some settings of properties from within CUBA.

hi, @bujoralexandru1996

is the directory with images located in the container with your application (for example, in Tomcat) or it is not?

Regards,
Daniil.

Hi, @tsarev

it’s not located in the application container.

Actually, it is not CUBA-specific task and there is no need to use CLASSPATH env variable.

There a lot of examples how to load a file on the internet: link.

Regards,
Daniil.

The point of the question was to find out if there were any mechanism that allows users to load files from external paths. Currently, my implementation is with the input stream, but it’s kinda messy, because the file that reads from the files has “hard-coded” paths to it. Thank you for your answer!

How do you want to use these files?

They represent some default seed data for populating the db.

I suggest that you create some application property (documentation) that will represent a location of your files.

Then you can create a service that will obtain the location from this property via AppContext.getProperty(propertyName).

In this case each developer will be able to set a location to the property.

Is it suitable for you?

I’ve thought of a similar solution, but this does not eliminate the fact that each developer MUST set it’s own path. The thing is, that this folder that I am trying to load will be located outside the cuba app. The solution that I am looking for should provide the following facilities. Having the app deployed as an UberJar in this path: /a/random/unix/system/path, I want my application to figure out that at the same path (/a/random/unix/system/path), there is a /resource folder in which there are some vital info.

Let’s say this is the file tree for my example:

- a 
    - random
        - unix
            - system
                - path
                    - resources
                        - countries
                        - logo.jpg
                    - app.jar (the UberJar)

I would like that my UberJar will know that in the same folder with it, there will be this folder. This allows me to move the 2 files (app.jar and resources) in any folder in the system and as long as they are in the same folder, everything should be good.

PS: My current implementation is giving the relative path to the class in which I am using the /resource folder, but this is an ugly approach.

Instead of getting the path like this:

String pathToRes = "../../../resources"; // this is how I do it now 

I would like to get it something like this:

String pathToRes = obj.getResource("logo.jpg");

Where obj already knows the absolute path to the /resources folder and searches for that resource that I need i.e. “logo.jpg”. I need obj to not be initialised with a hardcoded absolute path, instead, I would like to be initialised accordingly, to the location of the UberJar. I need this initialisation to be independent of the fact that the UberJar and /resources folder might be moved. At runtime, obj should contain the absolute path to the location of the UberJar.