2

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)

2
  • 1
    Even if the EAR is expanded, surely the jar remains as a jar? In which case, the image is found on the server inside a jar, but not found inside a jar using Maven?
    – Paul Grime
    Jun 18, 2012 at 8:08
  • Just figured the difference why it was working on the server. It was placed in the WEB-INF/classes.
    – Udo Held
    Jun 18, 2012 at 8:28

1 Answer 1

2

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

2
  • This one works partially. Unfortuately I now get the FileNotFoundException, when sending the mail and not directly when looking it up. Will post an update to the question.
    – Udo Held
    Jun 18, 2012 at 9:17
  • I switched to directly using urls and a URLDataSource in my DataHandler.
    – Udo Held
    Jun 22, 2012 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.