Issue file assets Libgdx - java

I have a problem to load a file from assets.
In pratcise I have to load TiledMap file and I do it in this way:
arrayTiledMap.add(new TmxMapLoader().load(Gdx.files.internal("scenario.tmx").path())));
(I add it in array for other reason)
In the project the tmx file(scenario.tmx) is located in the android assets folder.
When I execute the program in Eclipse there aren't problems , but when I create the JAR file for Desktop project and I execute it I get this error on console(I launch it by terminal):
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: Documents/University/Programming/Street.png (Internal)
The file Street.png is a file that I used in tiledMap editor for create the map.
At this point I check the JAR file contents and In the root directory of JAR there is the Documents folder and in it there is University folder and so on.
Because if the path is in the JAR file I get this error?
What is that wrong?
Thank you very much for your time, this error is driving me crazy
If you need other code, in particular, tell me.
(although I do not think since the problem only occurs when loading the file)

Because if the path is in the JAR file I get this error?
No. Its most likely you aren't exporting the JAR correctly.
File -> Export -> Java -> Runnable JAR File
And be sure to check this:
package required libraries into generated JAR.

Related

How to export working jar with non-jar dependencies

im fairly new to java and im making a pokemon style game for practice and i would like to be able to send the game to my friends.
here is the main problem: the game works fine in my netbeans IDE, but using the jar file in my dist folder does not work and throws a nullPointerException. i have narrowed down the problem. my game uses imageIcons and png/gif images that i have imported in my libraries. im getting access to them like this
Icon bckground = new javax.swing.ImageIcon(getClass().getResource("/pictures/BG.gif"));
i am unsure how to get the images into the lib folder for the program to find. i have tried copying the files straight into the lib folder and creating a folder for them called pictures; neither worked. right now the lib folder contains only a single jar from one of my other libraries. (that is the only jar file that i am importing to my libraries)
Pic of what it looks like in IDE
In my case, i did something like this.
final BufferedImage image = ImageIO.read(new File("something.jpg"));
I just transferred something.jpg into the dist folder, and it worked fine.
After clean-building your project, put the BG.gif into the dist folder, then run your jar file in dist folder. Now everything should be fine. When sending your game to your friends, you can encapsulte (hide, set read-only) your code (google encapsultaion java), then with the BG.gif being transferred into dist folder, archive the project. Then your friends only need to unzip it and find jar file in dist folder.
Hope this will help:)
Please put your picture (BG.gif) in the package (directory) where it is used as icon Icon bckground = new javax.swing.ImageIcon(getClass().getResource("BG.gif")); inside the jar file. You need to change the path of the file first getResource("BG.gif") and then create the jar file from IDE. If the jar does not contain the image, you can open the jar using using any unzip application (winrar etc.) and copy and paste the the image file in the directory where the class is present. Please let me know the outcome.

xml FileNotFoundException using slick2D library in java

private ConfigurableEmitter emitter;
File xmlFile = new File("ressources/emitter.xml");
emitter = ParticleIO.loadEmitter(xmlFile);
If I launch the project in eclipse, everything will works fine, but after I export my project and use JarSplice to create a .jar file, when I launch the jar file using the command prompt, the program will crash launching a FileNotFoundException, saying it cannot find the path specified.
java.io.FileNotFoundException: ressources\emitter.xml (The system cannot find the
path specified)
The surprising thing is that just before opening the xml file, I open a .png file located at the same place as the xml file, and this without any problem. In addition, when I open the .jar file I exported using winrar, I can find my xml file under the ressources folder. What can be the problem here?
Thank you!
EDIT :
Code with solution:
InputStream i=this.getClass().getClassLoader().
getResourceAsStream("ressources/test.xml");
emitter = ParticleIO.loadEmitter(i);
When you pack your project to JAR your resource don't live on disk, but are compressed into the JAR itself and you have to load as resource.There are a lot of guide on SO on how to load resource from JAR using ClassLoader.getResourceAsStrem() (follow this link)

Problems with loading CLP file from JAR

I am using CLIPSJNI.
What I have is:
Environment clips = new Environment();
clips.load("main.clp");
where main.clp is put in the same level as src and bin folder.
This runs fine in Eclipse. However when I export to JAR. It cannot work.
I understand that there are some problems with the path when we export to JAR.
So I've seen people suggesting using this.getClass().getResourceStream() but this is not the case. Because what I need is the name of the file, not its content.
Any suggestions on how to fix this?
The issue is that the load is being done within the native library on the C side which is being passed a file name as an argument. The C code has no concept of a JAR file or how to extract files embedded within one. I think what you would need to do is always place your .clp files within the JAR file and then have a routine which extracts the data from the JAR file and saves it to a file. You can then load it using the load method and delete the file once done.

How to fix this java.io.FileNotFoundException?

I'm trying to load a .csv file in a program but for some reason, it's unable to find the file. Where should I place the file?
Console
It looks like the file is in the src directory... which almost certainly isn't the working directory you're running in.
Options:
Specify an absolute filename
Copy the file to your working directory
Change the working directory to src
Specify a relative filename, having worked out where the working directory is
Include it as a resource instead, and load it using Class.getResourceAsStream
The file is located in the src directory so in order to access it you should use
src/Elevator.csv
As long as files are located inside your project folder you can access them using relative paths.
For example if a file is located under the Elevator folder then you access the file by using only its filename.
Elevator.csv
A good principle when using additional files in your project is creating separate folders from the ones that the source files are located. So you could create a folder resources under the project folder and place your file there. You can access then the file by using
resources/Elevator.csv
the path which it is trying to read is surely not exact as the path in which that file is actually present.Try printing absolute path of that file and compare it with actual path of your file.
I tried with all the above mention solution, but it didn't work..
but i went to my project folder and delete the target and tried to compile the project again. it then worked successfully

Deploying .jar file: Why can't I load icon files?

I'm exporting a simple java project that includes two directories; src and Icons. Icons is a directory that contains three .png files.
I'm exporting to an executable .jar file using File -> Export. The export works properly and the .jar file contains the Icon directory. But I can't get the correct path for the .png files when the project is deployed. During the development I'm using the following path:
Icons/picture.png
and it works as long as I run from within the Eclipse IDE.
How do I get the correct path for the icons?
Your code is looking for the image outside of the .jar file. Try the URL constructor of ImageIcon instead.
Icon icon = new ImageIcon(getClass().getResource("Icons/picture.png"));
See Class.getResource().
mmyers is correct, but be aware that getClass().getResource() will load resources relative to the package where the class is defined. I suspect your icons are packaged at the root of the jar file and not relative to the class itself. To get resources from the root of the classpath, try:
getClass().getClassLoader().getResourceAsStream("Icons/picture.png")

Categories