Loading Resources in Java using Eclipse IDEFebruary 13, 2012 11:25 am ·
This week’s member blog post of the week comes from Charles Johnson (CEHJ). Charles is a certified Java programmer who has been a member of Experts Exchange since 1997. He currently holds 20 certifications, including a Savant certification in Java Programming Language and a Genius certification in New to Java Progamming.
Charles also shares his tech expertise on his website, TechnoJeeves. In today’s post, Charles shares his expertise and a how-to video in order to answer a question frequently asked by new Java programmers.
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.”
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:
Importantly, the resource needs to be addressed AS a resource, and not a file, in the Java source code. This can be done with getClass().getResource or getClass().getResourceAsStream, which invokes the classloader to deal with the resource. The argument to the above methods is a String representing not a file, but a url relative to a path component of the classpath. Since the default classpath when we are dealing with an application is effectively always ‘/’, we can be certain that, from whatever package in our code we happen to be addressing (to use the earlier example) ‘/resources/my-image.png’, it will be loaded successfully. What’s more, since we are no longer treating the resource as a file, the current directory (or working directory) problem no longer exists so our application is now relocatable.