Home > Mobile >  File not found in jar after deployment
File not found in jar after deployment

Time:12-22

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related