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

4

6

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
Theomancy answered 25/8, 2011 at 1:32 Comment(4)
What's wrong with getting the File object from the ResourceBundle, then using File.getAbsolutePath() to pass to FontFactory.somemethod()? (Bear in mind that you're not guaranteed to get a File object if the WAR isn't expanded on deployment.)Straley
probably nothing is wrong with that :) thanks, I'm not all that familiar with ResourceBundle so didn't realize this would work, thanks for the tipTheomancy
Actually let me post you some pseudo-code I've used with success below - not sure on ResourceBundle, but Resource (out of Spring) works:Straley
Thanks, I will try that tomorrow when I am in my IDE againTheomancy
S
11

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".

Straley answered 25/8, 2011 at 1:51 Comment(4)
I'm not actually using spring for this particular project, probably should have left that keyword out, I do have a need to do it in spring sometimes also, so I was looking for a way that would work in both struts / springTheomancy
Small correction to the code: ClassPathResource rsrc = new ClassPathResource(classpathRelativePath); This way you can call the method .getFile()Projective
@GoranNastov The current docs show a Resource#getFile() call; can you elaborate? docs.spring.io/spring/docs/current/javadoc-api/org/…Bootle
@DaveNewton Thanks for the remark. I must have been using older version or different interface. I tested and my update is corrected.Projective
P
3

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.

Patrizio answered 4/1, 2012 at 12:57 Comment(0)
Q
1

If your resources directory is in the root of your war, that means resources/fonts/somefont.ttf would be a "virtual path" where that file is available. You can get the "real path"--the absolute file system path--from the ServletContext. Note (in the docs) that this only works if the WAR is exploded. If your container runs the app from the war file without expanding it, this method won't work.

Questor answered 25/8, 2011 at 3:5 Comment(1)
we do run it as exploded and I don't see that changing anytime soon so this should work, thanks for the tipTheomancy
G
0

You can look up the answer to the question on similar lines which I had Loading XML Files during Maven Test run

The answer given by BobG should work. Though you need to keep in mind that path for the resource file is relative to path of the current class. Both resources and java source files are in classpath

Gaiter answered 25/8, 2011 at 2:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.