Open Image, thats inside of a .jar file - java

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

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

how to put file in src java folder in java 8

I need to generate a JAVA file which will be used by the other JAVA file within the same project.
This creation will happen while building the project. Because the class is being used within the same project, hence I think it should be placed under src/main/java only.
The issue is from Paths API, I am not able to go to this location directly. I have to hard code the absolute path to my file for creating that class.
Is there any way by which I can use a relative path to create a file?
Also , Should file be there in src folder at first place?

How can I use resources like pictures and text files when I have BlueJ as my IDE?

I want to use things like text files and png files by using BlueJ. I also want it to work on a computer which does not have the resources.
How to do this?
There are many different ways to use external resources and to ensure they are accessible from another computer.
I'm not familiar with BlueJ but if its like most other IDEs...
For code that you write and run in BlueJ you should be able to put the resources next to the compiled .java files.
Say the resource you placed there was called "img.png" and the class containing your main method was called "Main.class".
To load this file as a BufferedImage you would do the following:
BufferedImage image = ImageIO.read(Main.class.getResourcesAsStream("img.png"));
This will load the file based on its location relative to your main class file.
If you want this file to be accessible to someone running your program on a different computer the best option is to bundle it with you code into a .jar file. See THIS for instructions on how to create a jar.
That will show you how to bundle your code into a jar next you have to add the resources (the "img.png" file). To do this download Jar Splice and follow the on screen instructions, its a very simple program to use.
The output from Jar Splice will be a .jar file containing your code and your resources which can be run on any computer with java installed.

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.

Netbeans created Jar does not work, but inside IDE program works

The Netbeans created Jar does not work, but inside the IDE program it works perfectly.
I believe that the main class is set, so I'm not sure what the problem is, I think it might have something to do with the txt files I'm using, in the IDE they are in C:\Users\J\Documents\NetBeansProjects\PointOfSale\src\pointofsale (the text files are with my java files). After building the dist/ jar though the text files are inside the jar with no folders or anything (Jar is in C:\Users\J\Documents\NetBeansProjects\PointOfSale\dist). I this this might be the problem, if its helpful, I access the files using
File file = new File(System.getProperty("user.dir")+"\\src\\pointofsale\\list.txt");
You need to use Class.getResourceAsStream() to load the file. It searches from inside the classpath (and therefore from inside the jar). Now you can't load the list.txt because it doesn't exist in the directory you're specifying, it's inside your jar.
Something along the lines of
getClass().getResourceAsStream("list.txt"); // Or "/list.txt"
Will give you an InputStream you can use to load the file contents.

Categories