2

I am new to Spring Boot. I have this emailprop.properties in src/main/resource:

       //your private key  
       mail.smtp.dkim.privatekey=classpath:/emailproperties/private.key.der

But I am getting the error as

classpath:\email properties\private.key.der (The filename, directory name, or volume label syntax is incorrect)

How do I properly load this file?

Update-1

  • my java code is dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"), emailProps.getProperty("mail.smtp.dkim.privatekey"));

  • its working as "D:\\WorkShop\\MyDemoProj\\EmailService\\src\\main\\resources\\private.key.der"Instead of emailProps.getProperty("mail.smtp.dkim.privatekey")

Update-2

  • i have tried java code is String data = ""; ClassPathResource cpr = new ClassPathResource("private.key.der"); try { byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream()); data = new String(bdata, StandardCharsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),data);

  • Error is : java.io.FileNotFoundException: class path resource [classpath:private.key.der] cannot be resolved to URL because it does not exist

  • Tried Code is : ClassPathResource resource = new ClassPathResource(emailProps.getProperty("mail.smtp.dkim.privatekey")); File file = resource.getFile(); String absolutePath = file.getAbsolutePath();

  • Still same error..

  • please update the answer..

Kixeye User
  • 41
  • 1
  • 7
  • 1
    first of all , how are you trying to load the file from the resources folder? have you tried [this one](https://smarterco.de/java-load-file-classpath-spring-boot/) or [this answer](http://stackoverflow.com/questions/36407575/how-to-get-files-from-resources-folder-spring-framework) ?? – AntJavaDev Nov 30 '16 at 15:33
  • `File file = new File(String.valueOf(this.getClass().getResource("classpath:/emailproperties/private.key.der")));` – GingerHead Nov 30 '16 at 15:37
  • @GingerHead thanks for replay. i have tried, but error as **EmailSmsService\class path resource [private.key.der] java.io.FileNotFoundException** – Kixeye User Nov 30 '16 at 19:53

2 Answers2

1

If you want to load this file runtime then you need to use ResourceLoader please have a look here for the documentation - section 8.4.

Resource resource = resourceLoader.getResource("classpath:/emailproperties/private.key.der");

Now if you want to keep this exact path in properties file you can keep it there and then load it in your Autowired constructor/field like that:

@Value("${mail.smtp.dkim.privatekey}") String pathToPrivateKey

and then pass this to the resource loader.

Full example you can find here. I don't want to copy paste it.

Anton Belev
  • 11,963
  • 22
  • 70
  • 111
  • My java code is : `dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"), emailProps.getProperty("mail.smtp.dkim.privatekey"));` – Kixeye User Nov 30 '16 at 19:54
0

If your file is located here:

"D:\\WorkShop\\MyDemoProj\\EmailService\\src\\main\\resources\\private.key.der" 

then it should be:

mail.smtp.dkim.privatekey=classpath:private.key.der

EDIT: I see now, you are using DKIMSigner, which expects file-path string,

Try changing your code like this:

ClassPathResource resource = new ClassPathResource(emailProps.getProperty("mail.smtp.dkim.privatekey"));
File file = resource.getFile();
String absolutePath = file.getAbsolutePath();
dkimSigner = new DKIMSigner(emailProps.getProperty("mail.smtp.dkim.signingdomain"), emailProps.getProperty("mail.smtp.dkim.selector"),absolutePath
);
vmarusic
  • 186
  • 1
  • 6
  • still getting error, if am **`System.out.println(emailProps.getProperty("mail.smtp.dkim.privatekey"))`**, here am printing **`class path resource [private.key.dev]`** – Kixeye User Dec 01 '16 at 12:38
  • thanks, but still i got this error. **`java.io.FileNotFoundException: class path resource [classpath:private.key.der] cannot be resolved to URL because it does not exist`** – Kixeye User Dec 01 '16 at 18:11