简体   繁体   中英

File not found in jar after deployment

I have program that throws FileNotFoundException on this statement when the jar is deployed:

final var fopFactory = FopFactory.newInstance(new File("src//main//resources//afp//fop.xconf"));

But this works very well locally with intellij. I looked around a bit and apparently it needs to go through an InputStream but my FopFactory.newInstance is waiting a File object as a parameter and not an InputStream. Could you tell me how to work around this problem please?

Thanks in advance.

A File object points to a file. The resources within a JAR file are not files, so you can not create a File object that points to them.

In Spring, one usually would inject a Resource instead, and pass that Resource to the FopFactory . That presumes that the FopFactory accepts a Resource , or can be changed to accept one.

If that isn't the case, calling code can ask the Resource for an InputStream , and pass that to the FopFactory . If the FopFactory is an org.apache.fop.apps.FopFactory , this can be accomplished by writing FapFactory.newInstance(baseURI, resource.getInputStream()) .

If the FopFactory doesn't accept an InputStream either, the only option is to read the InputStream , write it to a temporary file, and pass that File to the FopFactory . But that's really convoluted, and there is nearly always a better option.

Use new File(getClass().getRessource("/afp/fop.xconf").getFile());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM