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

17,035

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
17,035
Rick
Author by

Rick

Web programmer with an interest in web task automation, building websites, etc, I prefer to do everything in Python now as I have moved to it from using a variety of other languages in the past. I also like to do front-end AJAX / javascript work but am moving to do this through Python as well, with the Pyjamas framework.

Updated on June 04, 2022

Comments

  • Rick
    Rick about 2 years

    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
    
  • Rick
    Rick almost 13 years
    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 / spring
  • Rick
    Rick almost 13 years
    we do run it as exploded and I don't see that changing anytime soon so this should work, thanks for the tip
  • Goran Nastov
    Goran Nastov almost 10 years
    Small correction to the code: ClassPathResource rsrc = new ClassPathResource(classpathRelativePath); This way you can call the method .getFile()
  • Dave Newton
    Dave Newton almost 10 years
    @GoranNastov The current docs show a Resource#getFile() call; can you elaborate? docs.spring.io/spring/docs/current/javadoc-api/org/…
  • Goran Nastov
    Goran Nastov almost 10 years
    @DaveNewton Thanks for the remark. I must have been using older version or different interface. I tested and my update is corrected.