File not found using relative path and classpath type
See original GitHub issueIssue details
relative path doesn’t work with classpath type. It works well with internal type but not when assets are in the final jar.
Reproduction steps/code
load a skin containing a bitmap font with relative path :
{
com.badlogic.gdx.graphics.g2d.BitmapFont: {
default-font: { file: ../fonts/game_font.fnt }
}
Note : it will works for desktop when files are in an external folder but fails when files are inside the dist jar (loaded from classpath)
Version of LibGDX and/or relevant dependencies
1.9.6 with Lwjgl2 backend
Stacktrace
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.SerializationException: Error reading file: skins/game-skin.json
at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:579)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:380)
at com.badlogic.gdx.assets.AssetManager.finishLoadingAsset(AssetManager.java:411)
at net.mgsx.game.core.storage.EntityGroupStorage.loadNow(EntityGroupStorage.java:187)
at net.mgsx.game.core.storage.EntityGroupStorage.loadForEditing(EntityGroupStorage.java:86)
at net.mgsx.game.core.EditorApplication.loadWork(EditorApplication.java:113)
at net.mgsx.game.core.EditorApplication.restoreWork(EditorApplication.java:98)
at net.mgsx.game.core.EditorApplication.create(EditorApplication.java:59)
at net.mgsx.examples.platformer.OpenWorldEditorLauncher$1.create(OpenWorldEditorLauncher.java:59)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: skins/game-skin.json
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:98)
at com.badlogic.gdx.assets.loaders.SkinLoader.loadSync(SkinLoader.java:75)
at com.badlogic.gdx.assets.loaders.SkinLoader.loadSync(SkinLoader.java:38)
at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:141)
at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:90)
at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:504)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:378)
... 9 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: skins/game-skin.json
at com.badlogic.gdx.utils.Json.fromJson(Json.java:702)
at com.badlogic.gdx.scenes.scene2d.ui.Skin.load(Skin.java:96)
... 15 more
Caused by: com.badlogic.gdx.utils.SerializationException: Font file not found: ../fonts/game_font.fnt
at com.badlogic.gdx.scenes.scene2d.ui.Skin$3.read(Skin.java:472)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$3.read(Skin.java:463)
at com.badlogic.gdx.utils.Json.readValue(Json.java:892)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:428)
at com.badlogic.gdx.utils.Json.readValue(Json.java:860)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.readNamedObjects(Skin.java:449)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:438)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$2.read(Skin.java:434)
at com.badlogic.gdx.utils.Json.readValue(Json.java:892)
at com.badlogic.gdx.scenes.scene2d.ui.Skin$1.readValue(Skin.java:428)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:700)
... 16 more
Please select the affected platforms
- Android
- iOS (robovm)
- iOS (MOE)
- HTML/GWT
- Windows
- Linux
- MacOS
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Relative path File not found exception - java - Stack Overflow
The path is relative to the working directory of the server, not your project in NetBeans. Given your FNFE I suspect that the...
Read more >Path (Java Platform SE 7 ) - Oracle Help Center
Returns the name of the file or directory denoted by this path as a Path object. The file name is the farthest element...
Read more >Add Classpath Files Dialog - Advanced Installer
The location of the files can be absolute or relative. If the location will not point to an absolute path, it is considered...
Read more >Get the Path of the /src/test/resources Directory in JUnit
We can find the latest version of JUnit 5 on Maven Central. 2. Using java.io.File.
Read more >How to load resources and files in Java - Popular Blocks
It is possible to create a FileInputStream using a relative path, the key is to remember that not only must you specify the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
After few discussions on Discord, i will close this issue. There is no simple way to fix this, actually more than FileHandle would be impacted : AssetManager rely on String path names and some loaders (TMX Map Loader) resolve relative path internally. A quick fix could be to do the same thing when loading a skin but it may have other impacts. So in the end i don’t think it worth it.
If I understand correctly, the FileHandle is instanitated with
FileHandle("skins/../fonts/game_font.fnt")
here. If this is the case, I think this should be fixed - or better: resolved) right in the constructor.For GWT, there’s already a fixSlashes method called with a similar intention, but not dealing dealing with double dots.
Path normalization with Apache 2 license can be found here. Some of this is not needed, but there’s a loop dealing with doubler dots.