My program uses Image.class, which helps me recieve image.
Image img = new ImageIcon("Shooter2D/res/background.jpg").getImage();
When the program is run from the development environment - everything works, after compiling a jar file - does not work. Tell me how to properly set the path to work in the IDE (Intellij IDEA) and in the archive. Shooter2D.jar contain:
- META-INF
Manifest-Version: 1.0
Main-Class: Shooter2Dv22082013.Main
- res
all pictures
- Shooter2Dv22082013
all .class files, main is Main.class
indicative figure: http://imageshack.us/photo/my-images/801/eyjv.png/
Here's what the javadoc says about the constructor of ImageIcon:
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path.
(emphasis mine)
Your image is not stored in a file. It isn't in your file system. It's in a jar that is itself in the classpath. And that's where you want to load it from. Wherever the jar file of your application is on the end-user's machine, your program wants to load it from this jar file. And all the resources in this jar file are available from the ClassLoader.
So you should use
new ImageIcon(MyClass.class.getResource("/res/background.jpg"))
or
new ImageIcon(MyClass.class.getClassLoader().getResource("res/background.jpg"))