Hey I got a problem with getting all my pictures inside a JAR file
This is the code that is bothering me
CImgPanel imgPanel = new CImgPanel(new ImageIcon("img/logo1.png").getImage());
i have tried getClass().getResource and this.getClass().getClassLoader().getResourceAsStream
I have tried everything i could think of and i have look many place before asking here but no one got this problem,does anyone know what i can do to get the pictures inside the Jar?
Thanks in advance
I recommend:
Obtain your images using ImageIO.read(getClass().getResourceAsStream(IMAGE_PATH)) where the IMAGE_PATH is the path relative to the class files. This will return a BufferedImage.
Create your ImageIcon from the BufferedImage if an Icon is needed.
If this still doesn't work, show us the contents of your jar file by doing, jar tf MyJarFile.jar, and then print the results here as an edit to your original question. Also show your actual code attempt and any and all error messages that your code may generate.
Related
First:
I know, this question was asked about 100 times already.
I know, someone already could have gave the right answer.
But anyway, I have to ask this again. I didn't found a solution working for me. sorry.
I'm writing a game in java. Of course I have many packages (folders) with sounds and pictures and so on. But these folders are each of variable size. So I want to save the content of such a folder dynamically in a list.
Usually, I was making this:
File f[] = new File(getClass.getResource("/home/res/").toURI()).listFiles();
Now I can iterate though this file object and save each file. Perfect. Really?
No. When I extract this Project into a jar archive, this fails. All because a uri isn't "hierarchical" or some stuff like this. See this exception:
C:\Users\Administrator\Desktop>java -jar Homework.jar
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at homework.moonface.src.Moonface.loadSounds(Moonface.java:94)
at homework.moonface.src.Moonface.<init>(Moonface.java:55)
at control.Overview.main(Overview.java:16)
Ok, I thought, so I need to get this path and add it manual into the file object. (new File("path"); But... this doesn't work. I'm getting the known error that the input wasn't written correctly, or when i try to cut of "file:" from the resource url, it breaks because in a jar its "jar:file:" and not "file:". But also if I cut of jar:file: I'm getting null.
So, please don't mark this as a duplicate, and try to explain this shortly for me. It would help thousand other, who don't understand other solutions who aren't solutions.
Try this :
URL jarResourceURL = getClass().getResource("/home/res/");
JarURLConnection jarURLConnection = (JarURLConnection) jarResourceURL.openConnection();
Enumeration<JarEntry> entries = jarURLConnection.getJarFile().entries();
while (entries.hasMoreElements()){
entries.nextElement(); // iterate over entries and do something
}
UPD: I was thinking about how spring framework's ClassPathXmlApplicationContext resolves the resources from jars. So i investigated the source code and foud that there is an utility class org.springframework.core.io.ClassPathResource which have a convenient interface (moreover there is a possibility to get the corresponding java.io.File instance using it) and can help you to solve the problem. Here is the doc :
http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/core/io/ClassPathResource.html
I want to load an image which is in my projet folder as : /src/images/URL.jpg
I tried this code :
BufferedImage image = ImageIO.read(getClass().getResource("/images/URL.jpg"));
But I'm getting this error :
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at Personel.PersonnelMainForm.print(PersonnelMainForm.java:464)
How can I solve this problem ?
From personal experience I use:
BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/images/image.jpg"));
I get the resource as a stream and that seems to work fine for me.
You can try this version of read, which takes File as an argument.
BufferedImage image = ImageIO.read(new File("path"));
where path is the path to you file, absolute or relative as you need.
Another option, if you really want to load it as a resource, would be editing your classpath, as per this question.
I suppose you have a java class in the package.
You have to move up so many times as package levels.
Example:
Java class is defined as org.test.MyClass
you have to go up twice (../../) to be in the main directory.
I know there are a thousand answers on the message boards to this question, and I have tried them all and none of them have worked for whatever reason. Here is an excerpt from my code:
ImageIcon icon = new ImageIcon("/home/james/programmingpics/A_Flute");
ImageIcon icon1 = new ImageIcon("/home/james/programmingpics/C_D_Flute");
ImageIcon icon2 = new ImageIcon("/home/james/programmingpics/D_E_Flute");
ImageIcon icon3 = new ImageIcon("/home/james/programmingpics/E_Flute");
ImageIcon icon4 = new ImageIcon("/home/james/programmingpics/F_G_Flute");
ImageIcon icon5 = new ImageIcon("/home/james/programmingpics/G_Flute");
ImageIcon icon6 = new ImageIcon("/home/james/programmingpics/B_Flute");
ImageIcon icon7 = new ImageIcon("/home/james/programmingpics/C_Flute");
ImageIcon icon8 = new ImageIcon("/home/james/programmingpics/D_Flute");
ImageIcon icon9 = new ImageIcon("/home/james/programmingpics/F_Flute");
pretty simple, works perfect while it is on my computer. Whenever I zip the dist folder move the pictures out of the directory on my computer and run the program, I get back empty JFrame's. So I then did the project/properties/sources/add folder thing, compiled it and still the same result. Then I moved the classes folder into the dist folder, tried to change the path and see if that worked, still nothing. It sounds like there is a simple answer to this, but I have clearly missed it.
You use absolute path names in the constructors. Examples of absolute path names are "/home/myusername/folder/foo/bar.png" or "C:\Folder\Graphics\foo\bar.png".
Your code given in your posting always wants to read exactly from the given path at runtime. If you move the graphics to a different folder, you're program won't be able to find them. This way, the graphics aren't included/packed into your program, but they're loaded at runtime (not compile time!) from the path specified in the constructor.
Such absolute paths are very bad practice because they prevent your program from being portable. Other people won't be able to run your program on their computers. Because if you try to run that on a different machine, the code will probably fail, because the path "/home/james/..." might not exist on that computer. Maybe your user doesn't even use Linux or at least his user name is not "james".
So you have to get rid of the absolute paths. Instead, you should take care to include your graphics in your project and have them being put into the JAR archive.
One approach would be putting your graphics into your project's directory structure and then doing something like this:
URL imageURL = getClass().getClassLoader().getResource("images/A_Flute.png");
ImageIcon fluteIcon = new ImageIcon(imageURL);
This, of course, won't work if your graphics stay in your home folder. You have to copy the graphics.
I'm working on compiling all my code into one jar file, but I have many images that I need to include. Java doesn't include them, and I have tried a lot of different methods, but most out there are not suited for the GImage object. Is there a way to include these, or do I need to switch to icons objects?
I can't really understand what the Gimage means. But I can read the image from jar file by below method:
java.net.URL imageURL = getClass().getResource("bomb0.png");
ImageIcon bomb0 = new ImageIcon(imageURL);
And then you can:
GImage image = new GImage(bomb0.getImage());
If you think this method is ok, you can have a test ;-)
I've seen many different examples showing how to set a JFrame's IconImage so that the application uses that icon instead of the standard coffee mug. None of them are working for me.
Here's "my" code (heavily borrowed from other posts and the internet at large):
public class MyApp extends JFrame
{
public MyApp()
{
ImageIcon myAppImage = loadIcon("myimage.jpg");
if(myAppImage != null)
setIconImage(myAppImage.getImage());
}
private ImageIcon loadIcon(String strPath)
{
URL imgURL = getResource(strPath);
if(imgURL != null)
return new ImageIcon(imgURL);
else
return null;
}
}
This code fails down in loadIcon when making a call to the getResource() method. To me, there's only 2 possibilities here: (1) the myImage.jpg is in the wrong directory, or (2) getResource() doesn't like something about my image (I had to convert it from CMYK to RGB in Photoshop so I could use the same image elsewhere with ImageIO.)
I have used the System.out.println(new File(".").getAbsolutePath()); trick to locate the directory where the image JPG should be stored, and still nothing worked. I have subsequently placed the JPG in just about every directory inside my project, just to rule file location out as the culprit.
So that leaves me to believe there's something that getResource() doesn't like about the JPG itself. But I have now already exhausted my understanding of images and icons in the mighty, wide world of Swing.
My JPG loads fine in other image viewers, so that's ruled out as well. Anyone have any ideas?
Thanks in advance!
I have tried following which was a answer for same kind of question like yours. And it works for me.
Try putting your images in a separate folder outside of your src
folder. Then, use ImageIO to load your images. (answered Aug 27 '13 at 0:18
AndyTechGuy)
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
put the image in the root of the classpath and say getResource("classpath:myimage.jpg");
The problem with your code is that jvm is unsure where to lookup the image file so its returning null.
Here is a nice link about classpath
It should be
if(imgURL != null)
^
instead of
if(imgURL !- null)
and
URL imgURL = this.getClass().getResource(strPath);
instead of
URL imgURL = getResource(strPath);
Then it works fine, if "myimage.jpg" is in the same dir with MyApp.class
Two suggestions:
Try using the getClass().getResource("x.jpg"), and putting the file directly in the same folder as the .class file of the class you are in.
Make sure the name is identical in case - some operating systems are case sensitive, and within a JAR, everything is case sensitive.
You can try to use a "/" before your filename.
getClass().getResource("/myimage.jpg")
If you look into your build-output folder (target) you can look for your class where you are trying to get your resource from.
Your resources will probably be copied in some folders above.
For example your target directory could look like this:
target
|- de.example.app
|- Main.class
|- Main-x.y.z.jar
|- myimage.jpg
So if you just go for getClass().getResource("myimage.jpg") it will look under the folder target/de/example/app and won't find a jpg there.
You need to tell him that you want to look under the root-folder (target/**). That's why you need to place a "/" before your file.