0

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>
MAGS94
  • 500
  • 4
  • 15
  • 2
    run it from cmd to see errors – Madhawa Priyashantha Jul 06 '16 at 11:13
  • 3
    do you have any stacktrace or could you elaborate what 'not working' means? – Rhayene Jul 06 '16 at 11:13
  • 1
    "what are the possible causes" too many to fit in one answer :) – niceman Jul 06 '16 at 11:13
  • please add your Error stacktrace to identify the error cause. – Vikrant Kashyap Jul 06 '16 at 11:14
  • The two packages methods are obviously different and are creating different artefacts. As has already been suggested, reproduce your stack trace. If possible compare what has been built by gradle and IntelliJ. There has to be some differences in there. – PeterS Jul 06 '16 at 11:25
  • 1
    Have you already tried this? http://stackoverflow.com/questions/25138314/com-badlogic-gdx-utils-gdxruntimeexception-couldnt-load-file-error – Vishal Jumani Jul 06 '16 at 11:28
  • that mostly indicates a missing file (exactly "level.json"), if you have path like this in your code "levels/level.json" then in the folder that contains the result jar file there should be folder "levels" with your "level.json" inside it – niceman Jul 06 '16 at 11:33
  • we can say the same for any relative path in your code – niceman Jul 06 '16 at 11:34

1 Answers1

1

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")
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45