FileNotFoundException in test case run with maven using Spring resource loader

Our program is sending mails. For sending mails it includes an attachment.

The project is set up with maven. The JUnit test case loads the spring configuration first. Spring is loading the File with its DefaultResourceLoader. When running this directly within the workspace its running fine, however as a maven test it fails.

Spring Configuration for resource loader:

<property name="defaultResourceLoader">
    <bean class="org.springframework.core.io.DefaultResourceLoader" />
</property>

ImplementingClass:

@Service
public class SomeClassA {

     @Autowired
     private DefaultResourceLoader defaultResourceLoader; 

     @Value("${mailLogoPath}")
     private String mailLogo; 

     public void someMethod(){
         String filePath = defaultResourceLoader.getResource(mailLogo).getFile().getAbsolutePath();   
     }
}

The exception points to the actual problem and difference:

java.io.FileNotFoundException: class path resource [templates/efmaillogo.jpg] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/IBM/workspace/efMy10/ef-mvxsrv-reactor/ef-mvxsrv-service-resources/target/ef-mvxsrv-service-resources-1.5.23-SNAPSHOT.jar!/templates/efmaillogo.jpg at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:204) at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52) at SomeClassA.someMethod(SomeClassA.java:9)

Spring tries to look the file up within the jar. When running it as a junit test within eclipse, everything is working, because spring finds the file directly within the workspace. However when running the same unit test with maven it fails, because it doesn't find the file, because its in a jar in a different module. The file in the other module is needed by different modules, so I don't want to move it. On the application server where everything is deployed its working as well, because the .ear file is expanded.

I'm wondering if there is a different way to access the file with Spring, so that I don't have to skip this test case with maven.

Update Tried doing a lookup for the file with:

 String filePath = defaultResourceLoader.getResource(mailLogo).getURL.getFile();

However it now fails when actually sending the mail with Transport.send(msg).

java.io.FileNotFoundException: file:\C:\Users\uhe.m2\repository\com\clavisit\ef\mvxsrv\ef-mvxsrv-service-resources\1.5.23-SNAPSHOT\ef-mvxsrv-service-resources-1.5.23-SNAPSHOT.jar!\templates\efmaillogo.jpg (The filename, directory name, or volume label syntax is incorrect.) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at com.clavisit.ef.ep.service.integration.handler.mail.SendMail.sendMessage(SendMail.java:153)

Answers


String filePath = defaultResourceLoader.getResource(mailLogo).getURL().getFile();

The above code change must work as per the discussion in this thread http://www.coderanch.com/t/474047/Spring/Spring-cannot-find-file-classpath


Need Your Help

Passing the url in a link not working

php

I'm trying to pass the id of a blogpost in an url. It works in the adminpanel (read blogpost) now I want to do the same on my frontpage, so that it says read more under every blogpost, but when I u...

Trying to reverse the contents of the file and reverse the file's name into a new file?

c++ binary reverse fstream ifstream

So here's my code, but I don't know how to make the new file's name the original file's name reversed.