How to read resource file from classpath in BlackBerry app?

6k Views Asked by At

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?

3

There are 3 best solutions below

5
On BEST ANSWER

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.

0
On

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();

0
On

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