nixau
nixau

Reputation: 1095

How to read resource file from classpath in BlackBerry app?

I need to read a resource file from classpath in my BlackBerry application. The directory structure of my project is pretty common: under src directory there are 2 child dirs, one represents source packages root, another - resources root.

When I try to read any resource from classpath Class.getResourceAsStream method retures null

    InputStream rStream = null;
    String path = "/res/default_config.xml";
    try {
        rStream = getClass().getResourceAsStream(path);
    } finally {
        try {
            if (rStream != null) {
                byte[] data = IOUtilities.streamToBytes(rStream);
                System.out.println(new String(data));
                rStream.close();
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

How should I read classpath resource properly?

Upvotes: 0

Views: 6015

Answers (3)

Fostah
Fostah

Reputation: 11776

Even though it's generated as a COD file for running on the device, the JAR file is also created each build. It might be worth checking to make sure your xml file is being put in the directory that you expect it to be in as you can definitely store resources in sub-directories in your application and retrieve them using getClass().getResourceArStream();

Upvotes: 0

Siraj Shan
Siraj Shan

Reputation: 41

I think you specified the path as incorrect way. You just remove the / from the beginning of the path you specified. If you are specifying /. then it will check for you resource folder

Upvotes: 0

Maksym Gontar
Maksym Gontar

Reputation: 22775

And have you tried to put xml file directly into src folder and use getClass().getResourceAsStream("default_config.xml"); ?

Actually cannot reproduce.
Tested on simulator 8800 eJDE 4.2.1.
File was placed in src/res/ folder.

Upvotes: 7

Related Questions