-1

int x = new File("src/Images").list().length;

It works fine for me until I want to run it as an executable jar file. "/Images" & "Images" won't work and I've read that I can't use new File inside a jar.

I want the jar file to run, locate the file inside and count the amount of images in the folder.

Is there a simple alternative to the above line of code?

I've tried playing with DirectoryStream and a few other things but have had no luck.

Cheers

Lawffle
  • 13
  • 2
  • http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file?rq=1 – Erik Pragt Jul 19 '14 at 21:48
  • While this is "possible", it is difficult and makes a number of assumptions which you won't be able to gurentee at runtime. Instead, as part of your build process, you should create a file which contains a list of all the files, then at runtime, you can simply use Class#getResource to locate and read the contents of this file and use Class#getResource to load the images – MadProgrammer Jul 19 '14 at 22:13

1 Answers1

0

It usually uses the directory of .jar file as root directory.

The easiest way to "make it work" is this: Having folder src in the same folder as the .jar file and in src folder put the Images folder.

So it looks like this, when you execute the .jar:

...../myproject/myproject.jar
...../myproject/src/Images
libik
  • 22,239
  • 9
  • 44
  • 87
  • Src won't exist when the project is built into a jar, nor should you really want to expose the src directory at runtime. Externalising the images might not be a bad solution in of itself, I just wouldn't use the src directory ;) – MadProgrammer Jul 19 '14 at 22:11