0

I created a desktop game in eclipse using Libgdx. The game runs great in eclipse, however the FileHandle list() method doesn't work when exported to a jar file. I used the following lines of code:

songFiles = Gdx.files.internal("./bin/" + "songs/").list();
System.out.println(songFiles[0]);

Again, in eclipse it prints the file location.However when exported to a jar, I get a java.lang.ArrayIndexOutOfBoundsException: 0 error.

Cannot export default Java libGDX project as a jar from Eclipse looks similar to the issue that I am having

Community
  • 1
  • 1
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – ItamarG3 Dec 23 '16 at 18:03
  • 1
    No, the problem is that the list() method doesn't operate the same as it does in eclipse. I think that it is something to do with the file structure when a project is exported to a jar file. – Will Roberts Dec 23 '16 at 19:43
  • I was about to write something like this but can't figure out what exactly the problem is. – Gürtuğ Güngör Dec 23 '16 at 19:48
  • 2
    To quote the javadocs: "On the desktop, an Files.FileType.Internal handle to a directory on the classpath will return a zero length array.". https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/files/FileHandle.html#list--. Or to quote the wiki: "Listing of internal directories is not supported on Desktop." https://github.com/libgdx/libgdx/wiki/File-handling#listing-and-checking-properties-of-files. – Xoppa Dec 23 '16 at 20:23

2 Answers2

0

It says you created the list or array but u didn't initialize it with length. That's why you have 0 at the end of error message.

  • 1
    This wouldn't be the cause since the array is supplied by the method list() in libgdx's FIleHandle class (https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/files/FileHandle.html). That also doesn't explain why it works when running in eclipse, since eclipse would catch that. – Will Roberts Dec 23 '16 at 19:40
0

Since the list() method cannot be used on desktop applications, I will solve this issue by reading and writing the filenames in the songs directory to a text file.