Loading Resources In Java Using Eclipes IDE. There are many questions posted on Experts Exchange by people starting out in Java programming, who are using an IDE, most often Eclipse and sometimes NetBeans that center around their application not being able to find a file. This is quite often an image they want to use in their application. This often happens when they create an 'executable jar'.
An executable jar is an application that can be made to run in a windowing environment by double-clicking a shortcut icon, much the same as one can get a native application to run.
The ultimate cause of the problem is the failure to understand the concept of the 'current directory' or the 'working directory'. Wikipedia tells us:
"In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with each process. When the process refers to a file using a simple file name or relative path (as opposed to a file designated by a full path from a root directory), the reference is interpreted relative to the current working directory of the process. So for example a process with working directory /rabbit-hats that asks to create the file foo.txt will end up creating the file /rabbit-hats/foo.txt." ( http://en.wikipedia.org/wiki/Working_directory )
When developers starting out want to display, say my-image.png in the program, they often refer to this only by the path "my-image.png", having copied this into the project. This will work fine when the application is run through the IDE, but when, like all good java applications, it is made into an executable jar and the resulting icon is double-clicked, the application fails. Worse still, since Windows associates the open action of a .jar file with javaw.exe, which is designed to run without a console window, apparently nothing at all happens and the reason the application fails is a mystery. The application is not 'relocatable'.
The solution to this problem is to package the resources together with the class files in the application jar. One of the best ways to do this is to create a 'resources' folder at the package root and to place any needed resources in there. An video example of doing that in the Eclipse IDE can be seen here.
Tags: Loading Resources In Java Using Eclipes IDE, Java (software Platform), Technology, Java (programming Language)