Java won't export my resources properly - java

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

Related

Can I have my entire application in a single JAR file?

I don't quite understand how creation of a Swing application really works.
I have created a simple application that gets a background image from a path.
ImageIcon imageIcon = new ImageIcon("C:\\Java\\ApplicationName\\out\\Resourses\\backGround.jpg");
Image image = imageIcon.getImage(); // Создание картинки из него
Image temp = image.getScaledInstance(500,500,Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(temp);
g2d.drawImage(temp,0,0,null);
It works, so I decided to make a JAR file independent from changes in the Java folder (like deleting this image) and put images in src - images ( package ). I don't know if it is possible though.
I started to get background image like this.
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/images/backGround.jpg"));
The program works perfectly in IntelliJ IDEA but if I extract it to JAR file I get a blank background screen. Why is that?
And can I have my entire application including images in a single JAR file? One that is not dependent on other folders?
You can certainly put the image and the code in a single jar file. The image is not loading either because it's not in the jar file, or because it's not at the path /images/backGround.jpg inside the jar.
One helpful thing about jar files that you may not know: they are in .zip format, so you can use any zip tool to see what is inside the jar (temporarily renaming the jar file to .zip might make this easier). IF the image is not there, fix your jar build (you didn't say how you are making it, so not sure what you need to change). If the image is there but under a different path, then fix the jar build to put it in the right place, or change the path you are reading it from.
Yes definitely you can include your entire application into single jar file i.e. Runnable jar file. But i would suggest you not to include your image inside the jar file, instead pass that image path to an Property file and use that property file in your code for image path. So that even if next time you need to change the image you just change the path in your property file and you dont have to export your entire project again.

Is a 'res' class folder still necessary in Java when loading images?

I'm working on a game, and I've stumbled across a fews ways to load an image into my program. There are two common ways:
The first (No class folder needed):
ImageIcon ii = new ImageIcon("image.png");
Image i = ii.getImage();
When using this method, you simply add a folder to your Project Folder (In Eclipse) and DO NOT have to make it an external class folder.
The second option (Class folder needed):
Image i = ImageIO.read(ImageLoader.class.getResource("image.png"));
You can only load images this way if you made a resource folder (typically called 'res') and added it as an external class folder to your project.
My question is why would you use the second option instead of the first? From my limited experience, it seems like the first option works just the same as the second, just without having to turn the folder you're adding it to into an external class folder.
The first option doesn't work if you package your application as a jar. The second option works in both cases, as an exploded application in an ide or on the hard drive and packaged in a jar file.
new ImageIcon("image.png"); assumes that the image is stored on the file system (in your case, in the current working directory), which raises up some issues - as the working directory isn't always the same location that the jar is stored.
ImageIO.read(ImageLoader.class.getResource("image.png")) embeds the image within the Jar file which resolves the issues associated with the first method as the image is where ever the Jar file is.
It also simplifies installation, as you only need to track the jar files and not all the other "external" files.
ImageIcon doesn't report the reasons it fails. It loads the images in a separate thread, which make diagnosing issues difficult.
ImageIO loads the image in the current thread (and returns a fully realised image) but will also generate a IOException if the image fails to load for some reason, making a much better way to load images in general. It also supports (or has the capacity to support) a wider range of images.
In general, using ImageIO and embedded images is simpler and easier to work with and is generally the recommended approach for fixed resources

images not showing up when running JAR

I've spend 5 hours already and have no idea why images are not loaded when running Jar.
Project structure:
Blackjack_Game
- Source Packages
- Images
- blackjack
.... classes...
At project properties I have src - Source Packages folder added by default. Tried to put images directly into the project's folder and removing /Images/, but still no help.
Inside the code I have:
dealer_url = getClass().getResource("/Images/4_of_hearts.jpg");
File img = new File(dealerCardGenerator.dealer_url.getPath());
BufferedImage bufferedImage = ImageIO.read(img);
dealerCardGenerator.imageIcon = new ImageIcon(bufferedImage);
So inside NetBeans IDE everything is OK. Fully working. But after built&clean I see no images, but all actions are done.
Can you please suggest what is wrong? Getting so mad because of this ((((
A jar entry is not a file. java.io.File can only identify a file, not a jar entry. java.net.URL however can identify a jar entry using the jar: scheme, or a file using the file: scheme. That's why Class.getResource() returns a URL not a File.
Two solutions:
use the URL: ImageIO.read(getClass().getResource("whatever"))
ImageIO can also use an already-opened stream, which classloader can provide:
ImageIO.read(getClass().getResourceAsStream("whatever"))
Duplicate of at least #2-4 of the questions autosuggested as related:
Including Images with an executable jar
Loading images in a jar
Getting images from a .jar file
Thank you all for clarifying the problem. I've realized why some functions were not working.
So the solution is to replace URL with ImageIcon:
ImageIcon i2 = new javax.swing.ImageIcon(getClass().getResource("whatever"));
and then just set icon as label icon:
jLabel1.setIcon(i2);
In this way JAR works. Previously, all methods after this icon set operation were not working, as the program was actually stopped, now everything works fine.

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.

Categories