Java application wont load images after exporting - java

I have problem with eclipse, after i export my application into jar or runnable jar file app doesnt load images.When i run application inside eclipse it runs fine all images loaded properly.I tryed to move my source folder inside default src folder which doesnt helped. Also tryed to import all images one more time,Refreshing project , changing code to getClass().getResource("imgs\someimage.png") , making a new folder and moving everything there,also copy/paste whole classes into another project.Wierd thing is that i have same style in another project and everything work just fine!Bud in this case i cant get it to work.Build path is set perfectly just like project before.And all the images that i need after exporting into jar i see inside it.I use example: labelIcon.setIcon(new ImageIcon("imgs\\someimage.png")); for most people getClass().getResource()... fixed the problem , bud in my case not. Any ideas??Might this be a bug in Eclipse?

Instead of creating imgs folder, create package instead (in src folder). I.e. org.imgs. And then load images as resources:
getClass().getResource("/org/imgs/nameOfImage.png")

Related

Open Image, thats inside of a .jar file

i know that this question was asked several times before but none of the solutions quite work for my problem (or i just dont know how to adjust them properly).
I try to program an application, that is supposed to open an image, which is located inside of the jar file of the application.
The jar file is created by maven, so originally the picture was in the src/main/resources directory of my maven project and it will finally be in the base directory of my jar file.
The program itself consist of two java files and an fxml file. The one java file is my main class (called imageviewer.java) and the other java file is my javafx controller (called Contoller.java).
The method, that is supposed to open the picture is in the Controller.java file.
The solutions I found were all using getclass().getResources... , but in this case it does not work (maybe because it is not a jar file that will consist of a single class). The name of the final maven-generated jar file will be imageviewer-0.0.1-SNAPSHOT.jar.
How can I access the image inside it?
Okay I did actually find the solution.
In Eclipse I was only able to acess the image while it was in the same folder as the java documents. After I moved it into the "Resources" folder (which is where it belongs in a maven project) i was not able to acess it with a relative path.
All I had to do was creating a new package inside of the resources folder that is called exactly the same as the package in which the java files are.
So my project structure now looks like this:
src/main/java/myPackageName/MyJavaFiles.java
and
src/main/resources/myPackageName/Imagename.jpg
After that i was able to acess is with getClass().getResourcesAsStream("Imagename.jpg");

JavaFX, can't get icon on the title bar to show up after exporting to *.JAR

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

In Java where do I put my sprite files to make this open source code work?

I am a beginner and I am trying to learn by messing around with some open source game code.
I was setting it all up in Eclipse but I don't know where to put these sprite gif files.
In the code I found this:
URL url = this.getClass().getClassLoader().getResource(ref);
And when I put all the class files under a java project together and tried to run I got this error message:
Can't find ref: sprites/ship.gif
Of course the code came with sprites including ship.gif. I just don't know where to put it. I tried making a folder under the java project called sprites and putting it in there.
I don't have a res folder.
The this.getClass().getClassLoader().getResource method look for relative path of the data from the package of the class.
Assuming your class is com.my.package.MyClass, you usually have your project organized containing at least in your case:
com/my/package/MyClass.java
com/my/package/sprites/ship.gif
Most of the time, you certainly have a resources or a images folder for your java project.
You can only load those resources if they are on the classpath. Try to add the sprites folder as a source folder on your Eclipse build path and try again.
Put the .gif in the jar.
This can be achieved by creating dedicated folder you add in the sources of eclipse. Often this folder is called "resources".
Be ware that in some cases a "/" is required at the begining of ref.
Thanks everyone.
How I fixed my problem:
When I downloaded the source I opened folders and went to the class files and copied them to my project and then I was trying to do the same thing with the sprites. What I didn't realize is that if I just drop the two main folders "org" and "sprites" into the source file of my project then it all works on its own. These two folders were the first things I had after downloading this open source code.

Eclipse LibGDX folder sharing

(4/15/2014 Still no working answer to the question)
I used gdx-setup-ui.jar to create my Android/Desktop/HTML5 program which I imported into Eclipse.
myprogram
myprogram-android
myprogram-desktop
myprogram-html
My program runs fine on desktop and android, but when I run it as html I get an error if all of my classes are not in the same myprogram>src folder, if I put it in a sub folder in src then the html5 does not seem to access the class. If I take my classes out of the folder and put them in the root of myprogram>src they work. How can I fix this?
I also notice that when I do a symbolic link to the asset folder manually(without gdx-setup-ui.jar) my Java application will not find the pictures etc unless I physically copy the asset folder twice. I made sure the folder path was exactly the same and it still wouldn't detect it unless I made a second copy of the pictures. Strangely the gdx-setup-ui.jar does this symbolic copy and yet it works for some reason.
This tutorial explains how to do it in Eclipse. And for completeness sake, here is how to do it using ant and IntelliJ. I hope this was useful. I will try the Eclipse one now...

Eclipse Executable Jar Resource Files

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.

Categories