Search code examples
javaspringemailembedded-resource

java jar file reading image file


i have stand alone application, which has one of the module to sent out emails. This application packaged as executable JAR containing all the resource files including images.

I am using spring for sending email, which contains following code for inline:

Spring code is using org.springframework.core.io.FileSystemResource

//IN-LINE ATTCHEMENTS
                if (null != msg.getInlineAttachments() && msg.getInlineAttachments().size() > 0) {
                    for (Map.Entry<String, File> e : msg.getInlineAttachments().entrySet()) {
                        if (log.isTraceEnabled()) {
                            log.trace("Conntent-ID:" + e.getKey() + ", Resource:" + e.getValue());
                        }
                        try {
                            helper.addInline(e.getKey(), new FileSystemResource(e.getValue()));
                        } catch (Exception e1) {
                            log.error(e1);
                        }
                    }
                }

File image is passed to above code using following:

    ClassPathResource res = new ClassPathResource("./images/" + name);
    if (log.isTraceEnabled()) {
        log.trace(res.getFile().getAbsolutePath());
    }
    file = res.getFile();

Note: Application works fine when executed in development environment in eclipse, because it is exploded format, non-jar.

Exception:

    java.io.FileNotFoundException: class path resource [images/app_logo.png] 
cannot be resolved to absolute file path because it does not reside in the file system:
 jar:file:/C:/TEMP/app-1.0/app-1.0.jar!/images/app_logo.png

Solution

  • Only option left out is copy image files into temp folder, and reference from there...