Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java (maven web app), getting full file path for file in resources folder?

I'm working with a project that is setup using the standard Maven directory structure so I have a folder called "resources" and within this I have made a folder called "fonts" and then put a file in it. I need to pass in the full String file path (of a file that is located, within my project structure, at resources/fonts/somefont.ttf) to an object I am using, from a 3rd party library, as below, I have searched on this for a while but have become a bit confused as to the proper way to do this. I have tried as below but it isn't able to find it. I looked at using ResourceBundle but that seemed to involve making an actual File object when I just need the path to pass into a method like the one below (don't have the actual method call in front of me so just giving an example from my memory):

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

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

I don't want to use a hard-coded path since different developers who work on the project have different setups and I want to include this as part of the project so that they get it directly when they checkout the project source.

This is for a web application (Struts 1.3 app) and when I look into the exploded WAR file (which I am running the project off of through Tomcat), the file is at:

<Exploded war dir>/resources/fonts/somefont.ttf
like image 997
Rick Avatar asked Aug 25 '11 01:08

Rick


People also ask

How do you load properties file from resources folder in Java?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().

What is resources folder in Maven project?

Maven resources folder is used to store all your project resources files like , xml files, images, text files and etc. The default Maven resources folder is located at “yourproject/src/main/resources“.

How do you create an absolute path in Java?

The method java. io. File. getAbsolutePath() is used to obtain the absolute path of a file in the form of a string.


2 Answers

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 would be something like "/resources/fonts/somefont.ttf".

like image 161
BobG Avatar answered Nov 14 '22 22:11

BobG


You can use the below mentioned to get the path of the file:

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.

like image 40
user1129947 Avatar answered Nov 14 '22 23:11

user1129947