I have an application in netbeans with a folder in the same directory as the application containing all the images, and I also have an attribute String in the class where I save the absolute path when I upload an image.
How can I obtain the relative path of an image inside the same jar to store it in a string?
Because then I create the image from the URL string.
Edit:
I have a jar including a logic package, a presentation package (with swing) and an image package. In the logic I've tried to do:
String img = new String("\src\Images\Image.jpg"):
ImageIcon icon = new ImageIcon(img);
Icon imagen = new ImageIcon(icon.getImage().getScaledInstance(photo.getWidth(),photo.getHeight(),Image.SCALE_DEFAULT));
photo.setIcon(imagen);
Recommendations:
Never create a new String with the new String(...) constructor. There's no need to do this, and you just waste resources unnecessarily. Instead simply use a String literal.
Don't use Files to get images held inside of Jar files. Jar files do not hold Files.
By passing a String into your ImageIcon's constructor, you're doing just that (check the API), you're trying to get the image as a File.
Instead use ImageIO.read(...) and a resource to get the image.
The resource path will all depend on your Jars structure, something you have yet to show us.
e.g.,
String path = "\Images\Image.jpg"; // this path may not be correct
InputStream imgStream = getClass().getResourceAsStream(path);
BufferedImage img = ImageIO.read(imgStream);
// process your image here if desired
ImageIcon icon = new ImageIcon(img);
Related
Currently, the way I am referring to my images are by using the lines of code:
static ImageIcon img1 = new ImageIcon("src\\img1.png");
static ImageIcon img2 = new ImageIcon("src\\img2.png");
However, when I export this from eclipse into a .jar file, the picture does not show up. Is it possible to refer to the image without setting the complete file path (ex. ("C:\XXXX\XXXX\XXXX.img1.jpg)), because if I put the .jar on a flash drive, the complete file path would be useless?
On the package contains image create any empty class Example : ImageLoader
When you want to load image :
ImageIcon img1 = new ImageIcon(ImageLoader.class.getResource("img1.png"));
ImageLoader.class.getResource : will return the URL of ImageLoader class
I've placed the image TestIcon.png in the src folder of a Java applet (the directory is C:\Users\User\workspace\applettest2\src\TestIcon.png). Everything I've read says that I should be able to use the image simply by referencing "TestIcon.png". Example:
ImageIcon xIcon = new ImageIcon("TestIcon.png");
Howerver, running the applet in Eclipse with this code doesn't work; the image is not displayed. If I type out the image's entire path:
ImageIcon xIcon = new ImageIcon("C:\\Users\\User\\workspace\\applettest2\\src\\TestIcon.png");
then the applet displays the image properly. Isn't the default path for resources supposed to be in the "src" folder? Thanks.
If you are running it inside the Applet then try with Applet#getCodeBase() and Applet#getDocumentBase
Image image = getImage(getDocumentBase(), "TestIcon.png");
Find more samples Here and Here
I want to reference an image in my project that I will package into a Jar file.
This code is not working:
ImageIcon image = new ImageIcon("Images/buttonBackgroundSelected.png");
Any ideas? thanks
This constructor only works if the image is available in the file system outside the JAR file
new ImageIcon("Images/buttonBackgroundSelected.png");
You almost never want to do this. You could use:
ImageIcon imageIcon =
new ImageIcon(getClass().getResource("/Images/buttonBackgroundSelected.png"));
when the folder and image and the have been included in the JAR file.
However, loading images this way fails silently if any issues loading the image. Therefore use ImageIO.read should be used:
Image image =
ImageIO.read(getClass().getResource("/Images/buttonBackgroundSelected.png"));
The resultant image can be wrapped in an ImageIcon if required.
Alright, so I have created a Buffered Image to display on a JLabel. When I display the File location, it requires a src/ in front of the folder I want to access, or else an error will arise and I will not see the Buffered Image... I know that if I put the 'src/' in front of the resImg, the BufferedImage will not display outside of the IDE. Can anyone help?
This is the code that works inside of the IDE. When running outside from the .jar file, the image doesn't display.
static File f = new File("src/resImg/banner.png");
try {
banner = ImageIO.read(f);
picLabel = new JLabel(new ImageIcon(banner));
LabelPanel.add(picLabel);
} catch (IOException ex) {
Logger.getLogger(GameStart.class.getName()).log(Level.SEVERE, null, ex);
}
When the resource is in a jar file, it isn't a file on the local file system. You should use
URL imageSource = getClass().getResource("/resImg/banner.png");
if (imageSource == null) {
// Handle the resource being missing
}
picLabel = new JLabel(new ImageIcon(imageSource));
Here you're fetching the resources from the classloader, which will obviously have access to the files in your jar file.
If you want get image from jar, use smth like this:
URL imageurl = getClass().getResource("/images/imagename");
You cannot create a File object for a resource in the JAR file. Use this instead.
URL imgurl = this.getClass().getClassLoader().getResource("resImg/banner.png");
picLabel - new JLabel(new ImageIcon(imgurl));
Hope this helps.
You have to find a parent directory, the relativ path can start from like
workspace directory
directory the process starts
the relativ path is "realtiv" from where the java process is started from, so the path can change from time to time.
If the picture is placed in the jar, you can use
getClass().getResource("relativ jar path")
for finding the image. the jar itself knows, where it is placed.
Most probable cause is that your image is not packed inside jar file ( verify this with jar / zip tool ).
If image is there but does not display, it is because you trying to read this from file system relative to working directory ( and it is certainly not there )
So, proper solution would be to read image from class path:
Thread.currentThread().getContextClassloader().getResourceAsStream(... path to your image relative to jar root ---)
I have an eclipse project with image folder. I need to create an ImageIcon from an image located in this folder but can not figure out how to tell it to look in the folder with out giving exact position on my system which I do not want to do as the project gets moved around to different pc's.
Code example
ImageIcon ai = new ImageIcon("/somedir");
Thanks for the help.
you have to store the image folder into your projectand then:
java.net.URL imageURL = this.getClass().getClassLoader().getResource("path_to_image_folder");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
The path to the image folder can be relative to your class or absolute (starts with "/").
You can take a look here: How to use icons
It's very easy (remove the backslash):
ImageIcon ai = new ImageIcon("somedir");