I have been able to get resources to load in both Netbeans and a JAR file, such as ImageIcons, but today I am unable to “read” from same directory when running my program from a JAR file. I want to list the directories and files in a classpath directory, but when I run the program from a JAR file the directory becomes unreadable. It works fine in Netbeans.
tileDirectory = new File(TileDirectoryLister.class.getResource("/resources/images/tiles").getPath());
jta.append("\nThe class path for the tile directory is:"
+ "\n\t" + tileDirectory.getPath());
jta.append("\nThe tile directory is readable: " + tileDirectory.canRead());
When run from Netbeans it prints true; when run from a JAR file it prints false. Why does canRead print false when I run from a JAR file?
A File represents a path on the file system. Your icons are inside a jar file. You can’t use a File to access them.
To get access to some resource in the jar file, just use
to get an InputStream, or
to get a URL.