0

this is the font file under fonts folder in resources:

enter image description here

now, i want to get this file and use in CaptchaConfiguration that is a spring configuration class, but cannot:

@Configuration
public class CaptchaConfiguration {
    @Bean
    public CaptchaService captchaService() {

        WordGenerator wordGen = new RandomWordGenerator("123456789");

        Font font;
        try {
            ClassPathResource res = new ClassPathResource("fonts/iranSans.ttf");    
            File fontFile = new File(res.getPath());

            font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
        }
        catch (Exception e) {
            e.printStackTrace();
            font = new Font("Arial", Font.PLAIN, 10);
        }

        // ....
    }
}

i also tested File fontFile = new File(getClass().getResource("fonts/iranSans.ttf").getFile()), but didn't have any result and res is null.

Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71

1 Answers1

0

I had the same Problem at some point and I solved it like this:

new File(Objects.requireNonNull(CLASSNAME.class.getResource( "/micross.ttf")).getFile())

So I think you need to add a Backslash to the path: "/fonts/iranSans.ttf".

ouflak
  • 2,458
  • 10
  • 44
  • 49