Absolute/relative path in java (jar, ide) - java

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"))

Related

Loading image from a Java file

I have this project structure:
ProjectName
src
xxx
co
com
package
something.java
web
image
print
pic.jpg
I want to load the pic.jpg in the java file which will then be used in the pdf file generated. I have gone through the answers here but, nothing helped me yet. Possibly I am missing a small thing.
If the pic.jpg was under package, then getClass().getResource("pic.jpg") works absolutely fine. getClass().getResource("/web/image/print/pic.jpg") doesnt work as well. But I want to place all my images under image folder and refer it in the java file.
You should get the application runtime path and rebase the path to the folder which images locate.
You can use the code:
String path = new File(".").getCanonicalPath();// or System.getProperty("user.dir")
Seems your files are not copied to bin/ directory. Change your build script to copy them to output directory
getClass().getResource() will use class loader to load class, since those files need to be in classpath

Java won't export my resources properly

So I have 2 class folders one is res the other is lib. My res folder has two other sub folders one with images the other with sounds. My lib folder has 4 other jar files and a native folder. It all works within eclipse but when I try to export it as a runnable jar it does not work. I won't won't recognize anything.
I am to call my images I am using ImageIO.read(new File(imagePath)); For the sound I am using the external libraries I mentioned earlier to load and play them.
I am to call my images I am using ImageIO.read(new File(imagePath))
Contrary to your title, this is not an Eclipse problem - it's simply a bug in your code, because your code assumes that the image is stored as a file in the file system, when it's not.
You don't have a file for the image, so you shouldn't use new File. You should instead use Class.getResource or ClassLoader.getResource - or the getResourceAsStream equivalents. That way, it will load the resource from whatever context the class itself is loaded, which is appropriate for jar files. So for example, you might have:
Image image = ImageIO.read(MyClass.getResource("foo.png"));
... where foo.png is effectively in the same package structure as the class. Alternatively:
Image image = ImageIO.read(MyClass.getResource("/images/foo/bar.png"));
where images is a folder within the root directory of one of your jar files loaded by the same ClassLoader. (We don't have enough information to give you complete code here, but that should be enough to get you going.)

Find a resource both in .jar and eclipse with the same String computation

I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to "/bin/stuff/icons/image.png" , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path)
BufferedImage image = ImageIO.read(in)
Where path is
"projectName/resourceFolder/" + nameOfResource.stuff
as found in the src directory of the eclipse project.
E.g.
"myProject/images/icon.png"
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And - no - I don't save images in the bin/ - but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.
Don't put anything under the bin directory in Eclipse: if you run a clean on the project it will be erased.
What you can do is to define a new source folder like resources, and put the image there. This way it will be automatically copied to the bin folder.
If you include the resources folder into the Jar, it will be available in both environments by using something like:
ImageIO.read( getClass().getResource("/image.png") )
PS: You can evade using a different resources folder but mixing the sources and images will quickly pollute your source folder.

putting picture in java program using MyEclipse GUI

I want to add a picture to my GUI program created using Eclipse and MyEclipse (for GUI visual design) from the resource pictures I pasted earlier in the project.
I managed to load pictures that lies just beside the .JAR file using
image = ImageIO.read(new File("imageFile.jpg"));
But I want to use the image from my resources "src" folder directly , so that the .JAR file is a standalone file yet loads pictures nicely.
I tried to make it
image = ImageIO.read(new File("src/ldtlogo3.jpg"));
I use this method when exporting the .JAR file
Java: export to an .jar file in eclipse
Use the overloaded ImageIO.read method taking an InputStream as a parameter, and use MyClass.class.getResourceAsStream() to get this input stream. getResourceAsStream loads a resource from the classpath (and thus from the JAR of your application). Its api doc will tell you which path it expects.
Note that the src directory is used to hold your Java source files. The jar doesn't contain it. It contains the .class files, in a hierarchy which directly maps the package hierarchy. Eclipse will automatically "compile" the image file by copying to the output directory, along with the .class files.

Deploying .jar file: Why can't I load icon files?

I'm exporting a simple java project that includes two directories; src and Icons. Icons is a directory that contains three .png files.
I'm exporting to an executable .jar file using File -> Export. The export works properly and the .jar file contains the Icon directory. But I can't get the correct path for the .png files when the project is deployed. During the development I'm using the following path:
Icons/picture.png
and it works as long as I run from within the Eclipse IDE.
How do I get the correct path for the icons?
Your code is looking for the image outside of the .jar file. Try the URL constructor of ImageIcon instead.
Icon icon = new ImageIcon(getClass().getResource("Icons/picture.png"));
See Class.getResource().
mmyers is correct, but be aware that getClass().getResource() will load resources relative to the package where the class is defined. I suspect your icons are packaged at the root of the jar file and not relative to the class itself. To get resources from the root of the classpath, try:
getClass().getClassLoader().getResourceAsStream("Icons/picture.png")

Categories