# Resource System Example

## Introduction

The resource system example illustrates how you can easily bundle resources with your Java application by using the class path file engine. The class path file engine searches for the files or directories you specify in all directories or `.jar` files in the current class path. It also allows you to list the contents of directories in the class path, even if they are actually distributed over several different actual locations.

You can tell Qt to use the class path file engine when specifying the file name. Simply prefix the name with `classpath:`, and Qt will automatically invoke the class path file engine. If you wish to search for a file at a specific location in the class path, then a second request format is available: `classpath:<absolute path>#<filename>`. The first variable, the absolute path, can be the full path of a directory or `.jar` file, while the filename should be the resource you want to request. Qt will then only search the specific directory or `.jar` file for the file. This can be useful in cases when you want to work with `.jar` files inside of Qt.

## The Example

The Resource System (`com.trolltech.examples.ResourceSystem`) example shows off the different features of the class path file engine.

The example has two modes:

- In the default mode it will list the contents of the entire class path, and in addition the contents of a bundled `.jar` file.
- In the second mode it will limit itself to the contents of the `.jar` file.

In either mode, it will show these contents in a QTreeWidget and let the user select images to show them in the window. It first searches for the `ResourceSystem.jar` file, and gets the actual path on disk for this file by calling canonicalFilePath() on the QFileInfo object:

        String jar_file_name = "classpath:com/trolltech/examples/ResourceSystem.jar";
        QFileInfo jarInfo = new QFileInfo(jar_file_name);

The `ResourceSystem.jar` file is placed in the same directory as the currently running class file, and should be available using the path of the current package.

In the default mode, the application should list the contents of the entire class path. We do this by specifying the root directory and prefixing it with `classpath:` with the following assignment:

        searchPath = "classpath:/";

Every entry of the class path has a root directory, so this directory is distributed over all parts of the class path; i.e. if the classpath contains two entries: `/my_java_files/:/java_sdk/rt.jar`, the root directory will contain all contents of `/my_java_files` as well as all contents in the root of `rt.jar`. We simply use QDir on the root directory and entryList() to get its contents.

Since we also want the application to list the contents of `ResourceSystem.jar`, we will add this to the class path file engine in the default mode:

        QtJambiInternal.addSearchPathForResourceEngine(jarInfo.canonicalFilePath());

When the file is stored directly on the disk in the main file system, the canonical file path will return the actual, complete path and file name of the file. If it were stored within another `.jar` file, we wouldn't be able to add it to the class path without first extracting it (using the copy() method in QFile).

In the alternative mode, we only want to search `ResourceSystem.jar`. This is accomplished by requesting the root from this specific `.jar` file:

        searchPath = "classpath:" + jarInfo.canonicalFilePath() + "#/";

After the search path has been selected, the application recursively fills the tree widget with any directory or image file it finds. From this point on it works as you would expect it to if it was a normal image viewer application that searched the main file system. When the user selects a file from the tree widget, the application will attempt to load it as a pixmap and set it on its label at the bottom of the window:

        QFileInfo info = selected_item.getInfo();
        if (info.exists() && !info.isDir()) {
            QPixmap pm = new QPixmap(info.absoluteFilePath());
            m_currentImage.setPixmap(pm);
        }

## Conclusion

What you will see when exploring this example, is that the bundled `.jar` file contains a couple of image files in the path `/com/trolltech/examples/images`. The same path will contain an image in the Qt package (at the location in the class path where Java found the currently running class file).

When running in the default mode, all three images will be selectable as if they were in a single directory. In the alternative mode, only the two images contained in the `.jar` file are available.