0

I am writing a program and am attempting to export it and send it to someone. When I run the program in Eclipse, it works perfectly well. However, when I export it as a Runnable JAR File and double-click it, it quits and tells me to check the console.

Here's the stack trace:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at me.pogostick29.audiorpg.util.PrereleaseChecker.run(PrereleaseChecker.java:29)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at me.pogostick29.audiorpg.window.Window.<init>(Window.java:62)
    at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

I think it didn't export the image correctly. Here's how I access the image:

BufferedImage myPicture = null;
try { myPicture = ImageIO.read(new File("images/audiorpglogo.png")); }
catch (Exception e) { e.printStackTrace(); }
logo = new JLabel(new ImageIcon(myPicture));

Also, when i try

myPicture = ImageIO.read(Window.class.getResource("/images/audiorpglogo.png"));

I get a stack trace, even when running within Eclipse:

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1362)
    at me.pogostick29.audiorpg.window.Window.<init>(Window.java:70)
    at me.pogostick29.audiorpg.window.WindowManager.setup(WindowManager.java:16)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:29)
nrubin29
  • 1,522
  • 5
  • 25
  • 53
  • 1
    Go to command prompt/terminal, and run the program using `java -jar` syntax. Then you can see the exception output. Post that here. –  Apr 11 '13 at 23:42
  • How about showing us the exception / trace? – John3136 Apr 11 '13 at 23:42
  • Well, good stacktrace. Unfortunately it tells us nothing about your code. –  Apr 11 '13 at 23:45
  • It says that it cannot load the image. I guess Eclipse didn't add the image to the jar file. `javax.imageio.ImageIO.read` – nrubin29 Apr 11 '13 at 23:47
  • 1
    @PogoStick29 How are acessing the image file? Can you show us that code? – Smit Apr 11 '13 at 23:47
  • @PogoStick29 Yes, you're right, you need to add the image to the JAR for that to work. –  Apr 11 '13 at 23:51
  • Here's where the image is on Eclipse: http://icap.me/i/ji6RDiFPLI.png What do I need to do with it? – nrubin29 Apr 11 '13 at 23:52
  • Read [here](http://stackoverflow.com/questions/2273040/how-to-bundle-images-in-jar-file), for starters :P –  Apr 11 '13 at 23:54
  • @PogoStick29 YOu can use `Classname.class.getResource("Your file name")`. Make sure that you have that file in your class folder. – Smit Apr 12 '13 at 00:06
  • I tried `myPicture = ImageIO.read(Window.class.getResource("/images/audiorpglogo.png"));` but I got a stack trace `java.lang.IllegalArgumentException: input == null!` – nrubin29 Apr 12 '13 at 00:12
  • @PogoStick29 If you want to use this then you have to move your image file into that class folder. and only you have to use `audiorpglogo.png`. However the answer given Pshemo was very correct. Much better than waht I am telling you. – Smit Apr 12 '13 at 00:38
  • Noob question, but by class folder do you mean the package in which the class resides? – nrubin29 Apr 12 '13 at 00:44
  • @PogoStick29 Yes class folder where you have your files. – Smit Apr 12 '13 at 16:41

1 Answers1

0

You can try this way:

ImageIcon icon = new ImageIcon("images/myimage.jpg");

this will work if you have images/myimage.jpg directory in the same folder that you are running your application with your JAR file.


or if you want to use image that is placed inside JAR file you can use

ImageIcon icon = new ImageIcon(getClass().getResource("images/myimage.jpg"));

Don't be afraid when it wont work in IDE. It will work when you run it from JAR file.


Here is some code sample that works for me:

import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyFrame extends JFrame {
    private static final long serialVersionUID = 1L;

    private JLabel iconLabel;

    public MyFrame() throws IOException {

        ImageIcon icon = new ImageIcon(getClass().getResource(
                "images/myimage.jpg"));
        iconLabel = new JLabel(icon);
        getContentPane().add(iconLabel);

        setSize(icon.getIconWidth(), icon.getIconHeight());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        new MyFrame();
    }
}

When I try to run it in Eclipse I get

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at MyFrame.<init>(MyFrame.java:14)
    at MyFrame.main(MyFrame.java:25)

this Eclipse project looks like

enter image description here

After i export this project to JAR with images folder by

right-click on project -> Export -> JAR file -> next

enter image description here

next -> next -> select main class

enter image description here

I double-click created JAR file and see frame with my image:

enter image description here

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I need the JAR to be self-contained. I tried something similar to the second idea you had (see code above), and it didn't work. – nrubin29 Apr 12 '13 at 00:27
  • Here is where my image file is stored: http://icap.me/i/ItljCp3Z6c.png Here is how I attempt to access it: `myPicture = ImageIO.read(getClass().getResource("images/audiorpglogo.png"));` Here is the stack trace I get when exporting (as JAR file) and running with `java -jar`: `java.lang.IllegalArgumentException: input == null!` – nrubin29 Apr 12 '13 at 00:52
  • Would you like a copy of the code so you can try to make it work? – nrubin29 Apr 12 '13 at 00:53
  • @PogoStick29 Do you have this error when you run it from IDE or from console? – Pshemo Apr 12 '13 at 00:58
  • I get the error when I run it form the console with `java -jar` – nrubin29 Apr 12 '13 at 01:00
  • Are you sure your Jar file contains `images` directory? JARs are ZIP files so you can unzip them somewhere to check its content. – Pshemo Apr 12 '13 at 01:01
  • Here's what I get when I unzip: http://icap.me/i/wboY28Ixpo.png and here's what's inside the images folder: http://icap.me/i/RExTWBDbWs.png – nrubin29 Apr 12 '13 at 01:06
  • Could you tell me if my sample project works for you? Maybe problem is in paths not in code. Maybe you need to add `./images` or something like that (not sure myself). – Pshemo Apr 12 '13 at 01:14
  • @PogoStick29 Could you instead of `BufferedImage` and `ImageIO.read` try to use just `logo = new JLabel(new ImageIcon(getClass().getResource("images/myimage.jpg")));`. Also make sure that after doing any change in code you are running the newest JAR file. – Pshemo Apr 12 '13 at 01:20
  • I am now using `logo = new JLabel(new ImageIcon(getClass().getResource("images/audiorpglogo.png")));`. The program quits without a stack trace or anything. (http://icap.me/i/kCHAWZFj5T.png) – nrubin29 Apr 12 '13 at 01:36
  • @PogoStick29 Try to add these lines to your code `System.out.println("before using image"); logo = new JLabel(new ImageIcon(getClass().getResource("images/audiorpglogo.png"))); System.out.println("after using image");`. After you run your code if you see only `"before using image"` then it means that we still have problem with reading image, if you will also see `"after using image"` then it is probable that reading image worked. Also if you wont see any output in console then it means that your app crashed before this lines. – Pshemo Apr 12 '13 at 01:43
  • @PogoStick29 OK, I give up for today, I am too sleepy. Will try to help you tomorrow. Also maybe try to create and add to your question [SSCCE](http://sscce.org/) so others will be able to reproduce this unusual behavior on their computers. – Pshemo Apr 12 '13 at 01:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28163/discussion-between-pogostick29-and-pshemo) – nrubin29 Apr 13 '13 at 13:42