• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with File's listFiles()  RSS feed

 
Alec Porter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written some code to list all the files in a folder. The problem is that I'm getting a NullPointer Exception at f.listFiles(). The problem is it works fine when I run it with my IDE but when I run it as a JAR it doesn't work. So I'm going to assume that I'm getting the path of the directory incorrectly. So my question is how do I get the correct path for my files?


 
Paul Clapham
Sheriff
Posts: 22721
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alec Porter wrote:So I'm going to assume that I'm getting the path of the directory incorrectly.


Sounds like a good working assumption. So my first step would be to look at that path, to see what's wrong with it.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check what you get with this:

and you might get the clue where you're making a mistake.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I expect there was also a problem with the f.canRead() code - didn't that return false? Is there a zero-length file named CantReadError.txt sitting around somewhere? Knowing about an error like that is critical information, and I suspect it's been overlooked. Actually, the idea of using names of zero-length files for error notification is... unusual. I would strongly consider adding some sort of standard error logging - I recommend log4j. Put your error messages somewhere consistent where you will see them.

Also, if f.canRead() returns false - is there any reason to continue with the f.listFiles() code? It seems like this code should only be executed if f.canRead() returned true. Otherwise you're just creating more errors that obfuscate the original error message.
 
Alec Porter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This gives me:




file:/C:/Users/Sam/Desktop/ImageTest.jar!/images/

And this:




jar:file:/C:/Users/Sam/Desktop/ImageTest.jar!/images/


Not sure what the mistake is,

I tried .replaceFirst("file:", ""); on the path and that didn't work.


And yes canRead is returning false. So I was assuming that the path is incorrect from that.

Also this is in my policy file for testing so I don't think there should be a problem with security:

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure the problem is that you're trying to read from a jar file - the url is telling us there's a jar file at C:/Users/Sam/Desktop/ImageTest.jar, and this jar contains a path named "images". Unfortunately, the File class' listFiles() method doesn't know how to look inside a jar file and read as if there's a real directory there. Maybe someone else knows a nice shortcut here. Otherwise I would try taking that url and splitting it into two parts: what's before the !, and what's after:

file:/C:/Users/Sam/Desktop/ImageTest.jar!/images/ -> "file:/C:/Users/Sam/Desktop/ImageTest.jar" + "/images/"

The first part is a url of a real file, which you can open using JarFile or some similar class. Then you can get a list of all the entries in the jar file, and find which ones have a path matching the one you're looking for, "/images/". Then you can examine each individual entry in /images/ and read it if necessary.
 
Campbell Ritchie
Marshal
Posts: 56227
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also appear to have empty catch blocks, eg line 33 and 59. Those are dangerous; if an Exception occurs you don’t know about it. Put something in there, eg e.printStackTrace(); Then you will know something has gone wrong.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to print to console (as your example would) contents of /images/ from .jar file, like names of images stored there, you should check the JarFile class from java.util.jar; package.
 
Alec Porter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to print to console (as your example would) contents of /images/ from .jar file, like names of images stored there, you should check the JarFile class from java.util.jar; package.


Yes this is what I would like to do. I'll take a look and do some messing around and see what I can come up with. Thanks. The problem is that the jar isn't created till I make the code into a jar file that means there would be no way to test it in the compiler, should I make a separate jar file for images? I wanted to have the images in the same jar as the main class so I could load them like I did in the code. Is it not possible to get the listFiles() to work because their in the jar? And if so how does new ImageIcon( getClass().getResource("/images/image.png") ) this work for files in the jar but not listFiles?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a closer look at the documentation for the ImageIcon constructors. Also for Class#getResource(...).

There's no File involved in
new ImageIcon( getClass().getResource("/images/image.png") )
 
Alec Porter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the default Toolkit inside the ImageIcon class gets the image from a FileStream. I was just wondering why it was able to get the image without problems but I couldn't get the list of the files from that directory.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a FileStream?
 
Paul Clapham
Sheriff
Posts: 22721
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alec Porter wrote:The problem is that the jar isn't created till I make the code into a jar file that means there would be no way to test it in the compiler, should I make a separate jar file for images? I wanted to have the images in the same jar as the main class so I could load them like I did in the code. Is it not possible to get the listFiles() to work because their in the jar?


Normally the way that people do things, when they want to distribute their project as a jar which includes resources like images, is to use the Class.getResource() method to retrieve a resource. You've already seen that in this thread and you already know how to make it work when the resource is in the jar. So far so good.

Now, the way you make it work in your IDE is to realize that the getResource() method gets resources from the classpath. So that means, in your example, that you should put an "images" directory in your project's build path and then put the images in it. Sorry I can't be more specific, but how you do that depends on what your IDE is and even on how your project is set up in the IDE. But once you have that done, then Class.getResource() will find the images when you run the code in your IDE. If you do it right, then you can even ask your IDE to create a jar and it will automatically include your "images" directory.

So that's when you know the name of your resource. But where you're going outside of normal practice is that you're designing a system with a collection of resources of which you don't know all the names. And the getResource() method doesn't have a way of returning a list of URLs which is comparable to the File.listFiles() method. That's where you get into hacks like finding the URL of the jar file and using other code which then looks into the jar to get the resources. Unfortunately, as you see, this prevents you from testing in your IDE.

What I would recommend is that you create a resource which is a text file containing the names of all the images in your "image" directory. Then your first step is to use getResource() to get this text file, and then you can loop through it and get all of your image resources, also using getResource().

This simplifies your programming and testing tasks, at the expense of requiring you to maintain an extra resource which is somewhat redundant.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!