0

I am currently working on a project where I need to read an image but it doesn't work currently. As long as I run it in IntelliJ everything is fine, but as soon as I export the project into a jar I get this Error:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at fachklassen.Bildpuzzle.splitImage(Bildpuzzle.java:66)
    at fachklassen.Bildpuzzle.<init>(Bildpuzzle.java:21)
    at steuerung.Steuerung.neuesSpiel(Steuerung.java:51)
    at gui.GuiController.onClick(GuiController.java:125)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

This is the path that is printed out for debugging purposes: jar:file:/D:/___Workspace/Schiebepuzzle/classes/artifacts/Schiebepuzzle/Schiebepuzzle.jar!/katze.jpg

And my project structure looks like this(simplified):

-src
    -fachklassen
        -Bildpuzzle (The class where the code is executed)

    -res (marked as resource folder)
        -katze.jpg

I'm not sure if the image is actually in the jar (used an online decompiler where it didn't show up).

What did I do wrong?

        File file;
        URL url;
        if (src == null){
            url = Bildpuzzle.class.getResource("/katze.jpg");
            System.out.println(url);
            file = new File(url.getFile());
        }

I would expect it to read the file in the jar like it does in the IDE.

Thanks for any suggestions.

3 Answers3

2

ImageIO.read(File) method is using file IO which is not suitable for reading resources from the classpath. Assuming that Schiebepuzzle.jar is on the classpath you can use ImageIO.read(InputStream) method:

try (InputStream in = Bildpuzzle.class.getResourceAsStream("/katze.jpg")) {
  BufferedImage img = ImageIO.read(in);
  ...
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
0

Provide the absolute path to the file in the project structure.

    if (src == null){
        url = Main.class.getResource("/com/company/res/test.png");
        System.out.println(url);
        file = new File(url.getFile());
    }

You can see where the image is located in your jar and the absolute path to it by using jar -tf on you jar file.

jar -tf test.jar
META-INF/MANIFEST.MF
com/
com/company/
com/company/res/
com/company/res/test.png
com/company/test/
com/company/test/Main.class
KReth
  • 11
  • 6
  • Would the absolute path be (1) "/src/res/katze.jpg" or (2) "/res/katze.jpg" or something different? – Felix Dobler Apr 01 '19 at 20:04
  • If you run the jar -tf command on your jar you will see the absolute path of where your file is located at. I have added what returns when I run that on my jar in my answer. – KReth Apr 01 '19 at 20:32
  • Accessing an entry of a jar via `File` won't work. The op is trying to do this: He mentioned the path being `jar:file:/.../Schiebepuzzle.jar!/katze.jpg`. Also the path doesn't seem to be an issue, since `url` is not `null`... – fabian Apr 02 '19 at 11:34
0

Got it working :)

URL url = Bildpuzzle.class.getResource("/katze.jpg");
BufferedImage bufferedImage = ImageIO.read(url);

Is the solution. Thanks for your help!