-1

I am not convinced that it gives me all possibilities for my problem 1) xso said it was not a good answer 2) I would like to find an easiest solution 3) I doesn't address my question why browse the files in a jar has to be different as browse the files in the file system 4) it is an old thread, maybe it changed in Java 8 or Java 9 or in another library ?

even the new link doesn't convince me. Where is addressed the problem to find directories in directories ? Here it is only files in the jar. In my case, I don't know (and I don't want to know) the list of animals, the list of colors,...

I am developing games for eye-trackers (http://gazeplay.net). These games are done in particular for disabled children and their parents are often not accustomed to use computers. Hence, I try to release a software as easy as possible to run.

I have made it with java/javafx. My soft has to be downloaded as a jar and double clicked.

In a new game, the idea is to show some pictures to the child, a voice gives the order and (s)he stare at the correct one. For instance, « Where is the elephant ? » and the child has to select the elephant. It can be the same with colors, cartoon characters,… I tried to do something as generic as possible hence for animals, all are ordered in an « animals » directory where there is a directory for each animal (one for horses, one for eagles,…) and in each directory several images. It is hence easy to add a new animal (just need to add a directory) and easy to add a new picture (add it to the directory). It is easy to make a new game, build a new hierarchy of directories (one for colors, one for cartoon characters,…). Here is an excerpt of my actual directory structure.

actual directory structure

enter image description here

It works pretty well in my IDE but unfortunately it doesn’t work in my jar. It seems easy to show an image if you know its complete path but impossible to use functions as File listFiles(). I try to find solutions but they don’t seem to work. Some people seem to say that is it is possible with nio but I was not able to make it works.

I have two questions : is it possible and if no, why is it impossible ? Folders exist in my jar, they don’t disappear, why is there no way to list their content ? If no, how can I implement a solution as easy as possible ?

Thank you !

Community
  • 1
  • 1
  • did you try this File file= new File(YOURCLASSNAME .class.getClassLoader().getResource("data").toURI().getPath()); file.listFiles(); – mcd Nov 26 '17 at 08:58
  • 1
    It's not possible with the `Files` API since since entries of the jar file are not in a file system. (The jar itself of course is.) 2 solutions to your problem come to mind: 1. Instead of storing the images ect. in the jar, store them in the file system of the machine that runs the app. This may require you to promt the user for the location the images are stored/to store the location in a convenient place as application settings. 2. Add a index file as resource. Simply use a text file to list the resources in the directories and use this info to find the images. – fabian Nov 26 '17 at 09:22
  • @fabian: the question you linked to has an accepted answer which is both not correct and useless for what this OP is trying to do.. – xs0 Nov 26 '17 at 09:46
  • To Fabian, I am new in question asking on stack overflow. The link to this post can maybe help me in the resolution of my problem, it is true. Thank you for that, maybe I will do that, if I don't get a better answer. Unfortunately, the link doesn't manage the "why" part of my question. Why don't we have an unified way to manage both Files and directories in file system and in jar in 2017 in Java ? It seems so odd to me. – Didier Schwab Nov 26 '17 at 09:49
  • How can I delete the duplicate tag. It is an error. Sine it has been set nobody answer the question... – Didier Schwab Nov 26 '17 at 11:47
  • 1
    Please don't make useless edits. Instead, if you don't think your question is a duplicate. make a constructive edit explaining why you think it's a duplicate. I suggest you read [this](https://meta.stackexchange.com/a/194479/349538) to learn how to make a good edit and get your question reopened. – Donald Duck Nov 26 '17 at 12:17
  • 1) i explained 2) why don't you answer my question Donald ? – Didier Schwab Nov 26 '17 at 13:51
  • Any help ? This is an important problem I adress. I Wwould like an answer. How to delete the duplicate tag put on my subject by a random guy Who desapeared after That ? – Didier Schwab Nov 26 '17 at 14:29
  • I am sorry but I was not able to delete the tag. Nobody helped me and fabian never answered to me or to xso. I really need answers, I duplicate the question. Please, Fabian don't troll my question next time and give help to people. I lost my day thanks to you. – Didier Schwab Nov 26 '17 at 16:16
  • *"I am not convinced that it gives me all possibilities for my problem"*. Between the links provided and the comments, there are *so many* possible solutions you have access to for this. What is wrong with fabian's second suggestion (which is also one of the answers to the first linked question): create an index file listing the animals and include it is a resource? What is wrong with creating a zip file for the contents of `images`, and including that as a resource, so you can just read its content on the fly with `java.util.zip`? – James_D Nov 26 '17 at 19:20
  • What is wrong with acheron55's answer to the second link, which explicitly shows you how to treat a jar file as a fie system, which is what you are complaining about in one of the comments here? You just sound like you haven't really attempted any of the solutions in the resources that others have taken time to find for you. – James_D Nov 26 '17 at 19:22
  • > create an index file listing the animals and include it is a resource? This is not what I want to do, it is not generic and easy to manage. – Didier Schwab Nov 26 '17 at 19:45
  • > create an index file listing the animals and include it is a resource? This is not what I want to do, it is not generic and easy to manage. > Ok, I will reconsider acheron55's answer. I go through all answers, this one is maybe good > it still don't answer my question "why browse the files in a jar has to be different as browse the files in the file system ?", all my readings doesn't address that point – Didier Schwab Nov 26 '17 at 19:54
  • 1
    *"why browse the files in a jar has to be different as browse the files in the file system ?"*. Again, as I stated before, and as usual you seemed to completely ignore it, acheron55's answer shows how to treat a jar file the same way as a file system, using the `FileSystem` API. But, again, as I previously explained, they are completely different. The file system can change its contents after the application is deployed. The jar file, by definition, cannot. So there is far less need for an API that lists the content of the jar file: you know the content when you build the app. – James_D Nov 26 '17 at 19:57
  • 1
    "> create an index file listing the animals and include it is a resource? This is not what I want to do, it is not generic and easy to manage." If you don't want to do this by hand, there might be a way to automate it as part of the build, depending on the build tool you are using. – James_D Nov 26 '17 at 19:58
  • Also: "Folders exist in my jar". This is simply not a true statement. There is no entry in your jar file corresponding to a folder, and there is no baked-in hierarchical structure. There are simply a list of entries with each having a name: in the example you posted names include `data/animals/images/bear/bear-422682_1280.jpg`, `data/animals/images/bear/bear-1315128_1280.jpg`, but there is (typically) no entry with name `data/animals`. – James_D Nov 26 '17 at 20:08
  • Thank you James to have taken time to answer and explain to me. I will think about all these information and try the easiest way for us. Regards ! – Didier Schwab Nov 26 '17 at 20:18

1 Answers1

1

When you run code in IDE, it typically uses the files and directories as they are. When you pack the app into a JAR, they are packed into one file, so you get the issues you describe.

Both ways work, because when you run an app, it uses a so-called classpath to find stuff, and you can put both locations of directories and locations of jar files in the classpath, and Java automatically supports both. Normally, when it's used for code and known resources, it's all handled automatically, so you don't have to deal with it, but to discover stuff, you'll have to write some code yourself..

So, if you'd like to discover what files are available, you'll have to look at the classpath, then check for each entry if it's a directory or a jar file. For scanning directories, you can use the Files api, which you're already doing, and for scanning JAR files, you should probably use the JarFile API.

xs0
  • 2,990
  • 17
  • 25
  • he need to traverse each file from data directory using classpath as you said using getResource(). nice ans plus one – mcd Nov 26 '17 at 09:11