Java (maven web application), getting the full path to the file in the resource folder?

I am working on a project that is configured using the standard Maven directory structure, so I have a folder called "resources", and in this I created a folder called "fonts" and then put the file in it. I need to transfer the full path of the String file (the file that is in my project structure to resources/fonts/somefont.ttf ) to the object I am using from a third-party library, as shown below, I searched for this for a while, but a little confused about the right way to do this. I tried as below, but he cannot find it. I looked at using a ResourceBundle , but it seemed to be related to creating the actual File object when I just needed a path to go to a method like the one below (I don't have a direct method call, so just give an example from my memory):

 FontFactory.somemethod("resources/fonts/somefont.ttf"); 

I thought there was a way, with a project with the standard Maven directory structure, to get the file from the resource folder without using the full relative path from the class / package. Any advice on this is welcome.

I don’t want to use a hard-coded path, because the different developers who work on the project have different settings, and I want to include this as part of the project to get it directly when they check the source of the project.

This is for a web application (Struts 1.3 application), and when I look at the exploded WAR file (which I run the project through Tomcat), the file is located at:

 <Exploded war dir>/resources/fonts/somefont.ttf 
+6
source share
5 answers

the code:

 import java.io.File; import org.springframework.core.io.*; public String getFontFilePath(String classpathRelativePath) { Resource rsrc = new ClassPathResource(classpathRelativePath); return rsrc.getFile().getAbsolutePath(); } 

In your case, classpathRelativePath will be something like "/resources/fonts/somefont.ttf".

+11
source

You can use the following to get the file path:

 String fileName = "/filename.extension"; //use forward slash to recognize your file String path = this.getClass().getResource(fileName).toString(); 

use / pass the path to your methods.

+3
source

If your resource directory is at the root of your war, this means that the /fonts/somefont.ttf resources will be the "virtual path" where this file is available. You can get the "real path" - the absolute path to the file system - from the ServletContext. Note (in the docs) that this only works when the WAR explodes. If your container launches an application from a war file without its extension, this method will not work.

+1
source
 URL resource = getServletContext().getResource("/WEB-INF/classes/fonts/somefont.ttf"); String relativePath = resource.getPath(); 
+1
source

You can find the answer to the question about similar lines that I had Downloading XML files during the Maven test run

BobG's answer should work. Although you need to keep in mind that the path for the resource file is relative to the path to the current class. Both resources and java source files are in classpath

0
source

Source: https://habr.com/ru/post/895820/


All Articles