How can I access the images folder from within my src/ folder? - java

I am trying to access images that I have stored within an images folder in vs code. Prior to having these folders the images would show as they were in the same directory as my .java files, however, since moving the code to a src/ folder and images to a images/ folder for use in intelliJ, I am now unable to see the images appear on the program when running through vs code. Is there anyway I can access the images folder from the code within the src/ file?
Above is the directory's.
I have tried new ImageIcon("./images/example.png") however this does not seem to work
I also tried with two dots but that also did not work

Related

How to include some folder in target but not in out folder in IntelliJ IDEA

Surely there will be an answer to my question but I've been trying for days and I can't in any way.
This in the picture is the structure of my target folder. I would like to make the folders: image and video not be placed in the .jar file once the file is built but be placed as external folders in the same location as the .jar file.
However, I would like the folders to remain in the target file to allow me to access them via class.getResource during the test phase but once I have obtained the .jar file I have to be able to change these images at will and I don't want them to be present inside the .jar.
I hope I made myself clear.
I tried something inside the project structure but with no result
Structure folder

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");

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 export JAR and maintain directories

I have a resource folder where I keep images for my project.
So my structure ends up being
Project
-src
-bin
-res
Res is the folder that contains my images, and in my code, images are referenced like so.
"res/image.jpg"
That works fine when I run my project from within Eclipse, however if I wish to export it to a runnable JAR file, it puts all of the images into the root folder within the JAR. Is there any way to make it put the images in a res folder within the JAR?

Java application wont load images after exporting

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")

Categories