|  | Home · Examples

# The Qt Jambi Resource System

Qt Jambi provides a convenient way to bundle resources with you Java application - such as icons, translation files, etc. With the resource system, Jambi can search for files and directories you specify in the current classpath.

## Using the Resource System

Using the resource system, you can specify resource file names relative to the classpath. This is done by prefixing the filename of the resource with "classpath:", for instance:

            QFile resourceFile = new QFile("classpath:images/star.png");

Qt Jambi then goes through the classpath entries and appends `images/star.png` to each entry. Notice that this makes it easy to package resources in `.jar` files.

For instance if you have an image, `star.png`, located in the root directory of the `.jar` file, you can simply access it by "classpath:star.png". The `.jar` file must be in the classpath, of course.

It is also possible to search for a file in a specific (absolute) location in the classpath, using the syntax "classpath:<absolute path>#<filename>". The absolute path can be the full path to a directory or a `.jar` file, while the filename is the name of the resource.

In the following code snippet, Qt Jambi will pick the classpath entry `images`, and search for the `star.png` in that directory.

            QPixmap pixmap = new QPixmap("classpath:images#/star.png");

Giving a specific location is mostly useful if you want to get an absolute path to a resource, i.e., a path to one specific file in the classpath. We give a code example below.

            QFile absoluteFilePath = new QFile("classpath:images#/star.png");
            QFile relativeFilePath = new QFile("classpath:images/star.png");

If your classpath contains `resources.jar` and the directory `images`, Qt Jambi will search for `absoluteFilePath` only in `images`, and search for `relativeFilePath` in `images/images/` and in `images` inside the resources `.jar` file.

You can list contents of a directory in the classpath, for instance, using a QDir.

            QDir dir = new QDir("classpath:images");
            List<QFileInfo> images = dir.entryInfoList();

The resource system is read-only, so it is not possible to create new directories, write resource files, etc.

## The Resource System Implementation

In Jambi it is possible to write custom file engines by extending QAbstractFileEngine. A file engine knows how to resolve file names, read, and write files through a QIODevice. The QDir, QFile, and QFileInfo classes all make use of a file file engine. The resource system is implemented in such a custom file engine.

Note that this makes the resource system specific to Qt Jambi, so, for instance, you cannot access resources using the `java.io` package.

See also:
  Resource System Example, and QAbstractFileEngine.