java.io.FileNotFoundException: class path resource even though file exists in src/main/resources

11,272

When you use resource.getFile() you look for the file in the file system, thats why it does't work when you runnit as a jar.

Try with a InputStream

String data = "";
ClassPathResource resource = new ClassPathResource("/XML1.xml");
try {
    byte[] dataArr = FileCopyUtils.copyToByteArray(resource.getInputStream());
    data = new String(dataArr, StandardCharsets.UTF_8);
} catch (IOException e) {
    // do whatever
}
Share:
11,272
Adi
Author by

Adi

Updated on June 05, 2022

Comments

  • Adi
    Adi almost 2 years

    enter image description here

    I have my XML file under the src/main/resources directory. My spring code looks like

    import java.io.IOException;
    import java.util.concurrent.atomic.AtomicLong;
    
    import com.google.common.base.Charsets;
    import com.google.common.io.Files;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.integration.xml.transformer.XsltPayloadTransformer;
    import org.springframework.messaging.Message;
    import org.springframework.messaging.support.MessageBuilder;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class BdeApplicationController {
    
        @GetMapping("/ping")
        @ResponseBody
        public String ping(@RequestParam(name="name", required=false, defaultValue="Stranger") String name) {
            return myFlow();
        }
    
        private String myFlow() {
            XsltPayloadTransformer transformer = getXsltTransformer();
            return transformer.transform(buildMessage(getXMLFileString())).toString();
        }
    
        private String getXMLFileString() {
            try {
                return Files.toString(new ClassPathResource("XML1.xml").getFile(), Charsets.UTF_8);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }
    
        private XsltPayloadTransformer getXsltTransformer() {
            return new XsltPayloadTransformer(new ClassPathResource("XSLT1.xsl"));
        }
    
        protected Message<?> buildMessage(Object payload) {
            return MessageBuilder.withPayload(payload).build();
        }
    }
    

    On running this code I get the following exception: -

    java.io.FileNotFoundException: class path resource [XML1.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/user/Documents/project/target/bde-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/XML1.xml

    Can you please suggest how can I fix this?

  • jonhid
    jonhid about 6 years
    Are you running it like a jar?
  • Adi
    Adi about 6 years
    Hi, yes I am running it as a jar, and your solution works now :)