Eclipse export isn't showing images properly [duplicate] - java

When running a Java app from eclipse my ImageIcon shows up just fine.
But after creating a jar the path to the image obviously gets screwed up.
Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?
I'd like to distribute a single jar file if possible.

To create an ImageIcon from an image file within the same jars your code is loaded:
new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))
Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.
To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.

You can try something like:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
In your JAR file, you might have a directory structure of:
MyJAR.jar
- com (class files in here)
- images
----image.jpg

This is working for me to load and set the content pane background image:
jar (or build path) contains:
- com
- img
---- bg.png
java contains:
JFrame f = new JFrame("Testing load resource from jar");
try {
BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
e.printStackTrace();
}
Tested and working in both jar and unjarred (is that the technical term) execution.
BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.

In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:
src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
file.java
Resources
image.jpg
The code should be like:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));

Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).
This works for me
image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
lblhabitat1.setIcon(new ImageIcon(img));
if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling

Related

When I run my code in IDE, it loads textures, but when exported, it doesn't [duplicate]

When running a Java app from eclipse my ImageIcon shows up just fine.
But after creating a jar the path to the image obviously gets screwed up.
Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?
I'd like to distribute a single jar file if possible.
To create an ImageIcon from an image file within the same jars your code is loaded:
new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))
Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.
To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.
You can try something like:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
In your JAR file, you might have a directory structure of:
MyJAR.jar
- com (class files in here)
- images
----image.jpg
This is working for me to load and set the content pane background image:
jar (or build path) contains:
- com
- img
---- bg.png
java contains:
JFrame f = new JFrame("Testing load resource from jar");
try {
BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
e.printStackTrace();
}
Tested and working in both jar and unjarred (is that the technical term) execution.
BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.
In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:
src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
file.java
Resources
image.jpg
The code should be like:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).
This works for me
image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
lblhabitat1.setIcon(new ImageIcon(img));
if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling

Java: Reading in an image from same folder as java files

I have code that reads in an image file from the same directory as the java files, but currently I can only get it to work if the entier path is given.
picture = new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\...\\ogre.png"))));
The image is in the same folder as the java files. But when I try just "ogre.png" or ".\\ogre.png" or similar it does not work.
My question is this:
I will be exporting to a JAR eventually, will this affect that once the jar is created? (I'm assuming yes, since creating a jar doesnt change the source code).
How can I read the file from the same folder instead of the exact file path, In a situation where the containing folder were to be moved for example.
This is the standard way... (and it will work when everything is in the jar)
URL url = OneOfYourClass.class.getResource("package/pathTo/image/ogre.png");
ImageIcon image = new ImageIcon(url);
OneOfYourClass is a class that you have (maybe the main class)
package/pathTo/image/ is the path from this class to find your image

Java - How To Set An Image Path Without Using The C:\\ Direcory

I created a java app and I put some images in it and even gave it an image icon as the desktop image, but when i made it a jar file, and put it on another pc, all images were gone. This is the image path :
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
I checked it online and i found out that the problem is that i got the file through the C:\\ directory, they said the image file should look like this :
File imageFile = new File("res/icon.jpg");
I tried this but it didnt work, I kept getting an error message like : file not found
This is my full code :
BufferedImage image = null;
try {
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
image = ImageIO.read(imageFile);
} catch(IOException e) {
e.printStackTrace();
}
setIconImage(image);
Please, i have been trying to solve this for weeks, do anyone know how i can solve this, please if you do, please help
The images should not be loaded from the file system, but should be bundled into the app, inside your jar.
If you put the image foo.png inside the jar, under the package com.bar.resources for example, you simply need to use
InputStream in = getClass().getResourceAsStream("/com/bar/resources/foo.png")
to load the image as an input stream.
That will use the class loader to load the image. So, during development, if you're using a standard IDE project, you just need to put the image file in the appropriate package in your source directory: the IDE will "compile" the file by copying it to the same directory as the generated .class files.
If you're using a standard Maven/Gradle project, then it needs to be put in the appropriate package under src/main/resources.
Problem is you dont have res folder in your project, first of create a folder a "res" by Right Clicking on project and place the image icon.jpg image in that "res". and
use : `File imageFile = new File("res/icon.jpg");`// To retrieve image to your project.
You cannot retrieve the image from your local system, because it is not attached with your projects workspace.
There are two options....
1)Either make res a "source folder" . You will know this if you are using Eclipse.
Then you can use like
ImageIO.read(new File("res/icon.jpg"));
2) If res is a normal folder, you will have to use. In this case res will be considered as a package
ImageIO.read(new File("src/res/icon.jpg"));

resource path setting java jar

I have a folder and package structure looking like this
XXX
src
view(classes in here)
rsrc
view(images in here)
The class files, are using images of the image folders. While building a jar i dont have a problem with inserting images, i just copy (in my build.xml) the rsrc folder content to the src folder build, so the resources are in the .class path:
<copydir src="./rsrc/studentapp/view/"
dest="${build}/studentapp/view/"/>
After creating my jar I can get resources by (example):
URL resource = this.getClass().getResource("testimage.png");
The problem is, if i just want to compile a class without creating a jar, the above line wont work because it's not in the right folder. Is there a way to read out a file relatively back this order structure and also have it to work in the jar too?
best regards
EDIT: Working with windows maybe there is a different syntax
Solved it by just setting path relative to rscrc and copy the whole rsrc folder to the jar build
String path = "rsrc\\studentapp\\view\\testimage.png";
File test = new File(path);
try {
BufferedImage image = ImageIO.read(test);
ImageIcon icon = new ImageIcon(image);
JOptionPane.showMessageDialog(null,
" <- Image should appear here", "Testimage",
JOptionPane.INFORMATION_MESSAGE, icon);
} catch (IOException e1) {
e1.printStackTrace();
}
and here the line in the build.xml:
<copydir src="./rsrc" dest="${build}"/>

Java Swing: Displaying images from within a Jar

When running a Java app from eclipse my ImageIcon shows up just fine.
But after creating a jar the path to the image obviously gets screwed up.
Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?
I'd like to distribute a single jar file if possible.
To create an ImageIcon from an image file within the same jars your code is loaded:
new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))
Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.
To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.
You can try something like:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
In your JAR file, you might have a directory structure of:
MyJAR.jar
- com (class files in here)
- images
----image.jpg
This is working for me to load and set the content pane background image:
jar (or build path) contains:
- com
- img
---- bg.png
java contains:
JFrame f = new JFrame("Testing load resource from jar");
try {
BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
e.printStackTrace();
}
Tested and working in both jar and unjarred (is that the technical term) execution.
BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.
In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:
src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
file.java
Resources
image.jpg
The code should be like:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important).
This works for me
image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
lblhabitat1.setIcon(new ImageIcon(img));
if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling

Categories

Resources