How to get path to directory in resources

Hi Vaadin:)
I am working with Vaadin 14 with Spring boot and I have problem with trying to reach directory with sounds in resources as a File class.

		File soundsDir = new File(SOUNDS_DIRECTORY); // creating file with directory path - what should I put instead of SOUNDS_DIRECTORY variable?
        File[] soundFiles = soundsDir.listFiles(); // soundfiles is null because no directory is found
        if (soundFiles != null) {
		    for (File oneSoundFile : soundFiles) {
				// do something with each file in directory
			}
		}

Path of java class = /src/main/java/ui/utils/SoundLibrary.java

Path of sounds directory = /src/main/resources/static/frontend/sounds

Only moment it worked was, when I hardcoded path to the directory on my local to try it. I tried almost everything, even ServletUtils from [How to get the Absolute Path in Vaadin 10 ]
(https://vaadin.com/forum/thread/17283562/how-to-get-the-absolute-path-in-vaadin-10), but nothing helped…

Weird is, that when I try getElement().setProperty("src", "./frontend/sounds/example.mp3"); on one Sound (private class in SoundLibrary), it works.

Thanks,

Petr

Petr Lohonka:
Hi Vaadin:)
I am working with Vaadin 14 with Spring boot and I have problem with trying to reach directory with sounds in resources as a File class.

		File soundsDir = new File(SOUNDS_DIRECTORY); // creating file with directory path - what should I put instead of SOUNDS_DIRECTORY variable?
        File[] soundFiles = soundsDir.listFiles(); // soundfiles is null because no directory is found
        if (soundFiles != null) {
		    for (File oneSoundFile : soundFiles) {
				// do something with each file in directory
			}
		}

Path of java class = /src/main/java/ui/utils/SoundLibrary.java

Path of sounds directory = /src/main/resources/static/frontend/sounds

Only moment it worked was, when I hardcoded path to the directory on my local to try it. I tried almost everything, even ServletUtils from [How to get the Absolute Path in Vaadin 10 ]
(https://vaadin.com/forum/thread/17283562/how-to-get-the-absolute-path-in-vaadin-10), but nothing helped…

Weird is, that when I try getElement().setProperty("src", "./frontend/sounds/example.mp3"); on one Sound (private class in SoundLibrary), it works.

Thanks,

Petr

Hi Petr,

How about store directory path into springboot application.properties.

You can try set “SOUND_DIRECTORY” into your application.properties like below:

file.sound-dir=/home/someuser/somefolder/somesound

then you inject that properties into your java bean. I’ve also attached sample.

Best Regards,

Reja

17912493.java (129 Bytes)
17912496.properties (51 Bytes)

Hi reja,

really thank you for your answer. But my problem is not how to inject it to Java bean, but WHAT to inject to java bean.

If I would use your example, what path (what string) exactly in my case I should put to

file.sound-dir=??????????

if directory, where should point file.sound-dir is on path /src/main/resources/static/frontend/sounds.

What string would you exactly write in application.properties for my case?
I just don’t know, how to reach path /src/main/resources/static/frontend/sounds (if I put there simply this string, it doesn’t work).

Petr Lohonka:
Hi Vaadin:)
I am working with Vaadin 14 with Spring boot and I have problem with trying to reach directory with sounds in resources as a File class.

		File soundsDir = new File(SOUNDS_DIRECTORY); // creating file with directory path - what should I put instead of SOUNDS_DIRECTORY variable?
        File[] soundFiles = soundsDir.listFiles(); // soundfiles is null because no directory is found
        if (soundFiles != null) {
		    for (File oneSoundFile : soundFiles) {
				// do something with each file in directory
			}
		}

Path of java class = /src/main/java/ui/utils/SoundLibrary.java

Path of sounds directory = /src/main/resources/static/frontend/sounds

Only moment it worked was, when I hardcoded path to the directory on my local to try it. I tried almost everything, even ServletUtils from [How to get the Absolute Path in Vaadin 10 ]
(https://vaadin.com/forum/thread/17283562/how-to-get-the-absolute-path-in-vaadin-10), but nothing helped…

Weird is, that when I try getElement().setProperty("src", "./frontend/sounds/example.mp3"); on one Sound (private class in SoundLibrary), it works.

Thanks,

Petr

Hi Petr,

How about store directory path into springboot application.properties.

You can try set “SOUND_DIRECTORY” into your application.properties like below:

file.sound-dir=/home/someuser/somefolder/somesound

then you inject that properties into your java bean. I’ve also attached sample.

Best Regards,

Reja

Petr Lohonka:
Hi reja,

really thank you for your answer. But my problem is not how to inject it to Java bean, but WHAT to inject to java bean.

If I would use your example, what path (what string) exactly in my case I should put to

file.sound-dir=??????????

if directory, where should point file.sound-dir is on path /src/main/resources/static/frontend/sounds.

What string would you exactly write in application.properties for my case?
I just don’t know, how to reach path /src/main/resources/static/frontend/sounds (if I put there simply this string, it doesn’t work).

Hi Petr,

I see other similarity sample, but in directory "/src/main/resources/META-INF/resources/"

You can see the sample from below link. (Section “Resource Cheat Sheet” - Table 2. Spring projects)

https://vaadin.com/docs/v14/flow/importing-dependencies/tutorial-ways-of-importing.html

Hope this help.

Best Regards,

Reja

So you think it is maybe because I have the resources in the wrong directory?

Petr Lohonka:
So you think it is maybe because I have the resources in the wrong directory?

Yeah, I guess. If I’m not wrong, based on that documentation your static file ex. images should be in /src/main/resources/META-INF/resources/

Other similarity case in stackoverflow question.
https://stackoverflow.com/questions/57553973/where-should-i-place-my-vaadin-10-static-files/57553974#57553974

Best Regards,

Reja

Hi Reja,
sorry for the late answer.

I managed to make it run by moving resources to src/main/webapp/sounds (according to cheat sheet for VAADIN 14 for npm in [Storing and Loading Resources]
(https://vaadin.com/docs/v14/flow/importing-dependencies/tutorial-ways-of-importing.html)) and then adding ServletUtils class based on [How to get the Absolute Path in Vaadin 10]
(https://vaadin.com/forum/thread/17283562/how-to-get-the-absolute-path-in-vaadin-10).
Now I can reach files easily and use them.

Really thank you for your help!

Petr

Petr Lohonka:
Hi Reja,
sorry for the late answer.

I managed to make it run by moving resources to src/main/webapp/sounds (according to cheat sheet for VAADIN 14 for npm in [Storing and Loading Resources]
(https://vaadin.com/docs/v14/flow/importing-dependencies/tutorial-ways-of-importing.html)) and then adding ServletUtils class based on [How to get the Absolute Path in Vaadin 10]
(https://vaadin.com/forum/thread/17283562/how-to-get-the-absolute-path-in-vaadin-10).
Now I can reach files easily and use them.

Really thank you for your help!

Petr

Glad to hear that.

Cheers,
Reja