participate


CLDC and MIDP - getResourceAsStream questions
<<   Back to Forum  |   Give us Feedback
This topic has 1 reply on 1 page.
jadelindquist
Posts:18
Registered: 11/1/05
getResourceAsStream questions   
Jun 21, 2006 9:15 PM

 
I'm trying to understand how the file locations work with the getResourceAsStream() method.

I have properties files for localization. They're kept in the same package (the default package) as my other source code (which is all in a src directory).

I declare the filenames as such:
private static String _MESSAGES_BUNDLE = "/UI_en_US.properties";
private static String _WORDS_BUNDLE = "/WORDS_en_US.properties";

To open the files, I use these 3 lines of code:
Class clazz = Runtime.getRuntime().getClass();
in = clazz.getResourceAsStream(_MESSAGES_BUNDLE);
inW = clazz.getResourceAsStream(_WORDS_BUNDLE);

Everything works fine until I try to run this application on the web via the MicroEmulator. At that point it can't locate/open the resource files (even though they're bundled into the jar).

I read in a java forum that the CLASSPATH determines where the jvm looks for the files but I also read the following information (which didn't mention the CLASSPATH--plus I don't have permission to define the value of the CLASSPATH env var running on my ISP anyway).

In the following quote, the Class definition says that the rules are profile specific: "
public InputStream getResourceAsStream(String name)

Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are profile specific."

I looked up this section of the MIDP2.0 spec "Resource names refer to the contents of the MIDlet Suite JAR file. Absolute pathnames, beginning with ?/? are
fully qualified file names within the jar file.

Relative pathnames, not beginning with ?/? are relative to the class upon which getResourceAsStream is
called...."

And I experimented with my files a little. I moved the resource files into a package under the /src/ directory and named it 'res'. Thus now I had this directory structure:

/src/ -- contains all other java classes for my project
/src/res/ - - contains the resource files such as UI_en_US.properties

But if I change these lines:
private static String _MESSAGES_BUNDLE = "/UI_en_US.properties";
private static String _WORDS_BUNDLE = "/WORDS_en_US.properties";

To refer to the new subdirectory:
private static String _MESSAGES_BUNDLE = "/res/UI_en_US.properties";
private static String _WORDS_BUNDLE = "/res/WORDS_en_US.properties";

Then getResourceAsStream() can't locate/open the fies.

Similarly, if I move the files back to the src directory and make the path relative by removing the leading /:
private static String _MESSAGES_BUNDLE = "UI_en_US.properties";
private static String _WORDS_BUNDLE = "WORDS_en_US.properties";

getResourceAsStream() can't locate/open the fies.

I'd like to understand how the file locating is working--especially so I can run the MicroEmulator on my webpage which will run my J2ME app. on the webpage.

Any ideas/clarification/etc. would be immensely appreciated!
jdl
 
Printisor
Posts:780
Registered: 04/03/04
Re: getResourceAsStream questions   
Jun 22, 2006 4:33 AM (reply 1 of 1)  (In reply to original post )

 
I must confess I did not read the whole post. But since it is an issue of getResourceAsStream, if you but your properties files into the res directory, then you can access them via
getClass().getResourceAsStream("/firstFile.properties")
for example.

I have never used the MicroEmulator (never even heard of it, but this might be my slip), but this should be a general rule. Once you packaged your application into a jar archieve, you should be able to access your properties files like I mentioned without any problems. In the emulator it works, on a real phone it works, so you should be ok.

When you are usign the method above, the "/" specifies that you are already in the resources folder, so any consecutive folders, like res/localization/english/enFile.properties, can be accessed by using
getClass().getResourceAsStream("/localization/english/enFile.properties") 

Mihai
 
This topic has 1 reply on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums