Search code examples
javajsongradlelibgdx

Java Application not working after Packaging into jar file


I had a java app working properly by Intellij IDEA but not working after packaging into jar file

what are the possible causes ? and how can I find the mistake ?

the app is a gradle project (libgdx) and I packaged it by command line gradlew desktop:dist

it give me this

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRun timeException: Error reading file: json\levels.json <Absolute>
at com.badlogic.gdx.files.FileHandle.read<FileHandle.java:144>

Solution

  • After packaging the application JAR file, is the JSON file part of the JAR? Then you have to change the way of accessing the file. You can't access a file through an absolute file path, but instead you should use

    InputStream in = ClassLoader.getSystemResourceAsStream("path/to/file/in/jar")