I have a simple program that needs to display images.
I know how to do this running the code from Eclipse and I know how to do it running from a JAR file, but I'd like a solution that works in both cases.
Eclipse project is as such:
- Project (java)
- src
- controller
- Main.java
- ui
- Display.java
- images
- image.jpg
The code snippet that works from within Eclipse:
ImageIcon image = new ImageIcon("images/image.jpg);
The one that works for a JAR (all in a single JAR file):
java.net.URL imgURL = getClass().getResource("/images/image.jpg");
ImageIcon image = new ImageIcon(imgURL);
What would I need to change in order to get a single piece of code that works in both situations?
Put the images folder inside the src folder, and Eclipse will copy the images into the target folder (bin or classes, generally), which will make them available from the classpath, just as if they were in your jar in the released version of your app.
getResource() doesn't look in a jar. It looks in the classpath of the classloader. Whether the image is in a jar or not is thus not important. It must be in the classpath. And obviously the target folder of eclipse (bin or classes, generally) is in the runtime classpath of the app when you launch it from Eclipse.
Related
Every time I export my project into *.jar in eclipse I lose all of images in my project (in Eclipse). I've tried to put images directly into the *.jar and it works in some other cases but not this one.
Fragment of code with loading of this icon:
primaryStage.getIcons().add(new Image("file:icon.png"));
You're loading the image from the working directory, not the jar itself. This is why it's working in development, and not in production. If you move the image to the same folder as the jar is located it should work, but I'm assuming you want the image to be contained inside your jar file.
Move the image to one of your packages (any, as long as it gets bundled with the jar). Then you need to access the image, use the ClassLoader.getResourceAsStream(String) for this. It looks like you're using javafx, so you can use this to get your image: primaryStage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("the/path/to/your/image.png"));.
to work with the files inside the jar, need to use ZipInputStream or JarInputStream
I have a fairly simple Java program going with the following structure:
/src/main/java/ (all the classes in here)
/src/main/resources (marked as resource root in IntelliJ, one png file in here)
/pom.xml
The way I build my Maven project is a bit of a workaround (because i couldn't get IntelliJ's artifact system to work). I have a run/debug configuration that builds the project using Command line: install (hope I'm explaining that sufficiently well).
Everything works fine, except for the inclusion of the .png file.
The weird thing is that the file is actually added to the executable jar, but somehow the final program doesn't appear to use it at all. It's accessed via:
BufferedImage image = ImageIO.read(new File(getClass().getResource(url).toURI()));
Again, the final program works perfectly fine, except that the image is not visible at any point. Any help?
Edit: The png is located in the root folder of the jar, and the supplied URL is a string which is simply the name of the png file. I should probably add that the program works fine in the IntelliJ-Run-environment (including the png), but not using the executable jar.
Found the answer that worked for me here:
Access .png image in .jar file and use it
Had to use
img = ImageIO.read(MapObject.class.getResource(url));
instead of
ImageIO.read(new File(getClass().getResource(url).toURI()));
since the image was located in a jar.
I would like to export my Java program, but I have run into problems. It exports fine but my resource files like my res folder and my sound folder aren't there. They are located in the project directory, so I don't know if that should be a problem. I am currently just using ImageIcon().getImage() to get my pictures and that works fine but when I export and run it the program runs but it doesn't have any pictures. Any help would be greatly appreciated
The problem is that once the images/sounds are inside the jar you must access them as a resource.
Image image = new ImageIcon(this.getClass().getResource("/image.gif")).getImage();
Make sure the images are really inside the jar opening the jar with winrar or similar if not add the folders as source folders in your buildpath.
I have 2 Java projects, one is a Web Project in NetBeans and the other is a Java Project in Eclipse.
Just to know, the Java Project from Eclipse is used by the Web Project in NetBeans as a library (.jar)
Situation:
1. Java Project from Eclipse has the following structure:
And inside the src, there is a class that uses the file1, file2, etc in some method using global variables like this:
public static final String PATH_ONE = "./files/file1.xml";
public static final String PATH_TWO = "./files/file2.xml";
...
Finally, when I test the method in some main() class everything works good.
2. Web Project from NetBeans has a jar reference to the above library and If everything works good, then the web project will be able to execute the method that uses the global variables without problems from the library project.
Problem:
When I run the web project and I want to invoke the method from my library that uses the above xml files from it, for some reason, NetBeans or the project (I don't certainly know) looks for the path and fails in Exception because the path can not be found (It seems that tries to find the path in the web project and not in the library one).
How can I solve this issue? It sounds simple but I don't want to change my structure, load them as a resource or transfer files from one project to another and use external paths to make this work because I just make a recreation of the situation but I am working with lots of files with different folders and paths too.
If you unzip your jar you can't find your folder file because not is included in your classpath.
You can See .classpath file and the folder isn't. For these reason is FileNotFoudnException.
You can to add how source folder in Eclipse ID:
1. Right click in your project/Build Path/ New source folder
Create the source folder . Then you can add a new package with the name Folder.
Your project:
src
resources
folder
resource1.xml
resource2.xml
2. Modify your java project.
public static final String PATH ="/folder/resource1.xml";
Now, if you try to unzip the jar, you can see that the folder was added in the .classpath.
It works for me. I tried to paste image but i'm new user.
I have been browsing Stackoverflow all day looking for how to do this and I have not been successful yet
I am packaging a quick game I made into a executable jar but I didnt reference the images correctly I just referenced the files
background = ImageIO.read(new File("wood.jpeg"));
I have my classes in src default package
Im not sure where I should add the images or if I have to add it to the build path
or correct way of adding the images to the build path in the newest version of eclipse
Files in a Jar are not files in the sense of a file on disk. They are simply a (possibly) compressed stream of bytes.
Java makes it easy to extract these "resources" from Jar files through the use of the ClassLoader
background = ImageIO.read(getClass().getResource("/wood.jpeg"));
Should work...
This will return a URL which ImageIO can use to load the resource.
You could also have a read of
Classpath resource within jar
Jar get image as resource
Load a resource in Jar
And I could list some more. So, yeah, it gets asked a lot ;)
Try using Constructing Runnable Jar using the Eclipse.
R_Click on the Project in the Package Explorer --->
Export ---> Runnable JAR file --->
Select the option of Package required libraries into generated JAR