This question already has answers here:
Add image to JAR Java
(7 answers)
Closed 9 years ago.
I would like for the user to be able to choose from my pre-selected images within the program. I believe it would be similar to a text file within the program, however, as of right now it will only work with an image saved on to my computer. To specify, if I were to send my program to somebody, it would not function properly because they do not have the same photo I am using. I need the photos saved within the program. How would I go about dong this? Would jar files be the way to go?
Thanks!
Yes, jar files could work great for this. But if you use jar files, just remember that the images are not present as Files but rather as resources since Java doesn't see files within a jar. So don't try to read the image into the program via new File(...) but rather SomeClass.getResourceAsStream(...)
i.e.
BufferedImage myImg = ImageIO.read(getClass()
.getResourceAsStream("/images/myImage.jpg"));
Yes, a .jar is what you need. It contains all your files in a runnable form.
http://docs.oracle.com/javase/tutorial/deployment/jar/
You could either package the file into the program, which could be accomplished with a jar file as you mentioned. Jar files are archives that can hold classes, metadata, resources (such as images) and anything else you want.
An alternate way to go would be to upload the images to a site/server which you could then provide a URL to, such as your own personal site/server or an image hosting site. I'd go with a jar file though, as this approach would require an internet connection to function.
Related
I am making a Cluedo boardgame in java and I cannot get my images to show up when converting it to a .jar file.I am adding the images on top of one another so I used JLabels(new ImagIcon ("..")) like so.To add the images and now the images can't be seen when I create a .jar file. Just wondering if there is any way to do without editing my code a significant amount. I have read all stackoverflow answers on this issue and none of them solves this particular problem. Here is what the board game looks like when I run it in Eclipse.
When I convert it to .jar file it no longer displays the board or any of the players.
ImageIcon(String) expects that the String value is "file" reference to the image residing on disk.
If you have externalised your resources then you need to ensure that the path you are using is correct. Remember, the "working directory" in which the Jar is executed isn't always the same as the directory that the Jar is stored in.
To this end, it's generally recommend to "embedded" the images (and other resources) within the Jar itself. This way you can simply perform a lookup for the resource regardless of where it's installed.
The means by which you embedded resources is slightly different for each IDE, but it basically requires that the images are included in the Jar and a specific location/package.
An important note, when embedded in the Jar, the resources can no longer be referenced as if they "files", because they're not, they are part of the Jar/Zip archive and need to be referenced in a different manner.
To load embedded resources you need to use Class#getResource, which returns a URL or Class#getResourceAsStream which returns a InputStream.
In most case, the first is enough. While ImageIcon does take a URL, it is generally recommended to use ImageIO.read, have a read through Reading/Loading images for more details.
The advantage of this is two fold:
It blocks until the image is read, meaning the image is fully realised when the method returns
It throws an IOException when the image can't be read, which is way more meaningful, as ImageIcon fails silently
So, all that would accumulated down to something like...
new JLabel(new ImageIcon(ImageIO.read(instanceOfMyAwesomeGameObject.getResource("/path/to/resource/ResourceName.png"))));
I think it comes from the path to your images. The image path for sources is probably not the same as for .jar
I have an app that accesses words from a csv text files. Since they usually do not change I have them placed inside a .jar file and read them using .getResourceAsStream call. I really like this approach since I do not have to place a bunch of files onto a user's computer - I just have one .jar file.
The problem is that I wanted to allow "admin" to add or delete the words within the application and then send the new version of the app to other users. This would happen very rarely (99.9% only read operations and 0.1% write). However, I found out that it is not possible to write to text files inside the .jar file. Is there any solution that would be appropriate for what I want and if so please explain it in detail as I'm still new to Java.
It is not possible because You can't change any content of a jar which is currently used by a JVM.
Better Choose alternate solution like keeping your jar file and text file within the same folder
There are examples for JMH here. They are present as multiple links within the directory samples/. I can right click on the link and save the files one by one. Is there any way to save all of them at once? Rather than using any download managers! Can we use Java URL class with regex to open the similarly named files and save it?
I have some questions regarding external folder use within Java,
I would like it so that when you load the .jar file, it creates external folders, with the names "resources", and I was wondering how I would go about doing that?
Another question is that I would like to know how to download files from my FTP server (ftp.connorwright.uk) into the "resources" folder, which will lead me onto my next question
My last question is how can I reference these external folders within my code, without any IOException errors or anything
I have tried using the CommonsIO library, but that didn't seem to help.
Thank you.
Your first question is easy
File folder = new File("path\to\your\folder\resources");
folder.mkdir();
You can replace "mkdir" with "mkdirs" to create the entire path of folders in case it doesn't exist
Your second question is a bit complicated, but this website has a great tutorial on it
http://www.codejava.net/java-se/networking/ftp/java-ftp-file-download-tutorial-and-example
And lastly, the only way you can avoid IOExceptions is if the files you are looking for actually exists, there's no universal trick.
Hope this helps
I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:
C:\java\pacman\src
contains all of the .java class files.
C:\java\pacman\assets
contains about 4-5 images and audio files.
If I try to use the following code:
Image someFile=getCodeBase().toString() + "file.png";
The result of getCodeBase() is
file:/C:/java/pacman/bin/
However the following code fails to load:
img=new ImgHelper(getCodeBase().toString() + "assets/");
ImageIO.read(new File(img.getPath("pacman.png")));
Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:
file:/C:/java/pacman/bin/assets/pacman.png
saying:
Can't read input file!
But the url it gave opens fine if I paste it into run and hit enter:
So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:
public ImgHelper(String dir)
{
//this.imgDir=dir;
imgDir="C:\\java\\pacman\\assets\\";
}
Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?
Thanks...
Why not put it all in a JAR file and then call Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
There are two possible solutions that would work:
The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.