Error using ImageJFunctions.show from UberJar; but works from within IntelliJ

Hi,

I am have an issue with my software, when run from an UberJar built with Maven.

I use this call in my code to show an image in a window:

ImagePlus imp = ImageJFunctions.show(Views.stack(componentLevelImageStack));

This works perfectly, when running the program from within IntelliJ.

However, when building an UberJar, I get the following error:

Uncaught error fetching image:
java.lang.IllegalStateException: zip file closed
	at java.util.zip.ZipFile.ensureOpen(ZipFile.java:686)
	at java.util.zip.ZipFile.getInputStream(ZipFile.java:366)
	at java.util.jar.JarFile.getInputStream(JarFile.java:447)
	at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:164)
	at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:127)
	at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
	at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
	at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)

I would be very grateful for any pointers on how to fix this.

Thanks,
Michael

I am still trying to solve this issue.

While debugging and stepping into the code I was able to find out that the code fails, when it tries to load the ImageJ icon, which is part of the image window (see screenshot below).
It tries to load icons/imagej-256.png and then throw the exception inside the method ensureOpen();

Unfortunately, I don’t know how to proceed with this, so I would be very open for any suggestion on what to try out.

Furthermore, what boggles me is that the icon does not even show up in the image window (which works, if I run the code directly from IntelliJ instead of from the UberJar).

1 Like

Hi @michaelmell,

Sorry it took awhile for someone to get to help you, but glad you seem to have made some progress.

Do you happen to have a small (minimal) example that reproduces this error? It’ll be a bit tough to help if we can’t experience the same problem you’re getting.

As well, how exactly did you build the uber jar?
And to clarify - does the exception you included in the first post happen while building the uber jar? Or when running code from an uber jar that you’ve built?

John

Hi @bogovicj,

thanks for your reply.

The exception that I included in my first post happens at run time, but only when running the jar (not while building).

I have created a minimal example here:

Please see the pom.xml and deploy.sh for details on how the jar is built and the shading is done.
I suspect that I need to adjust the shading in pom.xml, but I am too unfamiliar with the configuration.

Best,
Michael

1 Like

Thanks! I could reproduce the issue you’re seeing. and I get it now!

This line is the issue:

When you run this with a jar, the image is not a file. It’s inside the jar (which is itself a zip file).
You could replace that line with:

URL imageFile = main.class.getResource( "/probabilities_watershedding_000.tif" );

and it should still work in your IDE, and it will know how to find the file in the jar.

You’ll still get errors though (I do). The issue then becomes, can you read an image that is itself part of a jar. And I’m not sure whether that’s easy to do with scifio at the momemt - I’d consider making a separate post if you want to try that.

Is there a particular reason you’d like to include your image inside the jar? The easy solution is to keep that file outside the jar.

John

P.s. Check this out too:

I realize now, that my example is a bit misleading. Sorry for this.
The embedding inside the JAR is actually not the issue that I am concerned with (I actually just copied the code for getting the path relative to the project-root from a unit-test).

In my original code I read an external image file and still get the issue. You also get this error, if you replace this line:

String imageFile = new File("").getAbsolutePath() + "/src/main/resources/probabilities_watershedding_000.tif";

with the absolute path to the image file, like so:

String imageFile = "/PATH/TO/GIT/REPO/src/main/resources/probabilities_watershedding_000.tif";

For anyone stumbling on this:
I have asked the same question on SO here:

It seems to me that might be something non-specific to ImageJ and I hope to get more exposure/input there.